fetch

package
v3.1.18 Latest Latest
Warning

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

Go to latest
Published: May 16, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

func New(baseURL ...string) *Fetch

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 NewFetch

func NewFetch(baseURL ...string) *Fetch

NewFetch is an alias of New kept for callers who prefer the explicit name.

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

func (f *Fetch) Client(client *http.Client) *Fetch

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) DoDelete

func (f *Fetch) DoDelete(ctx context.Context, path string, out any) (*FetchResult, error)

DoDelete sends a DELETE request and returns the response metadata.

func (*Fetch) DoGet

func (f *Fetch) DoGet(ctx context.Context, path string, out any) (*FetchResult, error)

DoGet sends a GET request and returns the response metadata.

func (*Fetch) DoPost

func (f *Fetch) DoPost(ctx context.Context, path string, params any, out any) (*FetchResult, error)

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

func (f *Fetch) DoPut(ctx context.Context, path string, params any, out any) (*FetchResult, error)

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) Get

func (f *Fetch) Get(path string) *FetchRequest

Get starts a GET request.

func (*Fetch) Header

func (f *Fetch) Header(key, value string) *Fetch

Header sets a common header sent with every request from this Fetch client.

func (*Fetch) Headers

func (f *Fetch) Headers(headers map[string]string) *Fetch

Headers sets common headers sent with every request from this Fetch client.

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.

func (*Fetch) Post

func (f *Fetch) Post(path string) *FetchRequest

Post starts a POST request.

func (*Fetch) Put

func (f *Fetch) Put(path string) *FetchRequest

Put starts a PUT request.

func (*Fetch) UseCookie

func (f *Fetch) UseCookie(enabled bool) *Fetch

UseCookie enables or disables cookie persistence for this Fetch client.

Enabling creates an in-memory cookie jar when the current client has none. Disabling clears the client's Jar so later requests do not send stored cookies.

type FetchAfterHook

type FetchAfterHook func(context.Context, *http.Response, []byte) ([]byte, error)

type FetchBeforeHook

type FetchBeforeHook func(context.Context, *http.Request, []byte) error

type FetchError

type FetchError struct {
	StatusCode int
	Status     string
	Header     http.Header
	Body       []byte
}

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

type FetchResult struct {
	StatusCode int
	Status     string
	Header     http.Header
	Body       []byte
}

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 Del

func Del(path string, out any) (*FetchResult, error)

Del is an alias of Delete.

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.

func Post

func Post(path string, params any, out any) (*FetchResult, error)

Post sends a POST request with Default.

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 Put

func Put(path string, params any, out any) (*FetchResult, error)

Put sends a PUT request with Default.

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.

Jump to

Keyboard shortcuts

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