Skip to main content

client for openapi service.

Project description

OpenAPI网关服务客户端

功能列表

  1. 校验token,获取用户信息
  2. 统一上报操作日志至网关侧

使用指南

安装

pip install ke-openapi-ait-client

配置网关域名

通过环境变量设置网关域名

OPENAPI_HOST = "https://***.com"

token校验

from ait_openapi import validate_token, support_model, account_balance_enough

token = "****"
# 根据token,解析用户身份
user_name = validate_token(token)
# 查询当前登陆用户,是否具有指定模型的权限
supported = support_model(token, 'model_name')
# 账户余额判断,支持传入指定判断阈值,服务方可以在每次用户请求前,根据本次预估花费,对用户余额进行校验,若余额不足,则可拒绝请求

balance = account_balance_enough(token, cost=1.0)

如果token无效,抛出AuthorizationException异常

上报操作日志

1. 上报模式

为降低上报操作日志延迟对业务逻辑的影响,上报操作日志采用异步上报模式

async异步上报

日志上报,将采用异步io的方式,上报操作日志,这种方式,可以在单线程中,同时处理多个并发非阻塞IO操作

from ait_openapi import operation_log


@operation_log()
def safety_check(request, *, validate_output: bool):
    pass

2. 上报参数更新

操作日志记录,需要传递一些动态参数,需导入预定义的contextvar,在请求起始处进行设置, 请求结束时清空contextvar

from ait_openapi import trace_id_context, caller_id_context, request_url_context

# 整个链路处理前,设置
t_token = trace_id_context.set("*********")  # trace_id 当前请求链路唯一标识
c_token = caller_id_context.set("*********")  # caller_id 调用方标识, 通过user_info.username获取
r_token = request_url_context.set("*********")  # request_url 当前请求url
# #
# 请求处理
# 清空contextvar
trace_id_context.reset(t_token)
caller_id_context.reset(c_token)
request_url_context.reset(r_token)
2.1 使用middleware设置contextvar, 将自动进行token解析,以及上下文设置
from ait_openapi.middleware import HttpContextMiddleware, WebSocketHttpContextMiddleware
from fastapi import FastAPI

app = FastAPI()
app.add_middleware(HttpContextMiddleware, exclude_path=["/v1/actuator/health/<pattern>"])
app.add_middleware(WebSocketHttpContextMiddleware, exclude_path=["/v1/actuator/health/<pattern>"])

3. 日志记录配置

日志记录中,op_type字段默认取值为当前方法名称,可通过在装饰器中传入参数进行覆盖。该装饰器可同时用于同步方法以及异步协程的日志上报 为了进行调用计费,如果当前操作日志中的信息,需要用于回调计费,则需要在装饰器中传入is_cost_log=True

from ait_openapi import operation_log


@operation_log('safety_check', is_cost_log=False)
def safety_check(request, *, validate_output: bool):
    pass


@operation_log(is_cost_log=False)
async def safety_check(request, *, validate_output: bool):
    pass

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

ke-openapi-ait-client-0.0.1.1.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ke_openapi_ait_client-0.0.1.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file ke-openapi-ait-client-0.0.1.1.tar.gz.

File metadata

  • Download URL: ke-openapi-ait-client-0.0.1.1.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.5

File hashes

Hashes for ke-openapi-ait-client-0.0.1.1.tar.gz
Algorithm Hash digest
SHA256 6219de4a73468da6d9d251c4d8273d88cb2bf72c5b530f314d636b2800ebd296
MD5 2dc58c8726f2675e13fe91eb686f3e06
BLAKE2b-256 f08c4b1498cb5c2471be304a343af74dbc2f07aefb5bc9cb0d5e232f00bee406

See more details on using hashes here.

File details

Details for the file ke_openapi_ait_client-0.0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for ke_openapi_ait_client-0.0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d27a3610ebce8ff41149e7a4909a6bcb004382766dc9fc440eab0012e22f1186
MD5 f73730ecdd6d09ef586d4a9fc00e0367
BLAKE2b-256 0035918f2a5f555939cf001efdf95339caf5be5cd82de2ad65ad6fc793513254

See more details on using hashes here.

Supported by

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