https://github.com/pablo-moreno/spook
Django Rest Framework extension to interconnect external HTTP APIs.
https://github.com/pablo-moreno/spook
django django-rest-framework external-api
Last synced: 9 months ago
JSON representation
Django Rest Framework extension to interconnect external HTTP APIs.
- Host: GitHub
- URL: https://github.com/pablo-moreno/spook
- Owner: pablo-moreno
- License: mit
- Created: 2020-07-11T10:57:01.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-09-29T10:51:16.000Z (over 3 years ago)
- Last Synced: 2025-03-13T00:04:32.851Z (10 months ago)
- Topics: django, django-rest-framework, external-api
- Language: Python
- Homepage:
- Size: 2.23 MB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Django Spook
[](https://pypi.org/project/spook/)
[](https://codecov.io/gh/pablo-moreno/spook/)
[](https://github.com/psf/black)
[](https://pypistats.org/packages/spook)
Library to interconnect multiple external HTTP APIs as Http Resources
## Installation
```bash
pip install spook
```
## Usage
Declare a serializer class for your input validation
```python
# 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
```python
# app/validators.py
from spook.validators import InputValidator
from app.serializers import MySerializer
class MyResourceInputValidator(InputValidator):
serializer_class = MySerializer
```
Declare an API Resource class.
```python
# 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
```python
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
```python
# 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**
```python
pip install poetry
```
**Install dependencies**
```python
poetry install
```
**Run tests**
```python
poetry run pytest --cov=spook
```