Documentation
¶
Overview ¶
Package httpclient provides HTTP client utilities for CLI tools.
Index ¶
- Constants
- func MapHTTPStatusToExitCode(status int) int
- func MapMemosCodeToExitCode(code int32) int
- func ParseSSELines(s string) ([]map[string]any, error)
- func ParseSSEStream(r io.Reader) ([]map[string]any, error)
- type Client
- func (c *Client) Delete(ctx context.Context, path string) (*http.Response, error)
- func (c *Client) Do(ctx context.Context, req *http.Request) (*http.Response, error)
- func (c *Client) Download(ctx context.Context, path string, w io.Writer) error
- func (c *Client) Get(ctx context.Context, path string) (*http.Response, error)
- func (c *Client) Patch(ctx context.Context, path string, contentType string, body io.Reader) (*http.Response, error)
- func (c *Client) Post(ctx context.Context, path string, contentType string, body io.Reader) (*http.Response, error)
- func (c *Client) Put(ctx context.Context, path string, contentType string, body io.Reader) (*http.Response, error)
- type Option
Constants ¶
const ( DefaultConnectTimeout = 10 * time.Second DefaultRequestTimeout = 60 * time.Second DefaultSSETimeout = 5 * time.Minute DefaultMaxRetries = 3 )
Default timeout values.
const ( ExitSuccess = 0 ExitClientError = 1 ExitServerError = 2 ExitNetwork = 3 ExitConfigError = 4 )
Exit codes for different error types.
Variables ¶
This section is empty.
Functions ¶
func MapHTTPStatusToExitCode ¶
MapHTTPStatusToExitCode maps HTTP status codes to CLI exit codes.
FileBrowser mapping:
- 2xx (200, 201, 204): ExitSuccess (0)
- 401, 403, 404, 409: ExitClientError (1)
- 5xx (500, 502, 503, 504): ExitServerError (2)
- Other 4xx: ExitClientError (1)
func MapMemosCodeToExitCode ¶
MapMemosCodeToExitCode maps Memos gRPC-style error codes to CLI exit codes.
Memos mapping:
- 0 (OK): ExitSuccess (0)
- 3 (INVALID_ARGUMENT): ExitClientError (1)
- 5 (NOT_FOUND): ExitClientError (1)
- 7 (UNAUTHENTICATED): ExitClientError (1)
- 16 (UNAUTHENTICATED): ExitClientError (1)
- Other: ExitServerError (2)
func ParseSSELines ¶
ParseSSELines is a simpler variant that takes a string and returns parsed JSON objects.
func ParseSSEStream ¶
ParseSSEStream reads SSE-formatted data lines and aggregates them into a slice.
SSE format:
data: {"key": "value"}
data: {"key": "value2"}
Only "data:" lines are parsed. Other lines (event:, id:, comments) are ignored. Empty data lines are skipped.
Types ¶
type Client ¶
type Client struct {
// BaseURL is the base URL for all requests.
BaseURL string
// HTTP is the underlying HTTP client.
HTTP *http.Client
// Token is the authentication token (optional).
Token string
// Verbose enables verbose logging to stderr.
Verbose bool
// MaxRetries is the maximum number of retries for 5xx and network errors.
MaxRetries int
// AuthHeader is the header name for authentication (default: "X-Auth" for FileBrowser).
AuthHeader string
}
Client is an HTTP client with retry logic and token support.
func (*Client) Patch ¶
func (c *Client) Patch(ctx context.Context, path string, contentType string, body io.Reader) (*http.Response, error)
Patch is a convenience method for PATCH requests.
type Option ¶
type Option func(*Client)
Option configures the Client.
func WithAuthHeader ¶
WithAuthHeader sets the authentication header name.
func WithMaxRetries ¶
WithMaxRetries sets the maximum number of retries.