Skip to main content

Django Rest Framework library to interconnect external APIs

Project description

Django Spook

PyPI codecov Code style: black

Library to interconnect multiple external HTTP APIs as Http Services

Installation

pip install spook

Usage

Declare a serializer class for your input validation

from rest_framework import serializers

class MySerializer(serializers.ModelSerializer):
    name = serializers.CharField()
    age = serializers.IntegerField()
    
    class Meta:
        fields = ('name', 'age', )

Declare an InputValidator

from spook.validators import InputValidator


class MyResourceInputValidator(InputValidator):
    serializer_class = MySerializer

Declare an API Resource class.

from spook.resources import APIResource


class MyResource(APIResource):
    api_url = 'https://my.external/api'
    validator = MyResourceInputValidator

Now you can instance MyResource class and use the methods

resource = MyResource()

# List resources
resource.list()

# Retrieve a single resource
resource.retrieve(pk=1)

# Create resource
resource.create({'name': 'Pablo', 'age': 28})

# Update resource
resource.update(pk=1, data={'name': 'Pablo Moreno'})

# Delete resource
resource.delete(pk=1)

There are also some views available

from spook.views import (
    APIResourceRetrieveView, APIResourceListView, APIResourceCreateView, APIResourcePutView,
    APIResourceRetrieveUpdateView, APIResourceRetrieveUpdateDestroyView, APIResourceListCreateView,
)


class ListCreateProductResourceView(APIResourceListCreateView):
    resource = ProductResource

    def get_token(self, request):
        return ''  # Wee need to override get_token()


class RetrieveUpdateDestroyProductResourceView(APIResourceRetrieveUpdateDestroyView):
    resource = ProductResource

    def get_token(self, request):
        return ''

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spook-3.1.1.tar.gz (6.3 kB view hashes)

Uploaded Source

Built Distribution

spook-3.1.1-py3-none-any.whl (7.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page