Documentation
¶
Index ¶
- Variables
- type Client
- func (c *Client) Create(payload any) (*Resp, error)
- func (c *Client) CreateMany(payload any) (*Resp, error)
- func (c *Client) Delete(id string) (*Resp, error)
- func (c *Client) DeleteMany(payload any) (*Resp, error)
- func (c *Client) Get(id string, dst any) (*Resp, error)
- func (c *Client) List(items any, total *int64) (*Resp, error)
- func (c *Client) Patch(id string, payload any) (*Resp, error)
- func (c *Client) PatchMany(payload any) (*Resp, error)
- func (c *Client) QueryString() (string, error)
- func (c *Client) RequestURL() (string, error)
- func (c *Client) Stream(payload any, callback StreamCallback) error
- func (c *Client) StreamPrint(payload any) error
- func (c *Client) StreamPrintURL(method, url string, payload any) error
- func (c *Client) StreamURL(method, url string, payload any, callback StreamCallback) error
- func (c *Client) Update(id string, payload any) (*Resp, error)
- func (c *Client) UpdateMany(payload any) (*Resp, error)
- type Option
- func WithAPI(path string) Option
- func WithBaseAuth(username, password string) Option
- func WithContext(ctx context.Context) Option
- func WithDebug() Option
- func WithHTTPClient(client *http.Client) Option
- func WithHeader(header http.Header) Option
- func WithLogger(logger types.Logger) Option
- func WithQuery(_keyValues ...any) Option
- func WithQueryExpand(expand string, depth uint) Option
- func WithQueryFuzzy(fuzzy bool) Option
- func WithQueryIndex(index string) Option
- func WithQueryNocache(nocache bool) Option
- func WithQueryOr(or bool) Option
- func WithQueryPagination(page, size uint) Option
- func WithQuerySelect(selects ...string) Option
- func WithQuerySortby(sortby string) Option
- func WithQueryTimeRange(columeName string, start, end time.Time) Option
- func WithRateLimit(r rate.Limit, b int) Option
- func WithRetry(maxRetries int, wait time.Duration) Option
- func WithTimeout(timeout time.Duration) Option
- func WithToken(token string) Option
- func WithUserAgent(userAgent string) Option
- type Resp
- type StreamCallback
Constants ¶
This section is empty.
Variables ¶
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 ¶
func New ¶
New creates a new client instance with given base URL and options. The base URL must start with "http://" or "https://".
func (*Client) Create ¶
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 ¶
CreateMany send a POST request to batch create multiple resources. payload should be a struct slice, eg: []User or []*User
func (*Client) DeleteMany ¶
DeleteMany send a DELETE request to batch delete multiple resources. payload should be a string slice contains id list.
func (*Client) Get ¶
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 ¶
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) PatchMany ¶
PatchMany send a PATCH request to batch partially update multiple resources. payload should be a struct slice, eg: []User or []*User
func (*Client) QueryString ¶
QueryString build the query string from structured query parameters and raw query string.
func (*Client) RequestURL ¶
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
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
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
})
type Option ¶
type Option func(*Client)
func WithAPI ¶ added in v0.10.5
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 WithContext ¶
func WithHTTPClient ¶
func WithHeader ¶
func WithLogger ¶
func WithQueryExpand ¶
func WithQueryFuzzy ¶
func WithQueryIndex ¶
func WithQueryNocache ¶
func WithQueryOr ¶
func WithQueryPagination ¶
func WithQuerySelect ¶
func WithQuerySortby ¶
func WithTimeout ¶
func WithUserAgent ¶
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
StreamCallback is a function type for handling stream events. It receives an SSE event and returns an error if processing should stop.