django-formapi
Create JSON API:s with HMAC authentication and Django form-validation.
Installation
Install django-formapi in your python environment
$ pip install django-formapi
Add formapi to your INSTALLED_APPS setting.
INSTALLED_APPS = (
...
'formapi',
)
Add formapi.urls to your urls.py.
urlpatterns = patterns('',
...
url(r'^api/', include('formapi.urls')),
)
Usage
Go ahead and create a calls.py.
class DivisionCall(calls.APICall):
"""
Returns the quotient of two integers
"""
dividend = forms.FloatField()
divisor = forms.FloatField()
def action(self, test):
dividend = self.cleaned_data.get('dividend')
divisor = self.cleaned_data.get('divisor')
return dividend / divisor
API.register(DivisionCall, 'math', 'divide', version='v1.0.0')
Just create a class like your regular Django Forms but inheriting from APICall. Define the fields that your API-call should receive. The action method is called when your fields have been validated and what is returned will be JSON-encoded as a response to the API-caller. The API.register call takes your APICall-class as first argument, the second argument is the namespace the API-call should reside in, the third argument is the name of your call and the fourth the version. This will result in an url in the form of api/[version]/[namespace]/[call_name]/ so we would get /api/v1.0.0/math/divide/.
A valid call with the parameters {'dividend': 5, 'divisor': 2} would result in this response:
{"errors": {}, "data": 5, "success": true}
An invalid call with the parameters {'dividend': "five", 'divisor': 2} would result in this response:
{"errors": {"dividend": ["Enter a number."]}, "data": false, "success": false}
Authentication
By default APICalls have HMAC-authentication turned on. Disable it by setting signed_requests = False on your APICall.
If not disabled users of the API will have to sign their calls. To do this they need a secret generate, create a APIKey through the django admin interface. On save a personal secret and key will be generated for the API-user.
To build a call signature for the DivisonCall create a querystring of the calls parameters sorted by the keys dividend=5&divisor=2. Create a HMAC using SHA1 hash function. Example in python:
import hmac
from hashlib import sha1
hmac_sign = hmac.new(secret, urllib2.quote('dividend=5&divisor=2'), sha1).hexdigest()
A signed request against DivisionCall would have the parameters {'dividend': 5, 'divisor': 2, 'key': generated_key, 'sign': hmac_sign}
Documentation
Visit /api/discover for a brief documentation of the registered API-calls.
Metadata
Release files for django-formapi 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| django-formapi-0.2.0.tar.gz | 93.6 kB | Without provenance | Details |
Release files / django-formapi-0.2.0.tar.gz
| Download URL | django-formapi-0.2.0.tar.gz |
|---|---|
| Size | 93.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
951cb6df2c25f9e875afce40aeda0f476412ed9b2599d508b04fd5e15ec5549e
|
|
BLAKE2b-256 checksum How to use checksums |
0d5df29c1c428e8c503444ad7a8bcb01d4dd06ed9b9b3b9eba89bd0ae993ec28
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for django-formapi 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.