Skip to main content

动态API客户端库,支持链式调用和自动方法解析

Project description

ApiClient

动态API客户端库,支持链式调用和自动方法解析。

注意: 包在PyPI上的名称是 liwancai-ApiClient,但导入时使用 ApiClient

安装

pip install liwancai-ApiClient

使用

基本用法

# 导入整个包的所有功能
from ApiClient import *

# 创建API客户端实例
client = ApiClient("https://api.example.com")

# 设置认证token
client.set_authtoken("your_token_here")

# 链式调用API
result = client.users.Get_profile(user_id=123)
print(result)

链式调用语法

ApiClient支持链式调用,自动解析方法和路径:

# GET 请求:以 Get_ 开头的方法
result = client.api.users.Get_list(page=1, size=10)

# POST 请求:其他方法
result = client.api.users.create(name="张三", email="zhangsan@example.com")

# 多级路径
result = client.admin.system.logs.Get_recent(hours=24)

路径转换规则

ApiClient会自动转换链式调用的路径:

链式调用 HTTP方法 实际URL 说明
client.users.Get_profile GET /users/profile Get_前缀表示GET方法
client.users.create POST /users/create 默认POST方法
client.admin.users.Get_list GET /admin/users/list 多级路径支持

命名转换规则

  • Group部分(除最后一个部分外):转为小写
  • 接口名(最后一个部分):
    • 如果是GET请求,去掉Get_前缀后转为kebab-case
    • 其他请求直接转为kebab-case

示例:

  • Get_profileprofile(GET请求,去掉Get_前缀)
  • createUsercreate-user(POST请求)
  • GetAPIListapi-list(GET请求,去掉Get_前缀)

API参考

ApiClient类

构造函数

ApiClient(base_url="https://api.liwancai.com")

参数:

  • base_url (str): API基础URL,默认为liwancai API

方法

set_authtoken(token)

设置认证token

参数:

  • token (str): 认证token
getattr(name)

开始链式调用

参数:

  • name (str): 路径第一部分

APIProxy类

内部使用的代理类,负责链式调用的路径收集和方法解析。

示例

完整示例

from ApiClient import ApiClient

# 创建客户端
client = ApiClient("https://api.example.com")
client.set_authtoken("your_jwt_token")

# 获取用户列表
users = client.users.Get_list(page=1, size=20)

# 创建新用户
new_user = client.users.create(
    name="李四",
    email="lisi@example.com",
    role="user"
)

# 获取用户详情
user_detail = client.users.Get_detail(user_id=123)

# 更新用户信息
updated_user = client.users.update(
    user_id=123,
    name="王五",
    email="wangwu@example.com"
)

# 删除用户
delete_result = client.users.delete(user_id=123)

错误处理

try:
    result = client.users.Get_profile(user_id=999)
    print("请求成功:", result)
except Exception as e:
    print("请求失败:", e)

依赖

  • requests>=2.25.0 - HTTP请求库
  • liwancai-PyLog>=1.0.0 - 日志库

许可证

MIT License

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

liwancai_apiclient-1.0.7.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

liwancai_apiclient-1.0.7-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file liwancai_apiclient-1.0.7.tar.gz.

File metadata

  • Download URL: liwancai_apiclient-1.0.7.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for liwancai_apiclient-1.0.7.tar.gz
Algorithm Hash digest
SHA256 72ed1f1596ed0267dc988d83aa9526c9f54686693522a15274701b5d41a30bc5
MD5 101a882a91ec1a50800435ec0e2b2ef0
BLAKE2b-256 c461921d6eede69c59d409af6fe8a768ad1b1db690c02b60364d1222845b7211

See more details on using hashes here.

File details

Details for the file liwancai_apiclient-1.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for liwancai_apiclient-1.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 f93eba5e118b700fdebb348faf6ef0b7c2a75e3759c976b7c6b27e45177c6438
MD5 78230637da222117f0ea46af62877500
BLAKE2b-256 f1462b5a62d3faeb19072d0780f8a609b02f9f2c88b5f7fa0aa48304fc4ce53d

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