Add restframework based API

This commit is contained in:
Sebastian Lohff 2020-06-07 02:27:39 +02:00
parent 15f4971bde
commit dc0111ce6d
11 changed files with 92 additions and 0 deletions

0
apiv2/__init__.py Normal file
View File

3
apiv2/admin.py Normal file
View File

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

5
apiv2/apps.py Normal file
View File

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

View File

3
apiv2/models.py Normal file
View File

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

33
apiv2/serializers.py Normal file
View File

@ -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 Normal file
View File

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

12
apiv2/urls.py Normal file
View File

@ -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 Normal file
View File

@ -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

View File

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

View File

@ -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 = (
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),
)