Skip to main content

django-apiview

A set of django tools to help you create JSON service.

Install

pip install django-apiview

Installed Decorators

  • @apiview
  • @requires(*parameter_names)
  • @choices(field, choices, allow_none=False)
  • @between(field, min, max, include_min=True, include_max=True, annotation=Number, allow_none=False)
  • @rsa_decrypt(field, private_key_instance)
  • @meta_variable(variable_name, meta_name)
  • @cache(key, expire=None, cache_name="default", get_from_cache=True, set_to_cache=True)
  • @safe_apiview

Note:

  • @apiview
    1. apiview = Apiview(SimpleJsonPacker())
  • @safe_apiview
    1. Requires django_middleware_global_request, see django_middleware_global_request's usage at https://pypi.org/project/django-middleware-global-request/.
    2. Requires server rsa settings: RSA_PRIVATEKEY_STRING, RSA_PRIVATEKEY (load RSA_PRIVATEKEY_STRING as RsaKey)
  • @cache
    1. Optional setting: DJANGO_APIVIEW_DISABLE_CACHE_HEADER_NAME = "HTTP_DISABLE_CACHE"
    2. Optional setting: DJANGO_APIVIEW_DEFAULT_CACHE_EXPIRE = None

Usage

Note:

  • You DON'T need to put django_apiview into INSTALLED_APPS.
  • Apiview always set csrf_exempt=True.
  • @apiview or @safe_apiview decorator must be the first decorator.
  • Return raw data without serialized, we'll do result pack for you.

app/views.py

import time
from django_apiview.views import apiview
from django_apiview.views import requires
from django_apiview.views import choices
from django_apiview.views import between

@apiview
def ping():
    return "pong"

@apiview
def timestamp():
    return int(time.time())

@apiview
@requires("msg")
def echo(msg: str):
    return msg

@apiview
def getBooleanResult(value : bool):
    return value

@apiview
def getIntegerResult(value: int):
    return value

@apiview
def getBytesResult(value: bytes):
    return value

@apiview
@choices("op", ["+", "-", "*", "/"])
@between("a", 2, 10, include_min=False)
@between("b", 2, 10, include_max=False)
def calc(a: int, op: str, b: int):
    if op == "+":
        return a + b
    if op == "-":
        return a - b
    if op == "*":
        return a * b
    if op == "/":
        return a / b

@safe_apiview(get_client_rsa_publickey=get_client_rsa_publickey)
def safe_ping():
    return "pong"

Bug report

Please report any issues at https://github.com/zencore-cn/zencore-issues.

Releases

v0.6.0 2020/09/06

  • Add safe_apiview.

v0.5.0 2020/08/13

  • Add cache decorator.
  • Add func's default values to View.data.

v0.4.0 2020/08/06

  • Add meta_variable decorator.
  • Datetime value encode to native time, and in format like 2020-08-06 14:41:00.

v0.3.4 2020/07/26

  • Fix BizError class check problem.

v0.3.3 2020/07/24

  • Add rsa_decrypt decorator.

v0.3.2 2020/07/18

  • Add Apiview class based implementation.
  • Add setup_result_packer api.
  • Rename simple_json_result_packer to simple_result_packer.

v0.3.1 2020/07/01

  • Change app name from apiview to django_apiview.
  • Add parameter validators.
  • WARN: NOT backward compatible.

v0.2.0

  • Using fastutils.typingutils for annotation cast.
  • Add result pack mechanism.
  • Move example views from the main app to example app and the example app is not include in published package.

v0.1.3

  • Add logging while getting result failed in @apiview.
  • Add Map, List annotations.

v0.1.2

  • Fix form process problem.

v0.1.1

  • Add PAYLOAD injection, PAYLOAD field has low priority.

v0.1.0

  • First release,

Download files

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

Source Distribution

django-apiview-0.6.0.tar.gz (7.7 kB view details)

Uploaded Source

File details

Details for the file django-apiview-0.6.0.tar.gz.

File metadata

  • Download URL: django-apiview-0.6.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for django-apiview-0.6.0.tar.gz
Algorithm Hash digest
SHA256 0bf394dc05aac64b16cace5454ac93d1ae03f00ca19ad4683111157e5de6b16d
MD5 20c2081e23e366ee01bc02d404507955
BLAKE2b-256 442bea4923536d7570074e301a0798bb441ad56c9efe74672e4bb3dcff70e96f

See more details on using hashes here.

Release history Release notifications | RSS feed

1.0.32

2 files

1.0.19

2 files

0.9.29

2 files

0.9.27

1 file

0.9.26

1 file

0.9.25

1 file

0.9.21

1 file

0.9.20

1 file

0.9.7

1 file

0.9.2

1 file

0.8.9

1 file

0.8.8

1 file

0.8.6

1 file

0.8.4

1 file

0.8.3

1 file

0.8.2

1 file

0.8.1

1 file

0.8.0

1 file

0.7.0

1 file

This release

0.6.0 This release

1 file

0.5.0

1 file

0.4.0

1 file

0.3.4

1 file

0.3.3

1 file

0.3.2

1 file

0.3.1

1 file

0.2.0

1 file

0.1.3

1 file

0.1.2

1 file

0.1.1

1 file

0.1.0

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page