Documentation
¶
Overview ¶
Package api is the SePay API v2 HTTP client.
Index ¶
- Constants
- Variables
- func Backoff(retryAfter string, attempt int) time.Duration
- func DataInto[T any](env *Envelope) (T, error)
- func DebugTransport(base http.RoundTripper) http.RoundTripper
- func HasDotSegment(rawPath string) bool
- func IsNotFound(err error) bool
- func NewIdempotencyKey() (string, error)
- func NoRedirect(*http.Request, []*http.Request) error
- func ReadBody(resp *http.Response) ([]byte, error)
- func UserAgent() string
- func WithRequestID(err error, h http.Header) error
- type Client
- func (c *Client) BaseURL() string
- func (c *Client) Delete(ctx context.Context, path string, opts WriteOptions) (*Envelope, error)
- func (c *Client) Get(ctx context.Context, path string, query url.Values) (*Envelope, error)
- func (c *Client) Post(ctx context.Context, path string, opts WriteOptions) (*Envelope, error)
- func (c *Client) Put(ctx context.Context, path string, opts WriteOptions) (*Envelope, error)
- func (c *Client) Request(ctx context.Context, method, path string, query url.Values, opts WriteOptions) (*Envelope, error)
- type Envelope
- type FlexBool
- type FlexInt
- type FlexNum
- type HTTPError
- type Meta
- type Pagination
- type TransportError
- type WriteOptions
Constants ¶
const APIVersion = "2025-07-01"
APIVersion is the API version pinned on every request.
const MaxResponseBytes = 10 << 20
MaxResponseBytes caps a response body so a hostile host can't OOM the client.
Variables ¶
var ErrDotSegment = errors.New(`refusing to send a request whose path has a "." or ".." segment`)
ErrDotSegment refuses a path with a "." or ".." segment, which the server would resolve elsewhere.
Functions ¶
func Backoff ¶
Backoff returns the 429 wait: Retry-After seconds, else 1,2,4,... by attempt, capped at maxBackoff. Capped in seconds before converting, since a large value overflows int64 nanoseconds.
func DebugTransport ¶
func DebugTransport(base http.RoundTripper) http.RoundTripper
DebugTransport logs each request at debug level. Only header names are logged and query values are masked unless listed in unmaskedParams.
func HasDotSegment ¶
HasDotSegment reports a "." or ".." segment in rawPath, decoded or not; query and fragment are ignored.
func NewIdempotencyKey ¶
NewIdempotencyKey returns a random key for a mutating request. Generate it once per logical operation and reuse it across retries via WriteOptions so a replay does not create a duplicate.
func NoRedirect ¶
NoRedirect refuses redirects so the Bearer token is never resent to another host.
Types ¶
type Client ¶
type Client struct {
MaxRetries int
// Reauth returns a replacement token after a 401 ("" for none). Any verb is replayed:
// the 401 comes from the auth filter before a handler runs.
Reauth func(ctx context.Context, rejected string) string
// Logf, if set, reports progress such as a 429 retry.
Logf func(format string, args ...any)
// contains filtered or unexported fields
}
Client calls the SePay API v2 over HTTP.
type Envelope ¶
type Envelope struct {
Status string `json:"status"`
Message string `json:"message,omitempty"`
Data json.RawMessage `json:"data,omitempty"`
Meta *Meta `json:"meta,omitempty"`
ErrorCode string `json:"error_code,omitempty"`
Errors map[string]string `json:"errors,omitempty"`
}
Envelope is the shared v2 response wrapper.
type FlexBool ¶
type FlexBool bool
FlexBool is a bool that also decodes from 1/0 or a quoted bool or number.
func (*FlexBool) UnmarshalJSON ¶
UnmarshalJSON accepts a bool, a number, either quoted, or null (false).
type FlexInt ¶
type FlexInt int
FlexInt is an int that also decodes from a JSON string; some v2 endpoints send numbers as strings.
func (*FlexInt) UnmarshalJSON ¶
UnmarshalJSON accepts a JSON number, a quoted number, or null (0).
type FlexNum ¶
type FlexNum float64
FlexNum is a float that also decodes from a JSON string. Money uses FlexInt.
func (*FlexNum) UnmarshalJSON ¶
UnmarshalJSON accepts a JSON number, a quoted number, or null (0).
type HTTPError ¶
type HTTPError struct {
StatusCode int
Code string
Message string
RequestID string
// Errors holds 422 field-level messages.
Errors map[string]string
}
HTTPError is a server-returned error, distinct from a network error.
type Meta ¶
type Meta struct {
Pagination Pagination `json:"pagination"`
}
Meta holds list-response metadata.
type Pagination ¶
type Pagination struct {
Total int `json:"total"`
PerPage int `json:"per_page"`
CurrentPage int `json:"current_page"`
LastPage int `json:"last_page"`
HasMore bool `json:"has_more"`
}
Pagination describes a list response's paging state.
type TransportError ¶ added in v0.1.1
type TransportError struct {
Host string
Timeout time.Duration
Err error
// contains filtered or unexported fields
}
TransportError is a request that got no usable answer: the host never replied, or the reply broke off. Unwrap keeps the Go chain (*url.Error, net and TLS errors) reachable for callers that branch on it.
func NewReadError ¶ added in v0.1.1
func NewReadError(host string, err error) *TransportError
NewReadError wraps err from reading the body of an answer host had started to send.
func NewTransportError ¶ added in v0.1.1
func NewTransportError(host string, timeout time.Duration, err error) *TransportError
NewTransportError wraps err from sending a request to host with a client timeout of timeout.
func (*TransportError) Cancelled ¶ added in v0.1.1
func (e *TransportError) Cancelled() bool
Cancelled reports a request stopped by its caller (Ctrl-C), which says nothing about the network.
func (*TransportError) Error ¶ added in v0.1.1
func (e *TransportError) Error() string
func (*TransportError) Reason ¶ added in v0.1.1
func (e *TransportError) Reason() string
Reason names the cause in plain words, without the Go error chain.
func (*TransportError) Unwrap ¶ added in v0.1.1
func (e *TransportError) Unwrap() error
type WriteOptions ¶
type WriteOptions struct {
// Body is JSON-encoded; a json.RawMessage is sent verbatim.
Body any
// IdempotencyKey is sent as X-Idempotency-Key and reused across retries.
IdempotencyKey string
// Header is applied after the standard headers, so it can override them.
Header http.Header
}
WriteOptions configures a mutating request (Post/Put/Delete).