httpclient

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package httpclient provides HTTP client utilities for CLI tools.

Index

Constants

View Source
const (
	DefaultConnectTimeout = 10 * time.Second
	DefaultRequestTimeout = 60 * time.Second
	DefaultSSETimeout     = 5 * time.Minute
	DefaultMaxRetries     = 3
)

Default timeout values.

View Source
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

func MapHTTPStatusToExitCode(status int) int

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

func MapMemosCodeToExitCode(code int32) int

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

func ParseSSELines(s string) ([]map[string]any, error)

ParseSSELines is a simpler variant that takes a string and returns parsed JSON objects.

func ParseSSEStream

func ParseSSEStream(r io.Reader) ([]map[string]any, error)

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 New

func New(baseURL string, opts ...Option) *Client

New creates a new Client with the given base URL and options.

func (*Client) Delete

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

Delete is a convenience method for DELETE requests.

func (*Client) Do

func (c *Client) Do(ctx context.Context, req *http.Request) (*http.Response, error)

Do executes an HTTP request with retries on 5xx and network errors.

func (*Client) Download

func (c *Client) Download(ctx context.Context, path string, w io.Writer) error

Download streams a response body to w.

func (*Client) Get

func (c *Client) Get(ctx context.Context, path string) (*http.Response, error)

Get is a convenience method for GET requests.

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.

func (*Client) Post

func (c *Client) Post(ctx context.Context, path string, contentType string, body io.Reader) (*http.Response, error)

Post is a convenience method for POST requests.

func (*Client) Put

func (c *Client) Put(ctx context.Context, path string, contentType string, body io.Reader) (*http.Response, error)

Put is a convenience method for PUT requests.

type Option

type Option func(*Client)

Option configures the Client.

func WithAuthHeader

func WithAuthHeader(h string) Option

WithAuthHeader sets the authentication header name.

func WithMaxRetries

func WithMaxRetries(n int) Option

WithMaxRetries sets the maximum number of retries.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the request timeout.

func WithToken

func WithToken(t string) Option

WithToken sets the authentication token.

func WithVerbose

func WithVerbose(v bool) Option

WithVerbose enables verbose logging.

Jump to

Keyboard shortcuts

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