Skip to main content

Python client SDK for AgileConfig

Project description

py-agile-config

面向 AgileConfig 的 Python 客户端 SDK。

本项目是 github.com/animacaeli/go-agile-config 的 Python 迁移版本,并在关键 行为上保持兼容:

  • 通过 HTTP Basic Auth 拉取已发布配置。
  • 通过 WebSocket 接收实时配置更新。
  • 配置存在 group 时按 group:key 存储,否则按 key 存储。
  • 默认要求 HTTPS/WSS,可信 HTTP/WS 需要显式开启。
  • 限制 HTTP 响应体和 WebSocket 消息大小。
  • 支持服务发现 API 和多 appId 客户端。

安装

pip install py-agile-config

快速开始

from agileconfig import Client, Options

client = Client(
    "https://config.example.com",
    "my-app-id",
    "my-app-secret",
    Options(env="DEV"),
)

client.start()
try:
    host = client.get_string("db.host", "localhost")
    print("DB Host:", host)
finally:
    client.stop()

本地开发或可信内网测试可以显式允许 HTTP:

client = Client(
    "http://localhost:5000",
    "my-app-id",
    "my-app-secret",
    Options(allow_insecure_http=True),
)

配置读取

value, ok = client.get("db:host")
host = client.get_string("db.host", "localhost")
value, ok = client.get_by_group("database", "host")
all_configs = client.get_all()

SDK 也提供 client.GetString(...) 等 Go 风格兼容别名,便于迁移 Go SDK 示例 代码。

配置变更回调

def on_change(keys: list[str]) -> None:
    for key in keys:
        print("changed:", key)

client = Client(
    server_url,
    app_id,
    secret,
    Options(on_change=on_change),
)

回调会收到新增、删除或修改的配置 key。初始加载时如果存在配置,也会触发回调, 以保持与 Go SDK 一致。

多 App ID

from agileconfig import MultiClient, MultiClientApp, Options

client = MultiClient(
    server_url,
    [
        MultiClientApp("mysql", "mysql-secret"),
        MultiClientApp("redis", "redis-secret"),
    ],
    Options(env="DEV"),
    on_change=lambda app_id, keys: print(app_id, keys),
)

client.start()
try:
    mysql_host, ok = client.get_by_group("mysql", "db", "host")
    redis_addr = client.get_string("redis", "addr", "localhost:6379")
finally:
    client.stop()

服务发现

from agileconfig import HeartbeatMode, RegisterService

services = client.list_services()
online = client.list_online_services()
offline = client.list_offline_services()

result = client.register_service(
    RegisterService(
        service_id="order-service",
        service_name="Order Service",
        ip="10.0.0.8",
        port=8080,
        metadata=["version=1.0.0"],
        heartbeat_mode=HeartbeatMode.CLIENT,
    )
)

client.heartbeat(result.unique_id)
client.unregister_service(result.unique_id)

开发

python3.13 -m venv .venv
.venv/bin/python -m pip install -e '.[test]'
.venv/bin/python -m pytest -q

发布到 PyPI

.venv/bin/python -m pip install build twine
.venv/bin/python -m build
.venv/bin/python -m twine upload dist/*

使用 PyPI API token 时设置:

export TWINE_USERNAME=__token__
export TWINE_PASSWORD=<pypi-token>

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

py_agile_config-0.4.1.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

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

py_agile_config-0.4.1-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file py_agile_config-0.4.1.tar.gz.

File metadata

  • Download URL: py_agile_config-0.4.1.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for py_agile_config-0.4.1.tar.gz
Algorithm Hash digest
SHA256 1865bec444ba06aa884226979de3b65236dd250d8881bef55cca499ad933b9a5
MD5 a192e0630d009a6b004649a6d109075c
BLAKE2b-256 673d59bd57f713ac2739770d44546dc1aaf81390a47255948b63f9e0e991950f

See more details on using hashes here.

File details

Details for the file py_agile_config-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for py_agile_config-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dd19bc707adce2d193ca935d1080bf18dba27e1bff8b0000a084f388e6809835
MD5 64202c9262dc16573d33bda5fb9aedc3
BLAKE2b-256 4cbe7b3b6ff31589e3eec51f46aa8777879296a823d6cbfc5e0a545e38b1c790

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