Django Simple JSON API
Install
pip install django-simple-json-api
Instructions
Add dsjapi.decorators.api or dsjapi.decorators.api_rq to your view function.
Return a JSON serializable object or a django.http.HttpResponse object.
Raising a dsjapi.exceptions.ServerException exception will automatically return the appropriate JSON response.
Decorator parameters
field
dsjapi.fields.Field
Expected field in request body. Default isNone.allowedMethods
listofstr
Allowed HTTP methods (uppercase). Default is["GET", "POST"].safeEncoding
bool
Trueto allow non-dictresponse objects. Default isFalse.
@api vs @api_rq
@apionly provides the validated request data.@api_rqprovides adsjapi.decorators.Requestobject that contains bothrequest(the originaldjango.http.HttpRequestobject) anddata(the validated request data) attributes.
Examples
Views
from dsjapi.decorators import api
from dsjapi.fields import *
@api (DictField ({
"name": StringField (minLength=2, maxLength=32, regex=r'^[a-zA-Z ]+$'),
"bio": StringField (maxLength=64, missing=Field.Do.DEFAULT, default=""), # Optional (default to "")
"age": IntField (min=18, max=130)
}))
def index (data):
result = putUser (data["name"], data["bio"], data["age"])
return { "result": result }
from dsjapi.decorators import api
from dsjapi.fields import *
@api (DictField ({
"start": ListField.byLength (2, FloatField ()),
"end": ListField.byLength (2, FloatField (), missing=Field.Do.SKIP), # Optional
"time": FloatField (min=120, max=60*60)
}))
def index (data):
if "end" in data:
route = getARoute (data["start"], data["time"])
else:
route = getABRoute (data["start"], data["end"], data["time"])
return { "route": route }
from dsjapi.decorators import api
from dsjapi.fields import *
@api (DictField ({
"numbers": ListField.byLength (2, FloatField (min=0))
}))
def index (data):
sum = 0
for number in data["numbers"]:
sum += number
avg = sum / len (data["numbers"])
return { "average": avg }
from dsjapi.decorators import api
from dsjapi.fields import *
from dsjapi.exceptions import *
@api (DictField ({
"id": IntField (min=0, max=4096),
"password": StringField (min=8, max=32)
}))
def index (data):
user = getUserById (data["id"])
if user is None:
raise NotFoundException ("User not found")
if user.getPassword () != data["password"]:
raise AuthenticationException ("Wrong password")
return { "id": user.getId (), "score": user.getScore () }
Custom fields
from dsjapi.fields import IntField, FieldException
class EvenNumberField (IntField):
def __init__ (self, min=None, max=None, missing=Field.Do.RAISE, error=Field.Do.RAISE, default=None):
super ().__init__ (int, min=min, max=max, missing=missing, error=error, default=default)
def clean (self, value):
value = super ().clean (value)
if value % 2 != 0:
raise FieldException ("Odd number")
return value
Metadata
Release files for django-simple-json-api 0.0.3
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 | Actions |
|---|---|---|---|---|
| django-simple-json-api-0.0.3.tar.gz | 5.4 kB | Without provenance | Details for django-simple-json-api-0.0.3.tar.gz |
Release files / django-simple-json-api-0.0.3.tar.gz
| Download URL | django-simple-json-api-0.0.3.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3284d0ee2a6552a150beb8dedabd89b545c9182df6413ca4ed1fcc3894251dfe
|
|
BLAKE2b-256 checksum How to use checksums |
facf603b6f9297ffe721a95470e4bbced95528978724fc1cbd90ff210babd33a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3
|
Security for django-simple-json-api 0.0.3
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.