Help us shape the future of Go! Take the 2026 Go Developer Survey

githubapi

package
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package githubapi 封装 GitHub REST API 的轻量匿名客户端。

仅支持未认证调用(受 60 req/h 限制),调用方应自行缓存结果。 错误以语义类别(Kind)暴露,便于上层针对网络失败、rate limit、404 等 给出不同的用户提示。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client 是匿名调用的 GitHub API 客户端。

受未认证 60 req/h 限制,调用方应缓存结果避免频繁请求。 零值不可用,必须通过 NewClient 创建。

func NewClient

func NewClient() *Client

NewClient 创建一个使用默认配置(5s 超时、Goink User-Agent)的客户端。

func (*Client) GetRawContent

func (c *Client) GetRawContent(ctx context.Context, owner, repo, branch, path string) ([]byte, *RateLimit, error)

GetRawContent 通过 GitHub Contents API 拉取指定文件的原始内容。

使用 Accept: application/vnd.github.raw+json header 直接返回 raw 内容 (而非默认的 base64 编码 JSON 包装),便于直接使用。

成功返回内容字节、rate limit 信息和 nil error。 失败返回 nil、响应中已知的 rate limit 信息(可能为 nil)和 *Error。

type Error

type Error struct {
	Kind    Kind
	Status  int       // HTTP 状态码,网络层错误为 0
	Message string    // 人类可读的错误描述
	ResetAt time.Time // rate limit 重置时间,仅 Kind == KindRateLimited 时有效
	Cause   error     // 底层错误(网络层错误时非 nil)
}

Error 是 GitHub API 调用返回的语义化错误。 调用方可用 errors.As 提取 *Error 后根据 Kind 分支处理。

func (*Error) Code

func (e *Error) Code() apperr.Code

Code 实现 apperr.Coder 接口,按 Kind 映射到 apperr.Code。 映射逻辑从 apperr.codeFromGitHubAPIError 搬来(v1.3.0 集中映射改接口模式后, apperr 不再 import githubapi,改由各领域 error 自行实现 Code())。

func (*Error) Error

func (e *Error) Error() string

Error 实现 error 接口。

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap 暴露底层错误,支持 errors.Is/errors.As。

type Kind

type Kind int

Kind 表示 GitHub API 错误的语义类别。

const (
	KindNetwork     Kind = iota // 网络层错误(超时、连接拒绝、DNS 失败)
	KindRateLimited             // rate limit 触发:403 + X-RateLimit-Remaining:0,或 429
	KindNotFound                // 404
	KindForbidden               // 403 非 rate limit(仓库私有、被屏蔽等)
	KindOther                   // 其他 4xx/5xx
)

func (Kind) String

func (k Kind) String() string

String 返回 Kind 的可读名称,便于日志和调试。

type RateLimit

type RateLimit struct {
	Remaining int       // 剩余请求数
	ResetAt   time.Time // 配额重置时间(UTC)
}

RateLimit 携带本次请求后的 rate limit 配额信息。 GitHub 在每个 API 响应的 header 中返回这些字段。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL