Documentation
¶
Index ¶
- func NewDefaultHTTPClient() (*http.Client, error)
- type Blackbox
- type Client
- type ClientOption
- type ClientOptions
- type DefaultTool
- func (d *DefaultTool) CopyHeaders(ctx context.Context, dst, src *http.Request, headers ...string) error
- func (d *DefaultTool) DecodeBody(ctx context.Context, r *http.Request, v any) error
- func (d *DefaultTool) Do(ctx context.Context, r *http.Request) (*http.Response, error)
- func (d *DefaultTool) DoWithTimeout(ctx context.Context, r *http.Request, t time.Duration) (*http.Response, error)
- func (d *DefaultTool) GetHeaders(ctx context.Context, r *http.Request) (http.Header, error)
- func (d *DefaultTool) NewRequest(ctx context.Context, method, url string, opts contract.RequestOptions) (*http.Request, error)
- func (d *DefaultTool) OpenFile(ctx context.Context, fsys http.FileSystem, name string) (http.File, error)
- func (d *DefaultTool) ParseForm(ctx context.Context, r *http.Request) error
- func (d *DefaultTool) QueryString(ctx context.Context, pairs ...string) (string, error)
- func (d *DefaultTool) RespondJSON(ctx context.Context, w http.ResponseWriter, v any, status int, escapeHTML bool) error
- func (d *DefaultTool) ServeFile(ctx context.Context, opts contract.ServeFileOptions) error
- func (d *DefaultTool) ServeHTML(ctx context.Context, w http.ResponseWriter, body io.Reader, status int) error
- func (d *DefaultTool) SetQueryParams(ctx context.Context, r *http.Request, q map[string][]string, override bool) error
- func (d *DefaultTool) ValidateQueryParams(ctx context.Context, q map[string][]string, rules ...contract.QueryParamRule) error
- func (d *DefaultTool) Write(ctx context.Context, w http.ResponseWriter, b []byte) (int, error)
- type Dependencies
- type Tool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewDefaultHTTPClient ¶ added in v0.1.559
NewDefaultHTTPClient returns a properly configured *http.Client with permissive timeouts for large file uploads and long-lived connections (e.g., WebSocket handshakes). Timeouts are controlled by the provided context in the calling code.
Types ¶
type Blackbox ¶
type Blackbox interface {
contract.HTTPRequester
contract.HTTPWriter
contract.HTTPFileOpener
contract.HTTPFormParser
}
type Client ¶
type Client interface {
Blackbox
contract.HTTPOperator
}
func NewClient ¶
func NewClient(d Dependencies, opts ClientOptions) (Client, error)
type ClientOption ¶ added in v0.1.14
type ClientOption = contract.Opt[ClientOptions]
func WithServeFileFS ¶ added in v0.1.896
func WithServeFileFS(x fs.FS) ClientOption
func WithTimeout ¶ added in v0.1.14
func WithTimeout(x time.Duration) ClientOption
type ClientOptions ¶ added in v0.1.14
func ApplyClientOptions ¶ added in v0.1.14
func ApplyClientOptions(opts *ClientOptions, modifiers ...ClientOption) ClientOptions
type DefaultTool ¶ added in v0.1.11
func NewTool ¶
func NewTool(c Client, opts contract.ToolOptions[Client]) *DefaultTool
func (*DefaultTool) CopyHeaders ¶ added in v0.1.11
func (d *DefaultTool) CopyHeaders(ctx context.Context, dst, src *http.Request, headers ...string) error
CopyHeaders copies the specified headers from the source request to the destination request. If no headers are specified, all headers from the source request will be copied to the destination request.
func (*DefaultTool) DecodeBody ¶ added in v0.1.11
DecodeBody reads and unmarshals the request body into the provided variable. It ensures the content type is JSON before attempting to decode.
func (*DefaultTool) DoWithTimeout ¶ added in v0.1.552
func (d *DefaultTool) DoWithTimeout(ctx context.Context, r *http.Request, t time.Duration) (*http.Response, error)
DoWithTimeout sends an HTTP request and returns an HTTP response bypassing the default timeout.
func (*DefaultTool) GetHeaders ¶ added in v0.1.11
GetHeaders extracts and returns all headers from an HTTP request as an http.Header map.
func (*DefaultTool) NewRequest ¶ added in v0.1.11
func (d *DefaultTool) NewRequest(ctx context.Context, method, url string, opts contract.RequestOptions) (*http.Request, error)
NewRequest creates a new HTTP request with the specified method, URL, and options.
func (*DefaultTool) OpenFile ¶ added in v0.1.377
func (d *DefaultTool) OpenFile(ctx context.Context, fsys http.FileSystem, name string) (http.File, error)
OpenFile attempts to open a file from the provided http.FileSystem by name.
func (*DefaultTool) ParseForm ¶ added in v0.1.796
ParseForm parses the form data from the request body.
func (*DefaultTool) QueryString ¶ added in v0.1.11
QueryString constructs a query parameter string from a variadic number of key-value pairs. The values passed should not already be URL-escaped, as the function will handle escaping.
func (*DefaultTool) RespondJSON ¶ added in v0.1.11
func (d *DefaultTool) RespondJSON(ctx context.Context, w http.ResponseWriter, v any, status int, escapeHTML bool) error
RespondJSON writes the JSON data to the response writer with appropriate headers. The Content-Type header is set to application/json. The Content-Length header is set with the length of the uncompressed data. The Content-Length is not set if the Content-Encoding header is set to gzip. The status code is set based on the provided code parameter.
func (*DefaultTool) ServeFile ¶ added in v0.1.377
func (d *DefaultTool) ServeFile(ctx context.Context, opts contract.ServeFileOptions) error
ServeFile serves a static file from the given file system.
func (*DefaultTool) ServeHTML ¶ added in v0.1.11
func (d *DefaultTool) ServeHTML(ctx context.Context, w http.ResponseWriter, body io.Reader, status int) error
ServeHTML writes an HTML response to the provided http.ResponseWriter with the given HTML content and HTTP status code.
func (*DefaultTool) SetQueryParams ¶ added in v0.1.326
func (d *DefaultTool) SetQueryParams(ctx context.Context, r *http.Request, q map[string][]string, override bool) error
SetQueryParams updates or appends query parameters to the request URL based on the override flag.
func (*DefaultTool) ValidateQueryParams ¶ added in v0.1.11
func (d *DefaultTool) ValidateQueryParams(ctx context.Context, q map[string][]string, rules ...contract.QueryParamRule) error
ValidateQueryParams is a function that validates the presence and values of mandatory query parameters in a given request. It takes a map of query parameters and a variadic slice of QueryParam pointers as arguments. The function iterates over each QueryParam in the mandatoryQueryParams slice and checks if its key is present in the queryParams map. If the key is not present and the Optional field of the QueryParam is set to false, an error is returned indicating that the query parameter is mandatory. If the key is not present but the Optional field of the QueryParam is set to true, the function skips the checks and continues with the next query parameter. For each query parameter that is present, the function checks if its values are valid according to the following rules:
- The value cannot be an empty string.
- The value must be one of the accepted values if the AcceptedValues field of the QueryParam is not nil and has at least one element.
- The value cannot be a duplicate of a previously seen value for the same key.
- If the MultipleValuesAllowed field of the QueryParam is set to false, the query parameter cannot have more than one value.
If any of these checks fail, an error is returned with a message indicating the nature of the failure. If all checks pass, the function returns nil, indicating that all mandatory query parameters are present and their values are valid.
func (*DefaultTool) Write ¶ added in v0.1.377
func (d *DefaultTool) Write(ctx context.Context, w http.ResponseWriter, b []byte) (int, error)
Write writes the given byte slice to the http.ResponseWriter.
type Dependencies ¶ added in v0.1.8
type Dependencies struct {
C contract.HTTPClient
G contract.GzipUtil
GW contract.GzipWriter
GR contract.GzipReader
M contract.Marshaler
U contract.Unmarshaler
F contract.FSysOperator
R contract.Reader
}