Documentation
¶
Overview ¶
Package client provides the HTTP client for interacting with Redmine API.
Package client provides the HTTP client for interacting with Redmine API.
Package client provides the HTTP client for interacting with Redmine API.
Package client provides the HTTP client for interacting with Redmine API.
Package client provides the HTTP client for interacting with Redmine API.
Package client provides the HTTP client for interacting with Redmine API.
Package client provides the HTTP client for interacting with Redmine API.
Index ¶
- Constants
- func CollectAll[T any](seq iter.Seq2[T, error]) ([]T, error)
- func Paginate[T any](ctx context.Context, fetcher PageFetcher[T], pageSize int) iter.Seq2[T, error]
- type BatchResult
- type Client
- func (c *Client) BuildPath(path string, params map[string]string) string
- func (c *Client) Delete(ctx context.Context, path string) error
- func (c *Client) Get(ctx context.Context, path string, result any) error
- func (c *Client) GetConnectionPoolConfig() *ConnectionPoolConfig
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) Post(ctx context.Context, path string, body, result any) error
- func (c *Client) Put(ctx context.Context, path string, body, result any) error
- func (c *Client) SetAPIKey(key string)
- func (c *Client) SetBaseURL(newURL string)
- func (c *Client) TestAuth(ctx context.Context) error
- func (c *Client) WithRetry(ctx context.Context, fn RetryableFunc) error
- type ConnectionPoolConfig
- type LoggingTransport
- type Option
- func WithConnectionPool(config *ConnectionPoolConfig) Option
- func WithHTTPClient(httpClient *http.Client) Option
- func WithIdleConnTimeout(timeout time.Duration) Option
- func WithMaxConnsPerHost(maxConnsPerHost int) Option
- func WithMaxIdleConns(maxIdleConns int) Option
- func WithMaxIdleConnsPerHost(maxIdleConnsPerHost int) Option
- func WithRetry(maxRetries int, initialDelay, maxDelay time.Duration) Option
- func WithTimeout(timeout time.Duration) Option
- type PageFetcher
- type PaginatedResponse
- type Pagination
- type RetryConfig
- type RetryableFunc
Constants ¶
const DefaultConcurrency = 5
DefaultConcurrency is the default maximum concurrency for batch operations.
const (
// DefaultPageSize 是默认的每页大小
DefaultPageSize = 100
)
Variables ¶
This section is empty.
Functions ¶
func CollectAll ¶
CollectAll 收集迭代器中的所有数据,返回切片和错误 这是一个便捷函数,用于将迭代器转换为切片
参数:
- seq: 分页迭代器
返回:
- []T: 所有数据项的切片
- error: 第一个遇到的错误,如果没有错误则为 nil
使用示例:
items, err := client.CollectAll(client.Paginate(ctx, fetcher, 100))
if err != nil {
// 处理错误
}
// 使用 items
func Paginate ¶
Paginate 创建一个分页迭代器,使用 Go 1.23 的 iter.Seq2 它会自动处理分页逻辑,支持上下文取消
参数:
- ctx: 上下文,用于取消操作
- fetcher: 获取分页数据的函数
- pageSize: 每页大小,如果为 0 则使用 DefaultPageSize
返回:
- iter.Seq2[T, error]: 一个迭代器,每次迭代返回一个数据项和一个错误 当迭代完成时,错误为 nil;当发生错误时,错误不为 nil
使用示例:
for item, err := range client.Paginate(ctx, fetcher, 100) {
if err != nil {
// 处理错误
break
}
// 处理 item
}
Types ¶
type BatchResult ¶
type BatchResult[T any] struct { Index int // Original index in the request list Result T // Operation result Error error // Error if operation failed, nil otherwise }
BatchResult represents a single result from a batch operation.
func BatchGet ¶
func BatchGet[T any](c *Client, ctx context.Context, paths []string, concurrency int) []BatchResult[T]
BatchGet concurrently executes multiple GET requests and returns results. Uses semaphore pattern to control maximum concurrency.
func BatchGetFunc ¶
func BatchGetFunc[T any, R any](items []T, ctx context.Context, fn func(ctx context.Context, index int, item T) (R, error), concurrency int) []BatchResult[R]
BatchGetFunc executes batch operations with a custom function. Use when more complex request logic is needed.
Parameters:
- items: Input items
- ctx: Context for cancellation
- fn: Processing function that receives index and item, returns result and error
- concurrency: Maximum concurrency, defaults to 5 if <= 0
Returns:
- []BatchResult[R]: Results in the same order as input items
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the HTTP client for Redmine API.
func (*Client) GetConnectionPoolConfig ¶
func (c *Client) GetConnectionPoolConfig() *ConnectionPoolConfig
GetConnectionPoolConfig 从 Client 获取当前的连接池配置 如果 Transport 不是 *http.Transport 类型,返回 nil
func (*Client) SetBaseURL ¶
SetBaseURL sets the base URL for the client.
type ConnectionPoolConfig ¶
type ConnectionPoolConfig struct {
// MaxIdleConns 控制所有主机的最大空闲连接数
// 默认值: 10
MaxIdleConns int
// MaxIdleConnsPerHost 控制每个主机的最大空闲连接数
// 默认值: 5
MaxIdleConnsPerHost int
// MaxConnsPerHost 控制每个主机的最大连接数(包括空闲和活跃)
// 默认值: 10
MaxConnsPerHost int
// IdleConnTimeout 是空闲连接在关闭前保持打开的最长时间
// 默认值: 30 * time.Second
IdleConnTimeout time.Duration
}
ConnectionPoolConfig 定义 HTTP 连接池配置
func DefaultConnectionPoolConfig ¶
func DefaultConnectionPoolConfig() *ConnectionPoolConfig
DefaultConnectionPoolConfig 返回默认的连接池配置
type LoggingTransport ¶
type LoggingTransport struct {
// contains filtered or unexported fields
}
LoggingTransport is an http.RoundTripper that logs requests and responses
func NewLoggingTransport ¶
func NewLoggingTransport(transport http.RoundTripper, logger io.Writer, verbose bool) *LoggingTransport
NewLoggingTransport creates a new LoggingTransport
func (*LoggingTransport) SetVerbose ¶
func (t *LoggingTransport) SetVerbose(verbose bool)
SetVerbose sets the verbose mode
type Option ¶
type Option func(*Client)
Option is a functional option for configuring the Client.
func WithConnectionPool ¶
func WithConnectionPool(config *ConnectionPoolConfig) Option
WithConnectionPool 返回一个配置连接池的选项函数 如果传入 nil,则使用默认配置
func WithHTTPClient ¶
WithHTTPClient sets a custom HTTP client.
func WithIdleConnTimeout ¶
WithIdleConnTimeout 返回一个配置空闲连接超时的选项函数
func WithMaxConnsPerHost ¶
WithMaxConnsPerHost 返回一个配置每个主机最大连接数的选项函数
func WithMaxIdleConns ¶
WithMaxIdleConns 返回一个配置最大空闲连接数的选项函数
func WithMaxIdleConnsPerHost ¶
WithMaxIdleConnsPerHost 返回一个配置每个主机最大空闲连接数的选项函数
func WithTimeout ¶
WithTimeout sets the request timeout.
type PageFetcher ¶
type PageFetcher[T any] func(ctx context.Context, offset, limit int) (items []T, total int, err error)
PageFetcher 是一个泛型函数类型,用于获取指定偏移量和限制的数据 返回值:items 是当前页的数据项,total 是总数量,err 是错误信息
type PaginatedResponse ¶
type PaginatedResponse struct {
TotalCount int `json:"total_count"`
Limit int `json:"limit"`
Offset int `json:"offset"`
}
PaginatedResponse is the base response for paginated results.
type Pagination ¶
Pagination represents pagination parameters.
type RetryConfig ¶
RetryConfig configures retry behavior.
type RetryableFunc ¶
type RetryableFunc func() error
RetryableFunc is a function that can be retried.