Add restframework based API
This commit is contained in:
parent
15f4971bde
commit
dc0111ce6d
|
@ -0,0 +1,3 @@
|
||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
# Register your models here.
|
|
@ -0,0 +1,5 @@
|
||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class Apiv2Config(AppConfig):
|
||||||
|
name = 'apiv2'
|
|
@ -0,0 +1,3 @@
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
# Create your models here.
|
|
@ -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')
|
|
@ -0,0 +1,3 @@
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
# Create your tests here.
|
|
@ -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)),
|
||||||
|
]
|
|
@ -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
|
|
@ -41,6 +41,7 @@ INSTALLED_APPS = (
|
||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
|
'rest_framework',
|
||||||
'bgpdata',
|
'bgpdata',
|
||||||
'tastypie',
|
'tastypie',
|
||||||
)
|
)
|
||||||
|
|
|
@ -8,6 +8,7 @@ from django.views.generic import RedirectView
|
||||||
|
|
||||||
import bgpdata.urls
|
import bgpdata.urls
|
||||||
import bgpdata.api_urls
|
import bgpdata.api_urls
|
||||||
|
import apiv2.urls
|
||||||
|
|
||||||
urlpatterns = (
|
urlpatterns = (
|
||||||
# Examples:
|
# Examples:
|
||||||
|
@ -16,6 +17,7 @@ urlpatterns = (
|
||||||
url(r'^$', RedirectView.as_view(url='/map/')),
|
url(r'^$', RedirectView.as_view(url='/map/')),
|
||||||
url(r'^map/', include(bgpdata.urls)),
|
url(r'^map/', include(bgpdata.urls)),
|
||||||
url(r'^api/v1/', include(bgpdata.api_urls)),
|
url(r'^api/v1/', include(bgpdata.api_urls)),
|
||||||
|
url(r'^api/v2/', include(apiv2.urls)),
|
||||||
|
|
||||||
url(r'^admin/', admin.site.urls),
|
url(r'^admin/', admin.site.urls),
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue