Documentation
¶
Overview ¶
Package restkit is a dependency-light HTTP-JSON transport core shared by hand-written API clients (moexoptcalc, alor, tinvest/rest). It owns the request lifecycle, client construction with otelhttp instrumentation, the Values/WithQuery query helpers, and three typed errors (ConfigError, RequestError, ResponseError), all matched with errors.As. Per-client behaviour (auth, header/query pins) is injected through RequestHook.
Index ¶
- Constants
- func Do[T any](ctx context.Context, c *Client, method, path string, body any, ...) (T, error)
- func Pathf(format string, args ...string) string
- type Client
- type ConfigError
- type Option
- type RequestError
- type RequestHook
- type ResponseError
- type Values
- func (v Values) Bool(key string, p *bool) Values
- func (v Values) Float(key string, p *float64) Values
- func (v Values) Int(key string, p *int) Values
- func (v Values) Int32(key string, p *int32) Values
- func (v Values) Int64(key string, p *int64) Values
- func (v Values) Str(key string, p *string) Values
Constants ¶
const ( OpMarshal = "marshal" // encoding the request body to JSON OpBuild = "build" // constructing the *http.Request OpHook = "hook" // a RequestHook returned an error OpSend = "send" // the HTTP round-trip OpRead = "read" // reading the response body OpUnmarshal = "unmarshal" // decoding the 2xx body into T )
Op values name the failed RequestError stage, following the net.OpError / fs.PathError convention (a short verb). Match on these consts, not literals.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
func Do[T any]( ctx context.Context, c *Client, method, path string, body any, hooks ...RequestHook, ) (T, error)
Do issues the request and decodes the 2xx JSON body into T. A non-2xx is a *ResponseError; any other stage failure is a *RequestError naming it.
func Pathf ¶ added in v0.2.0
Pathf builds a request path, percent-escaping each interpolated argument as a single path segment. It is the path-side counterpart to Values (which escapes query parameters): pass a format like "/v1/accounts/%s/orders/%s" with raw parameter values, and a value containing '/', '?', '#', or a space is escaped rather than corrupting the URL (injecting a segment, starting a query string, etc.).
Every arg is interpolated with %s; use fmt.Sprintf directly if you need other verbs. An empty arg yields an empty segment (the server decides how to handle it) — Pathf validates escaping, not presence.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an immutable, concurrency-safe HTTP-JSON transport core. Build with New; issue calls via the package-level Do.
type ConfigError ¶
type ConfigError struct {
Name string // client name from WithName; "restkit" by default
Reason string // e.g. "empty endpoint"
}
ConfigError reports invalid input to New (an empty endpoint). Match with errors.As.
func (*ConfigError) Error ¶
func (e *ConfigError) Error() string
type Option ¶
type Option func(*config)
Option configures a Client at construction time.
func WithHTTPClient ¶
WithHTTPClient overrides the default *http.Client (30s timeout, stdlib transport). A nil client is ignored, leaving the default in place.
func WithHook ¶
func WithHook(h RequestHook) Option
WithHook registers a client-wide RequestHook, run on every call before any per-call hooks. Repeatable; runs in registration order. Use it for auth, header pins, or a query pin.
type RequestError ¶
RequestError reports a per-call failure before a usable result. Like *url.Error: Op names the stage, Err wraps the cause. Match with errors.As; the cause stays reachable via errors.Is (Unwrap).
func (*RequestError) Error ¶
func (e *RequestError) Error() string
func (*RequestError) Unwrap ¶
func (e *RequestError) Unwrap() error
type RequestHook ¶
RequestHook mutates a built request before it is sent. It reaches headers and req.URL (query), covering auth, header pins, and query. Returning an error aborts the call as a *RequestError with Op == OpHook.
func WithQuery ¶
func WithQuery(q url.Values) RequestHook
WithQuery returns a hook that merges q into the request's existing query, so a client-wide pin and a per-call filter compose rather than clobber.
type ResponseError ¶
ResponseError reports a non-2xx HTTP response, carrying the status and raw body verbatim (the error envelope is not decoded). Match with errors.As.
func (*ResponseError) Error ¶
func (e *ResponseError) Error() string
func (*ResponseError) GetStatusCode ¶
func (e *ResponseError) GetStatusCode() int
GetStatusCode lets callers read the status via an interface{ GetStatusCode() int } probe without importing this type.