Documentation
¶
Index ¶
- Variables
- type Fetch
- func (f *Fetch) After(hook FetchAfterHook) *Fetch
- func (f *Fetch) Before(hook FetchBeforeHook) *Fetch
- func (f *Fetch) Client(client *http.Client) *Fetch
- func (f *Fetch) Delete(path string) *FetchRequest
- func (f *Fetch) DoDelete(ctx context.Context, path string, out any) (*FetchResult, error)
- func (f *Fetch) DoGet(ctx context.Context, path string, out any) (*FetchResult, error)
- func (f *Fetch) DoPost(ctx context.Context, path string, params any, out any) (*FetchResult, error)
- func (f *Fetch) DoPut(ctx context.Context, path string, params any, out any) (*FetchResult, error)
- func (f *Fetch) Get(path string) *FetchRequest
- func (f *Fetch) Header(key, value string) *Fetch
- func (f *Fetch) Headers(headers map[string]string) *Fetch
- func (f *Fetch) Method(method, path string) *FetchRequest
- func (f *Fetch) Patch(path string) *FetchRequest
- func (f *Fetch) Post(path string) *FetchRequest
- func (f *Fetch) Put(path string) *FetchRequest
- func (f *Fetch) UseCookie(enabled bool) *Fetch
- type FetchAfterHook
- type FetchBeforeHook
- type FetchError
- type FetchRequest
- func (r *FetchRequest) Body(body []byte) *FetchRequest
- func (r *FetchRequest) Do(ctx context.Context, out any) error
- func (r *FetchRequest) Header(key, value string) *FetchRequest
- func (r *FetchRequest) Headers(headers map[string]string) *FetchRequest
- func (r *FetchRequest) JSON(v any) *FetchRequest
- func (r *FetchRequest) Query(key string, value any) *FetchRequest
- func (r *FetchRequest) Result(ctx context.Context, out any) (*FetchResult, error)
- func (r *FetchRequest) String(body string) *FetchRequest
- type FetchResult
Constants ¶
This section is empty.
Variables ¶
var Default = New()
Default is used by the package-level Get/Post/Put/Delete helpers.
Applications that need shared headers, hooks, cookies, or a custom transport can replace or configure this value during setup.
Functions ¶
This section is empty.
Types ¶
type Fetch ¶
type Fetch struct {
// contains filtered or unexported fields
}
Fetch is a reusable HTTP client with a fetch-like request builder.
A Fetch instance owns shared configuration such as base URL, common headers, hooks, cookie behavior, and the underlying *http.Client. Each Get/Post/Method call creates an independent FetchRequest, so per-request headers, query values, and body data do not leak into later requests.
func New ¶
New creates a reusable Fetch client.
The optional baseURL is prepended to relative request paths. Cookie storage is disabled by default; call UseCookie(true) to enable a cookie jar.
func (*Fetch) After ¶
func (f *Fetch) After(hook FetchAfterHook) *Fetch
After appends a response hook that runs after the response body is read.
The hook receives the response metadata and body, and returns the body that should be decoded or returned. Typical uses include decrypting, decompressing, or unwrapping an API envelope.
func (*Fetch) Before ¶
func (f *Fetch) Before(hook FetchBeforeHook) *Fetch
Before appends a request hook that runs after the request is built and before it is sent.
The hook receives the outgoing request and the raw body bytes. Typical uses include hash signatures, auth headers, request tracing, and timestamp headers.
func (*Fetch) Client ¶
Client sets the underlying HTTP client.
Passing nil resets to a default client with a 30 second timeout. If you need cookies, call UseCookie(true) after Client unless your custom client already has the desired Jar.
func (*Fetch) Delete ¶
func (f *Fetch) Delete(path string) *FetchRequest
Delete starts a DELETE request.
func (*Fetch) DoPost ¶
DoPost sends a POST request and returns the response metadata.
The params value is encoded by type: []byte uses a raw body, string uses a string body, nil sends no body, and all other values are JSON encoded.
func (*Fetch) DoPut ¶
DoPut sends a PUT request and returns the response metadata.
The params value is encoded by type: []byte uses a raw body, string uses a string body, nil sends no body, and all other values are JSON encoded.
func (*Fetch) Method ¶
func (f *Fetch) Method(method, path string) *FetchRequest
Method starts a request with an arbitrary HTTP method and path.
func (*Fetch) Patch ¶
func (f *Fetch) Patch(path string) *FetchRequest
Patch starts a PATCH request.
type FetchAfterHook ¶
type FetchError ¶
FetchError is returned when the response status is outside the 2xx range.
Header and Body are still populated so callers can inspect API error details and response headers, for example a refreshed token returned with a 401.
func (*FetchError) Error ¶
func (e *FetchError) Error() string
type FetchRequest ¶
type FetchRequest struct {
// contains filtered or unexported fields
}
FetchRequest is one request built from a reusable Fetch client.
Use request-level Header/Headers for headers that should only apply to this request. Use Fetch.Header/Fetch.Headers for headers that should be sent with every request from the client.
func (*FetchRequest) Body ¶
func (r *FetchRequest) Body(body []byte) *FetchRequest
Body sets the raw request body.
func (*FetchRequest) Do ¶
func (r *FetchRequest) Do(ctx context.Context, out any) error
Do sends the request and decodes the response body into out.
It is the convenience form of Result when callers only need decoded data. Use Result when response metadata is needed, such as reading X-Token from headers.
func (*FetchRequest) Header ¶
func (r *FetchRequest) Header(key, value string) *FetchRequest
Header sets a header for this request only.
Request headers override common headers with the same key.
func (*FetchRequest) Headers ¶
func (r *FetchRequest) Headers(headers map[string]string) *FetchRequest
Headers sets headers for this request only.
func (*FetchRequest) JSON ¶
func (r *FetchRequest) JSON(v any) *FetchRequest
JSON marshals v as the request body and sets Content-Type to application/json.
func (*FetchRequest) Query ¶
func (r *FetchRequest) Query(key string, value any) *FetchRequest
Query sets one query parameter for this request.
func (*FetchRequest) Result ¶
func (r *FetchRequest) Result(ctx context.Context, out any) (*FetchResult, error)
Result sends the request, decodes the response body into out, and returns response metadata.
Supported out values are nil, *[]byte, *string, or a JSON target such as a struct/map pointer. The returned Body is the final body after all After hooks.
func (*FetchRequest) String ¶
func (r *FetchRequest) String(body string) *FetchRequest
String sets the request body from a string.
type FetchResult ¶
FetchResult contains the response metadata and the final response body.
Body is the body after all After hooks have run. Header is cloned from the http.Response so callers can safely read values such as X-Token after Do/Result returns.
func Delete ¶
func Delete(path string, out any) (*FetchResult, error)
Delete sends a DELETE request with Default.
func Get ¶
func Get(path string, out any) (*FetchResult, error)
Get sends a GET request with Default and decodes the response into out.