Skip to main content

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_profile → profile(GET请求,去掉Get_前缀)
  • createUser → create-user(POST请求)
  • GetAPIList → api-list(GET请求,去掉Get_前缀)

API参考

ApiClient类

构造函数

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

参数:

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

方法

set_authtoken(token)

设置认证token

参数:

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

开始链式调用

参数:

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

APIProxy类

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

UserMgr 用户管理接口调用

对接 UserMgr(用户认证/RBAC 服务)时,登录接口使用 account 参数(支持手机号/邮箱/昵称/自定义账号)。

⚠️ 不要使用旧版的 phone 参数登录——动态解析客户端按参数名转发,写 phone= 会导致登录失败(phone 仅用于注册接口)。

from ApiClient import ApiClient

client = ApiClient("http://localhost:2003")

# 登录(POST /login,参数必须是 account)
resp = client.login(account="admin", password="your_password")
# resp.data.accessToken -> WebToken

# 设置 WebToken 后调用用户接口
client.set_webtoken(resp.data.accessToken)
me   = client.user.Get_me()           # GET /user/me
perm = client.user.Get_role_perm()    # GET /user/role-perm

# 管理员接口(需管理员角色)
users = client.admin.Get_user_list()  # GET /admin/user-list
logs  = client.admin.Get_login_log()  # GET /admin/login-log

路径映射(动态客户端链式调用 → UserMgr 路由):

链式调用 方法/路径
client.login(account=..., password=...) POST /login
client.user.Get_me() GET /user/me
client.user.Get_role_perm() GET /user/role-perm
client.user.Post_info_update(...) POST /user/info-update
client.admin.Get_user_list() GET /admin/user-list
client.admin.Get_login_log() GET /admin/login-log

方法名大小写规则见上文「命名转换规则」;Post_/Get_ 前缀分别对应 POST/GET 方法。

示例

完整示例

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

Metadata

Release files for liwancai-ApiClient 1.0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for liwancai-ApiClient 1.0.10
File Size Uploaded Provenance Actions
liwancai_apiclient-1.0.10.tar.gz 5.7 kB Without provenance Details for liwancai_apiclient-1.0.10.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for liwancai-ApiClient 1.0.10
File Interpreter ABI Platform Actions
liwancai_apiclient-1.0.10-py3-none-any.whl Python 3 none any Details for liwancai_apiclient-1.0.10-py3-none-any.whl

Total release size: 11.8 kB

Release files / liwancai_apiclient-1.0.10.tar.gz

Download URL liwancai_apiclient-1.0.10.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
20d250b536b77feb4da99e01a3f13b0a063ffb6919661e98f383ff12083054b6
BLAKE2b-256 checksum
How to use checksums
df5b4ffa8e3ef3f623fa479c6f0b96b10ed72ba09c7afffa5d7028a821b19f1a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / liwancai_apiclient-1.0.10-py3-none-any.whl

Download URL liwancai_apiclient-1.0.10-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
03f41988d5664cc4fef5decf283dec30f3577937bc4308f3885ee8f7a4c510a1
BLAKE2b-256 checksum
How to use checksums
6e7d51247139f35a77d22f90b3b7a2f7b77637771a41f687ef8582dc0746494a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for liwancai-ApiClient 1.0.10

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.

Release history Release notifications | RSS feed

This release

1.0.10 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page