Skip to main content

Django Rest Framework library to interconnect external APIs

Project description

Django Spook

PyPI codecov Code style: black PyPI - Downloads

Library to interconnect multiple external HTTP APIs as Http Resources

Installation

pip install spook

Usage

Declare a serializer class for your input validation

# app/serializers.py
from rest_framework import serializers

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

Declare an InputValidator

# app/validators.py
from spook.validators import InputValidator
from app.serializers import MySerializer


class MyResourceInputValidator(InputValidator):
    serializer_class = MySerializer

Declare an API Resource class.

# app/resources.py
from spook.resources import APIResource
from app.validators import MyResourceInputValidator


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

# app/views.py
from spook.views import (
    APIResourceRetrieveView, APIResourceListView, APIResourceCreateView, APIResourcePutView,
    APIResourceRetrieveUpdateView, APIResourceRetrieveUpdateDestroyView, APIResourceListCreateView,
)
from app.resources import ProductResource


class ListCreateProductResourceView(APIResourceListCreateView):
    resource = ProductResource

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


class RetrieveUpdateDestroyProductResourceView(APIResourceRetrieveUpdateDestroyView):
    resource = ProductResource

    def get_token(self, request):
        return ''

Development

We recommend to use a virtual environment

Install poetry

pip install poetry

Install dependencies

poetry install

Run tests

poetry run pytest --cov=spook

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.2.8.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

spook-3.2.8-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file spook-3.2.8.tar.gz.

File metadata

  • Download URL: spook-3.2.8.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.7.14 Linux/5.15.0-1020-azure

File hashes

Hashes for spook-3.2.8.tar.gz
Algorithm Hash digest
SHA256 91327afd7cb19dae3d9b1e307a3fdcda8f710fb0d08b4e5c056b2771f936ee61
MD5 9d8dfdadd52899c5898de4f1e752e608
BLAKE2b-256 499f3c6dd9037ff76baab492d6768481bf552e622be842777f5550601647eac1

See more details on using hashes here.

File details

Details for the file spook-3.2.8-py3-none-any.whl.

File metadata

  • Download URL: spook-3.2.8-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.7.14 Linux/5.15.0-1020-azure

File hashes

Hashes for spook-3.2.8-py3-none-any.whl
Algorithm Hash digest
SHA256 a1640aa2445de0616359dd7ff9702f8e32196a487db9677f28ffc684cf68766e
MD5 65f75ea436686e36012e68ba6f64c5c4
BLAKE2b-256 b22c761dd84ca4c1489be352edb73591d88245db17172faeacbf9f93fbfa3d8a

See more details on using hashes here.

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