Skip to main content

ECAPI SDK for Python

Project description

ecapi-sdk (Python)

ECAPI 的 Python SDK,面向当前重构后的 REST/OpenAPI 契约。当前门面由 openapi.json 生成并覆盖全部 160 个接口,并随包提供 py.typedclient.pyi,方便 IDE 显示参数、必填字段和 docstring。

安装

pip install ecapi-sdk

仓库内更新 SDK 文档和类型提示:

python3 sdk/generate_sdk_api_reference.py
python3 -m py_compile sdk/python/ecapi-sdk/src/ecapi_sdk/client.py

快速开始

from ecapi_sdk import ECAPIClient, ECAPIError, is_ecapi_error

client = ECAPIClient(
    base_url="https://api.easecation.net",
    auth={"type": "apiKey", "apiKey": "ec_xxx"},
)

try:
    me = client.user.get_me()
    player = client.player.get_info({"displayName": "Steve"})
    wallet = client.player.get_wallet("player-ecid")
except ECAPIError as error:
    print(error.status, error.code, error.request_id, str(error))
    raise

认证方式

client.set_api_key("ec_xxx")
client.set_bearer_token("iam-jwt-token")
client.set_app_session_token("app-session-token")
client.clear_auth()

也可以在单次请求中传入 auth=... 覆盖默认认证;传 auth=None 表示本次不发送认证。

认证来自 IAM callback、短期 token 或其它动态来源时,可以使用 auth_provider,SDK 会在每次请求前调用它:

client = ECAPIClient(
    auth_provider=lambda: {"type": "apiKey", "apiKey": get_current_api_key()},
)

如果 auth_provider 返回 awaitable,且当前没有正在运行的 event loop,同步客户端会用 asyncio.run() 等待它;在已有 event loop 中请使用同步 provider 或在业务层先解析认证。

类型提示与文档

  • client.pyclient.pyi 均从 OpenAPI 生成,query/body 会生成 TypedDict,字段必填性来自 OpenAPI。
  • API_REFERENCE.md 会列出方法、接口说明、权限、HTTP 方法、路径、必填参数和可选参数。
  • ECAPIError 会提取服务端错误 envelope 中的 coderequest_iddetailsfield 和用户友好的 message

常用调用

client.system.get_health()
client.system.get_liveness()
client.system.get_readiness()

client.player.search_ecid({"search": "Steve"})
client.player.cutoff_leaderboard.get_leaderboard({"game": "bedwar"})
client.punish.create({
    "type": "WARNING",
    "ecid": "player-ecid",
    "source": "console",
    "reason": "测试警告",
})

未封装或临时接口可使用统一入口:

client.request("GET", "/players/info", query={"displayName": "Steve"})

契约说明

本 SDK 不提供旧路径兼容别名,所有方法都调用新的 REST/OpenAPI 路径。OpenAPI 是路径、参数和权限说明的唯一事实源,完整方法请查看 API_REFERENCE.md

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

ecapi_sdk-3.2.6.tar.gz (19.3 kB view details)

Uploaded Source

Built Distribution

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

ecapi_sdk-3.2.6-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

Details for the file ecapi_sdk-3.2.6.tar.gz.

File metadata

  • Download URL: ecapi_sdk-3.2.6.tar.gz
  • Upload date:
  • Size: 19.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ecapi_sdk-3.2.6.tar.gz
Algorithm Hash digest
SHA256 9b5a834fab6553a93212f6422fc3dddb7dac28a5bdf7f7efb7d4a41b094538bf
MD5 5c5690ca6fba38e37a27f9b68ca55a9e
BLAKE2b-256 f0d18aa0623016642a8e16f2d2eedce755c5aea2082ffc3c912290a60ffa590c

See more details on using hashes here.

File details

Details for the file ecapi_sdk-3.2.6-py3-none-any.whl.

File metadata

  • Download URL: ecapi_sdk-3.2.6-py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ecapi_sdk-3.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 cc7753d9dd6bd736508dffd81563e7e1d706bd4bbcbe7ba667851d84e516ec08
MD5 b5ce60c5a16984cf8118b718b5494cb1
BLAKE2b-256 6d687be2c98d2a2bec8b0806bea6384ac6aae5e481d24eb4084dd893690491ed

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