request

package
v0.20260622.2 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do[T any](r *Request) (resp *T, err error)

Do executes the request and decodes the envelope's result field into *T. A non-empty error array is returned as a *client.APIError.

func DoRaw

func DoRaw(r *Request) ([]byte, error)

DoRaw executes the request and returns the raw, undecoded response body.

func DoRawResult

func DoRawResult(r *Request) ([]byte, error)

DoRawResult executes the request and returns the raw JSON bytes of the envelope's "result" field (after verifying the error array is empty). Tests use it to diff the real response shape against the typed structs.

func HMACSign

func HMACSign(secret, uriPath, nonce, postData string) (string, error)

HMACSign is Kraken's default request signer:

API-Sign = base64( HMAC-SHA512( base64decode(secret), uriPath + SHA256(nonce + postData) ) )

where uriPath is the full path (e.g. "/0/private/AddOrder"), postData is the url-encoded POST body (which itself begins with "nonce=..."), and nonce is that same nonce value as a string. The secret is the base64 "Private Key" from the Kraken API-management page and is decoded to raw bytes before use as the HMAC key.

func Subscribe

func Subscribe[T any](ctx context.Context, c WsClient, channel string, params map[string]any, private bool, cb func(*WsPush[T], error)) (done chan<- struct{}, stop <-chan struct{}, err error)

Subscribe opens a dedicated connection to the public or private v2 gateway, subscribes to the channel with the given params (injecting the auth token when private), and invokes cb for every data push (decoded into *WsPush[T]). It returns a done channel (close to stop) and a stop channel (closed when the reader exits).

func SubscribeRaw

func SubscribeRaw(ctx context.Context, c WsClient, channel string, params map[string]any, private bool, cb func(message []byte, err error)) (done chan<- struct{}, stop <-chan struct{}, err error)

SubscribeRaw is like Subscribe but delivers each data frame's raw bytes.

Types

type Client

type Client interface {
	GetHttpClient() *resty.Client
	GetAPIKey() string
	GetAPISecret() string
	GetLogger() log.Logger
	GetSignFn() SignFn
	Nonce() int64
}

Client is what every endpoint Service needs from the Kraken REST client. All getters are read-only; the concrete *client.Client satisfies it.

type Request

type Request struct {
	// contains filtered or unexported fields
}

func Get

func Get(ctx context.Context, c Client, path string, params ...map[string]string) *Request

Get builds a GET request for a public endpoint. Any params maps are merged (empty values dropped) into the query string.

func Post

func Post(ctx context.Context, c Client, path string, params ...map[string]string) *Request

Post builds a POST request, used for both public and (with WithSign) private endpoints. Any params maps are merged into the url-encoded form body.

func (*Request) SetParam

func (r *Request) SetParam(key, value string) *Request

SetParam sets a single request parameter, overriding any existing value. An empty value is ignored (use it to skip optional fields without branching at the call site).

func (*Request) WithSign

func (r *Request) WithSign() *Request

WithSign marks the request as private: a nonce is injected into the body and the API-Key / API-Sign headers are attached at send time. Public market endpoints omit this.

type SignFn

type SignFn = func(secret, uriPath, nonce, postData string) (signature string, err error)

SignFn mirrors client.SignFn: it turns the request components into the API-Sign header value, given the configured (base64-encoded) secret.

type WsClient

type WsClient interface {
	GetPublicURL() string
	GetPrivateURL() string
	GetLogger() log.Logger
	GetDialer() *websocket.Dialer
	Token(ctx context.Context) (string, error)
}

WsClient is what the subscribe/trade framework needs from a *client.WebSocketClient.

type WsError

type WsError struct {
	Message string
}

WsError is a Kraken WebSocket control-frame error.

func (*WsError) Error

func (e *WsError) Error() string

type WsPush

type WsPush[T any] struct {
	Channel string     `json:"channel"`
	Type    WsPushType `json:"type"`
	Data    T          `json:"data"`
}

WsPush is the envelope Kraken pushes for a channel data event. Type is WsPushTypeSnapshot (full) or WsPushTypeUpdate (incremental).

type WsPushType added in v0.20260622.1

type WsPushType string

WsPushType classifies a channel data frame: a full snapshot or an incremental update.

const (
	WsPushTypeSnapshot WsPushType = "snapshot" // full state
	WsPushTypeUpdate   WsPushType = "update"   // incremental change
)

type WsTradeConn

type WsTradeConn struct {
	// contains filtered or unexported fields
}

WsTradeConn is a persistent private connection for placing/managing orders over WebSocket. Unlike the subscription channels it is request/response: each call sends a method frame tagged with a unique req_id and waits for the matching reply. One connection serves any number of concurrent calls. The auth token (fetched at dial) is injected into every request's params.

func DialWsTrade

func DialWsTrade(ctx context.Context, c WsClient) (*WsTradeConn, error)

DialWsTrade opens the private v2 gateway, fetches an auth token, and returns a ready trade connection. Close it when done.

func (*WsTradeConn) Close

func (t *WsTradeConn) Close() error

Close terminates the connection and fails any in-flight calls.

type WsTradeResponse

type WsTradeResponse[T any] struct {
	Method  string `json:"method"`
	ReqID   int64  `json:"req_id"`
	Result  T      `json:"result"`
	Success bool   `json:"success"`
	Error   string `json:"error"`
	TimeIn  string `json:"time_in"`
	TimeOut string `json:"time_out"`
}

WsTradeResponse is the reply to a trade method frame. Result carries the method-specific payload (e.g. the order id).

func WsTradeCall

func WsTradeCall[T any](ctx context.Context, t *WsTradeConn, method string, params map[string]any) (*WsTradeResponse[T], error)

WsTradeCall sends a trade method and decodes the reply, returning a *WsError on a non-success response.

Jump to

Keyboard shortcuts

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