client

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2026 License: MIT Imports: 13 Imported by: 0

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

View Source
const DefaultConcurrency = 5

DefaultConcurrency is the default maximum concurrency for batch operations.

View Source
const (
	// DefaultPageSize 是默认的每页大小
	DefaultPageSize = 100
)

Variables

This section is empty.

Functions

func CollectAll

func CollectAll[T any](seq iter.Seq2[T, error]) ([]T, error)

CollectAll 收集迭代器中的所有数据,返回切片和错误 这是一个便捷函数,用于将迭代器转换为切片

参数:

  • seq: 分页迭代器

返回:

  • []T: 所有数据项的切片
  • error: 第一个遇到的错误,如果没有错误则为 nil

使用示例:

items, err := client.CollectAll(client.Paginate(ctx, fetcher, 100))
if err != nil {
    // 处理错误
}
// 使用 items

func Paginate

func Paginate[T any](ctx context.Context, fetcher PageFetcher[T], pageSize int) iter.Seq2[T, error]

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 NewClient

func NewClient(baseURL, apiKey string, opts ...Option) *Client

NewClient creates a new Redmine API client.

func (*Client) BuildPath

func (c *Client) BuildPath(path string, params map[string]string) string

BuildPath builds a URL path with query parameters.

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, path string) error

Delete makes a DELETE request to the specified path.

func (*Client) Get

func (c *Client) Get(ctx context.Context, path string, result any) error

Get makes a GET request to the specified path.

func (*Client) GetConnectionPoolConfig

func (c *Client) GetConnectionPoolConfig() *ConnectionPoolConfig

GetConnectionPoolConfig 从 Client 获取当前的连接池配置 如果 Transport 不是 *http.Transport 类型,返回 nil

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping tests the connectivity to the given URL using HTTP HEAD request.

func (*Client) Post

func (c *Client) Post(ctx context.Context, path string, body, result any) error

Post makes a POST request to the specified path.

func (*Client) Put

func (c *Client) Put(ctx context.Context, path string, body, result any) error

Put makes a PUT request to the specified path.

func (*Client) SetAPIKey

func (c *Client) SetAPIKey(key string)

SetAPIKey sets the API key for the client.

func (*Client) SetBaseURL

func (c *Client) SetBaseURL(newURL string)

SetBaseURL sets the base URL for the client.

func (*Client) TestAuth

func (c *Client) TestAuth(ctx context.Context) error

TestAuth tests the authentication by making a request to the current user endpoint.

func (*Client) WithRetry

func (c *Client) WithRetry(ctx context.Context, fn RetryableFunc) error

WithRetry executes the given function with retry logic.

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) RoundTrip

func (t *LoggingTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper

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

func WithHTTPClient(httpClient *http.Client) Option

WithHTTPClient sets a custom HTTP client.

func WithIdleConnTimeout

func WithIdleConnTimeout(timeout time.Duration) Option

WithIdleConnTimeout 返回一个配置空闲连接超时的选项函数

func WithMaxConnsPerHost

func WithMaxConnsPerHost(maxConnsPerHost int) Option

WithMaxConnsPerHost 返回一个配置每个主机最大连接数的选项函数

func WithMaxIdleConns

func WithMaxIdleConns(maxIdleConns int) Option

WithMaxIdleConns 返回一个配置最大空闲连接数的选项函数

func WithMaxIdleConnsPerHost

func WithMaxIdleConnsPerHost(maxIdleConnsPerHost int) Option

WithMaxIdleConnsPerHost 返回一个配置每个主机最大空闲连接数的选项函数

func WithRetry

func WithRetry(maxRetries int, initialDelay, maxDelay time.Duration) Option

WithRetry configures retry behavior.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

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

type Pagination struct {
	Limit  int `url:"limit,omitempty"`
	Offset int `url:"offset,omitempty"`
}

Pagination represents pagination parameters.

type RetryConfig

type RetryConfig struct {
	MaxRetries   int
	InitialDelay time.Duration
	MaxDelay     time.Duration
}

RetryConfig configures retry behavior.

type RetryableFunc

type RetryableFunc func() error

RetryableFunc is a function that can be retried.

Jump to

Keyboard shortcuts

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