Browse Source

Add restframework based API

master
Sebastian Lohff 2 years ago
parent
commit
dc0111ce6d
  1. 0
      apiv2/__init__.py
  2. 3
      apiv2/admin.py
  3. 5
      apiv2/apps.py
  4. 0
      apiv2/migrations/__init__.py
  5. 3
      apiv2/models.py
  6. 33
      apiv2/serializers.py
  7. 3
      apiv2/tests.py
  8. 12
      apiv2/urls.py
  9. 30
      apiv2/views.py
  10. 1
      dnmapper/settings.py
  11. 2
      dnmapper/urls.py

0
apiv2/__init__.py

3
apiv2/admin.py

@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
apiv2/apps.py

@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
from django.apps import AppConfig
class Apiv2Config(AppConfig):
name = 'apiv2'

0
apiv2/migrations/__init__.py

3
apiv2/models.py

@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

33
apiv2/serializers.py

@ -0,0 +1,33 @@ @@ -0,0 +1,33 @@
import json
from rest_framework import serializers
from bgpdata.models import CrawlRun, ASLastSeen
class CrawlRunSerializer(serializers.ModelSerializer):
graph = serializers.ReadOnlyField()
class Meta:
model = CrawlRun
fields = ('id', 'startTime', 'endTime', 'asCount', 'asOnlineCount', 'asOfflineCount', 'peeringCount', 'graph')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if "with_graph" not in self.context['request'].query_params:
self.fields.pop("graph")
def to_representation(self, instance):
data = super().to_representation(instance)
print(data)
for elem in data:
if "graph" in data and isinstance(data['graph'], str):
data['graph'] = json.loads(data['graph'])
return data
class ASLastSeenSerializer(serializers.ModelSerializer):
class Meta:
model = ASLastSeen
fields = ('id', 'asn', 'directlyCrawled', 'online', 'lastSeen', 'crawlLastSeen')

3
apiv2/tests.py

@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

12
apiv2/urls.py

@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
from django.conf.urls import include, url
from rest_framework import routers
from apiv2.views import CrawlRunViewSet, ASLastSeenViewSet
router = routers.DefaultRouter()
router.register('crawlrun', CrawlRunViewSet)
router.register('asn', ASLastSeenViewSet)
urlpatterns = [
url('', include(router.urls)),
]

30
apiv2/views.py

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
from rest_framework import viewsets
from apiv2.serializers import CrawlRunSerializer, ASLastSeenSerializer
from backend import crawler
from bgpdata.models import CrawlRun, ASLastSeen
class CrawlRunViewSet(viewsets.ReadOnlyModelViewSet):
"""Represents a CrawlRun.
Graph is shown if with_graph is passed as query arg.
With /live/ the current network is shown (internally, a crawl is triggered for each request)"""
queryset = CrawlRun.objects.all()
serializer_class = CrawlRunSerializer
def get_object(self):
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
if self.kwargs.get(lookup_url_kwarg) == "live":
net = crawler.get_current_network()
obj = crawler.make_crawl_from_net(net)
self.check_object_permissions(self.request, obj)
else:
obj = super().get_object()
return obj
class ASLastSeenViewSet(viewsets.ReadOnlyModelViewSet):
queryset = ASLastSeen.objects.all()
serializer_class = ASLastSeenSerializer

1
dnmapper/settings.py

@ -41,6 +41,7 @@ INSTALLED_APPS = ( @@ -41,6 +41,7 @@ INSTALLED_APPS = (
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'bgpdata',
'tastypie',
)

2
dnmapper/urls.py

@ -8,6 +8,7 @@ from django.views.generic import RedirectView @@ -8,6 +8,7 @@ from django.views.generic import RedirectView
import bgpdata.urls
import bgpdata.api_urls
import apiv2.urls
urlpatterns = (
# Examples:
@ -16,6 +17,7 @@ urlpatterns = ( @@ -16,6 +17,7 @@ urlpatterns = (
url(r'^$', RedirectView.as_view(url='/map/')),
url(r'^map/', include(bgpdata.urls)),
url(r'^api/v1/', include(bgpdata.api_urls)),
url(r'^api/v2/', include(apiv2.urls)),
url(r'^admin/', admin.site.urls),
)

Loading…
Cancel
Save