Skip to main content

Django简易json api接口封装。

Project description

django-apis

Django简易json api接口框架。使用pydantic做接口参数验证。

安装

pip install django-apis

配置项

  • DJANGO_APIS_OPENAPI_SERVERS: Swagger服务器列表。
  • DJANGO_APIS_OPENAPI_TITLE: Swagger标题。
  • DJANGO_APIS_OPENAPI_VERSION: Swagger版本号。
  • DJANGO_APIS_OPENAPI_DESCRIPTION: Swagger描述。
  • DJANGO_APIS_OPENAPI_SECURITY_DEFINITIONS: Swagger认证列表。
  • DJANGO_APIS_OPENAPI_LOGIN_URL: Swagger管理界面访问受管理登录保护。在未登录访问时跳转的登录页面地址。

如何自定义Apiview类型

注意事项

  1. 定义自己的base_response_class,并重置自定义的Apiview中的base_response_class属性。
  2. 重载Apiview中的make_responsemake_error_response方法。

自定义Apiview类型案例

from django.http import JsonResponse
from django_apis.views import Apiview

from typing import Any
import pydantic

class ErrorInfo(pydantic.BaseModel):
    code: int = 0
    message: str = "OK"

# 响应体格式定义要与make_response及make_error_response实际响应匹配
class ExampleApiviewBaseResponse(pydantic.BaseModel):
    success: bool = True
    error: ErrorInfo = ErrorInfo()
    result: Any = None

class ExampleApiview(Apiview):
    base_response_data_field = "result"
    base_response_class = ExampleApiviewBaseResponse

    def make_response(self, data):
        return JsonResponse(
            {
                "success": True,
                "error": {
                    "code": 0,
                    "message": "OK",
                },
                "result": data,
            },
            json_dumps_params={
                "ensure_ascii": False,
            },
        )

    # 强制所有响应的http status_code值为200
    def make_error_response(self, code, message, status_code=200):
        return JsonResponse(
            {
                "success": True,
                "error": {
                    "code": code,
                    "message": message,
                },
                "result": None,
            },
            json_dumps_params={
                "ensure_ascii": False,
            },
        )

example_apiview = ExampleApiview()

Swagger认证列表案例

DJANGO_APIS_OPENAPI_SECURITY_DEFINITIONS = {
    "BasicAuth": {
        "type": "http",
        "scheme": "basic",
    },
    "BearerAuth": {
        "type": "http",
        "scheme": "bearer",
    },
    "ApikeyAuth": {
        "type": "apiKey",
        "name": "X-API-Key",
        "in": "header",
    },
    "OpenID": {
        "type": "openIdConnect",
        "openIdConnectUrl": "https://example.com/.well-known/openid-configuration",
    },
    "OAuth2": {
        "type": "oauth2",
        "flows": {
            "authorizationCode": {
                "authorizationUrl": "https://example.com/oauth/authorize",
                "tokenUrl": "https://example.com/oauth/token",
                "scopes": {
                    "read": "Grants read access",
                    "write": "Grants write access",
                    "admin": "Grants access to admin operations",
                },
            }
        },
    },
}

使用案例

import pydantic
from django_apis.views import apiview
from django_apis.schemas import OptionalUploadedFiles

@apiview()
def ping() -> str:
    return "pong"


class EchoPayload(pydantic.BaseMode):
    msg: str

@apiview(methods="post")
def echo(payload: EchoPayload) -> str:
    return payload.msg

class ApplyForm(pydantic.BaseMode):
    name: str
    start_date: str
    end_date: str
    files: OptionalUploadedFiles

@apiview(methods="post")
def apply(form: ApplyForm) -> bool:
    return True

版本记录

v0.2.0

  • 注意:不兼容0.1.x。

v0.2.1

  • 使用openapi 3.1.0版本.
  • 添加并通过更多关于复合模型的测试用例。

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

django-apis-0.2.1.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

django_apis-0.2.1-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file django-apis-0.2.1.tar.gz.

File metadata

  • Download URL: django-apis-0.2.1.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for django-apis-0.2.1.tar.gz
Algorithm Hash digest
SHA256 fe1880bd657048b768c839818dfcaee05bc3111aacb3c11ede121ee07be69d73
MD5 b35f5e86f8219c22b046ad67102e05a2
BLAKE2b-256 1cd33de1f9c369a890d4b9b045822530721244d90b806d1886e50e9deabcc792

See more details on using hashes here.

File details

Details for the file django_apis-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: django_apis-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for django_apis-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3dc5a86140a876d8e024c6c0dbdf38d8668f707bce7ae1c1b33bbea6fc0a68a3
MD5 ced2477cacf4573cd5bd1761aba3f096
BLAKE2b-256 294516abc1cbdb90a4aa7d734bd897c27bb0053fe690465ece47181578c728da

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