client

package
v0.10.9 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotStringSlice = errors.New("payload must be a string slice")
	ErrNotStructSlice = errors.New("payload must be a struct slice")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	types.Logger
	// contains filtered or unexported fields
}

func New

func New(addr string, opts ...Option) (*Client, error)

New creates a new client instance with given base URL and options. The base URL must start with "http://" or "https://".

func (*Client) Create

func (c *Client) Create(payload any) (*Resp, error)

Create send a POST request to create a new resource. payload can be []byte or struct/pointer that can be marshaled to JSON.

func (*Client) CreateMany

func (c *Client) CreateMany(payload any) (*Resp, error)

CreateMany send a POST request to batch create multiple resources. payload should be a struct slice, eg: []User or []*User

func (*Client) Delete

func (c *Client) Delete(id string) (*Resp, error)

Delete send a DELETE request to delete a resource.

func (*Client) DeleteMany

func (c *Client) DeleteMany(payload any) (*Resp, error)

DeleteMany send a DELETE request to batch delete multiple resources. payload should be a string slice contains id list.

func (*Client) Get

func (c *Client) Get(id string, dst any) (*Resp, error)

Get send a GET request to get one resource by given id. The id parameter specifies which resource to retrieve. The dst parameter must be a pointer to struct where the resource will be unmarshaled into.

func (*Client) List

func (c *Client) List(items any, total *int64) (*Resp, error)

List send a GET request to retrieve a list of resources. items must be a pointer to slice where items will be unmarshaled into. total will be set to the total number of items available.

func (*Client) Patch

func (c *Client) Patch(id string, payload any) (*Resp, error)

Patch send a PATCH request to partially update a resource.

func (*Client) PatchMany

func (c *Client) PatchMany(payload any) (*Resp, error)

PatchMany send a PATCH request to batch partially update multiple resources. payload should be a struct slice, eg: []User or []*User

func (*Client) QueryString

func (c *Client) QueryString() (string, error)

QueryString build the query string from structured query parameters and raw query string.

func (*Client) RequestURL

func (c *Client) RequestURL() (string, error)

RequestURL constructs the full request URL including base URL and query parameters.

func (*Client) Stream added in v0.10.5

func (c *Client) Stream(payload any, callback StreamCallback) error

Stream sends a POST request and processes the response as a Server-Sent Events (SSE) stream. The callback function will be called for each event received, allowing immediate processing.

Parameters:

  • payload: The request payload (can be []byte or struct/pointer that can be marshaled to JSON)
  • callback: Function to handle each SSE event. Return an error to stop streaming.

Example:

err := client.Stream(payload, func(event types.Event) error {
    fmt.Printf("Event: %s, Data: %v\n", event.Event, event.Data)
    return nil // Continue streaming
})

func (*Client) StreamPrint added in v0.10.5

func (c *Client) StreamPrint(payload any) error

StreamPrint sends a POST request and prints each SSE event as it arrives. This is a convenience method that automatically prints events to stdout. JSON data will be pretty-printed if possible.

Parameters:

  • payload: The request payload (can be []byte or struct/pointer that can be marshaled to JSON)

Example:

err := client.StreamPrint(payload)

func (*Client) StreamPrintURL added in v0.10.5

func (c *Client) StreamPrintURL(method, url string, payload any) error

StreamPrintURL sends a request to a custom URL and prints each SSE event as it arrives. This is a convenience method that automatically prints events to stdout.

Parameters:

  • method: HTTP method (e.g., "GET", "POST")
  • url: Full URL or path relative to base address (empty string uses base address)
  • payload: The request payload (can be []byte or struct/pointer that can be marshaled to JSON, or nil)

Example:

err := client.StreamPrintURL("POST", "/api/chat/stream", payload)

func (*Client) StreamURL added in v0.10.5

func (c *Client) StreamURL(method, url string, payload any, callback StreamCallback) error

StreamURL sends a request to a custom URL and processes the response as a Server-Sent Events (SSE) stream. This allows streaming from any endpoint, not just the base address. If url is empty, it uses the base address.

Parameters:

  • method: HTTP method (e.g., "GET", "POST")
  • url: Full URL or path relative to base address (empty string uses base address)
  • payload: The request payload (can be []byte or struct/pointer that can be marshaled to JSON, or nil)
  • callback: Function to handle each SSE event. Return an error to stop streaming.

Example:

err := client.StreamURL("POST", "/api/chat/stream", payload, func(event types.Event) error {
    fmt.Printf("Event: %s, Data: %v\n", event.Event, event.Data)
    return nil
})

func (*Client) Update

func (c *Client) Update(id string, payload any) (*Resp, error)

Update send a PUT request to fully update a resource.

func (*Client) UpdateMany

func (c *Client) UpdateMany(payload any) (*Resp, error)

UpdateMany send a PUT request to batch update multiple resources. payload should be a struct slice, eg: []User or []*User

type Option

type Option func(*Client)

func WithAPI added in v0.10.5

func WithAPI(path string) Option

WithAPI sets a custom API path for the client. The path will be appended to the base address when making requests. Leading and trailing slashes are automatically handled.

func WithBaseAuth

func WithBaseAuth(username, password string) Option

func WithContext

func WithContext(ctx context.Context) Option

func WithDebug

func WithDebug() Option

func WithHTTPClient

func WithHTTPClient(client *http.Client) Option

func WithHeader

func WithHeader(header http.Header) Option

func WithLogger

func WithLogger(logger types.Logger) Option

func WithQuery

func WithQuery(_keyValues ...any) Option

func WithQueryExpand

func WithQueryExpand(expand string, depth uint) Option

func WithQueryFuzzy

func WithQueryFuzzy(fuzzy bool) Option

func WithQueryIndex

func WithQueryIndex(index string) Option

func WithQueryNocache

func WithQueryNocache(nocache bool) Option

func WithQueryOr

func WithQueryOr(or bool) Option

func WithQueryPagination

func WithQueryPagination(page, size uint) Option

func WithQuerySelect

func WithQuerySelect(selects ...string) Option

func WithQuerySortby

func WithQuerySortby(sortby string) Option

func WithQueryTimeRange

func WithQueryTimeRange(columeName string, start, end time.Time) Option

func WithRateLimit

func WithRateLimit(r rate.Limit, b int) Option

func WithRetry

func WithRetry(maxRetries int, wait time.Duration) Option

func WithTimeout

func WithTimeout(timeout time.Duration) Option

func WithToken

func WithToken(token string) Option

func WithUserAgent

func WithUserAgent(userAgent string) Option

type Resp

type Resp struct {
	Code      int             `json:"code"`
	Msg       string          `json:"msg"`
	Data      json.RawMessage `json:"data"`
	RequestID string          `json:"request_id"`
}

type StreamCallback added in v0.10.5

type StreamCallback func(event types.Event) error

StreamCallback is a function type for handling stream events. It receives an SSE event and returns an error if processing should stop.

Jump to

Keyboard shortcuts

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