简单易用的 GitCode API SKILL,提供 CLI 和轻量级 Python SDK 两种调用方式,用于与 GitCode 的 REST API 进行交互。支持加密访问令牌、异步调用以及完善的类型提示。 - 文档:https://gitcode-api.readthedocs.io/ - PyPI 页面:https://pypi.org/project/gitcode-api - GitHub 仓库:https://github.com/Trenza1ore/GitCode-API
GitCode API SDK. 使用已发布的 Python 包 :. 认证默认为 GITCODE ACCESS TOKEN 环境变量, 或者通过 api ke是一项面向实际任务的技能,主要用于如果值中任意一个被加密, 则通过 de。它将相关步骤、工具调用和结果整理方式集中到统一流程中,帮助使用者更快完成目标并减少重复操作。
实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。
执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。
使用已发布的 Python 包:
pip install -U gitcode-api
认证默认从 GITCODE_ACCESS_TOKEN 环境变量读取;也可显式传入 api_key=...。若该值已被加密,请传入 decrypt=...,使客户端可在认证前对其进行解密。
安装前须向用户确认:
gitcode-api 可能会影响全局运行环境。GITCODE_ACCESS_TOKEN 环境变量(建议加密):decrypt 参数,以便在运行时解密加密后的 api_key 值或加密后的 GITCODE_ACCESS_TOKEN。本 SDK 的设计风格与 OpenAI 的 Python 客户端类似:
GitCode(...) 或 AsyncGitCode(...)。client.repos、client.pulls、client.users 和 client.search。client.repos.get() 或 await client.pulls.list()。methods(按稳定 SDK 顺序、基于路径段排序的公开可调用名称列表)和 method_signature(name)(对单个可调用对象缓存的 inspect.signature 字符串),用于运行时内省;详见 references/api-reference.md 中的“资源内省”章节。with GitCode(...) as client: 或 async with AsyncGitCode(...) as client: 语法,以便 SDK 自动关闭底层 httpx 客户端(包括自定义的 http_client)。与 OpenAI 的强类型请求/响应结构不同,本 SDK 聚焦于 GitCode REST 资源,返回轻量级响应对象,并支持属性访问。
同步方式:
from gitcode_api import GitCode
with GitCode(
api_key="your-token",
owner="SushiNinja",
repo="GitCode-API",
) as client:
repo = client.repos.get()
pulls = client.pulls.list(state="open", per_page=5)
print(repo.full_name)
for pull in pulls:
print(pull.number, pull.title)
异步方式:
import asyncio
from gitcode_api import AsyncGitCode
async def main() -> None:
async with AsyncGitCode(
api_key="your-token",
owner="SushiNinja",
repo="GitCode-API",
) as client:
branches = await client.branches.list(per_page=5)
for branch in branches:
print(branch.name)
asyncio.run(main())
使用加密令牌:
from gitcode_api import GitCode
from trusted_library import decryption_method
with GitCode(
api_key="encrypted-token",
owner="SushiNinja",
repo="GitCode-API",
decrypt=decryption_method,
) as client:
repo = client.repos.get()
pulls = client.pulls.list(state="open", per_page=5)
print(repo.full_name)
for pull in pulls:
print(pull.number, pull.title)
若在客户端初始化时设置了 owner= 和 repo=,则调用仓库相关资源方法时可省略这两项参数;否则,需在各方法调用中显式传入 owner= 和 repo=。
client.repos 和 client.contentsclient.branches 和 client.commitsclient.issues 和 client.pullsclient.labels、client.milestones 和 client.membersclient.releases、client.tags 和 client.webhooksclient.users、client.orgs、client.search 和 client.oauthclient.repos.get()、client.contents.get()、client.contents.create()、client.contents.update()client.branches.list()、client.commits.list()、client.commits.compare()client.issues.list()、client.issues.create()、client.pulls.list()、client.pulls.create()、client.pulls.merge()client.users.me()、client.orgs.list_authenticated()、client.search.repositories()client.oauth.build_authorize_url()、client.oauth.exchange_token()完整方法清单请参阅 references/api-reference.md
响应为轻量级对象,而非原始字典(plain dict)。
典型用法示例:
pull = client.pulls.get(number=42)
print(pull.title)
print(pull.get("source_branch"))
payload = pull.to_dict()
内置辅助脚本:
scripts/check_env.py:用于验证 Python 版本、包导入及令牌配置是否正常。scripts/gitcode_api_cli.py:一个遗留的命令行接口示例(已弃用;使用时会发出警告)。推荐改用包内实验性 CLI(gitcode-api 或 python -m gitcode_api);详见 https://gitcode-api.readthedocs.io/en/latest/sdk/cli.html。生产环境中,请勿将令牌置于命令行参数中,应使用环境变量或自行封装调用逻辑。Q:在公司网络中访问 GitCode 时出现 SSL 错误,提示 “self-signed certificate”。
A:通常需配置自定义 CA 证书包。用户可通过 httpx 客户端的 verify 参数指定证书路径(类似于 requests 库使用的 REQUESTS_CA_BUNDLE 环境变量)。
from gitcode_api import GitCode
from httpx import Client
with GitCode(
owner="SushiNinja",
repo="GitCode-API",
http_client=Client(verify="path/to/my/certificate.crt"),
) as client:
repo = client.repos.get()
pulls = client.pulls.list(state="open", per_page=5)
...