request

package
v0.20260804.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DialPrivateLoggedIn added in v0.20260618.1

func DialPrivateLoggedIn(ctx context.Context, client WsClient) (*websocket.Conn, error)

DialPrivateLoggedIn dials the private WebSocket gateway and completes the login handshake, returning a ready connection. WebSocket order-entry (op:"trade") connections build on this. The caller owns and must Close the returned connection.

func Do

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

Do executes the request and decodes the envelope's data field into *T. A non-"00000" code 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. Use it for the rare endpoints whose payload shape is non-uniform.

func DoRawData

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

DoRawData executes the request and returns the raw JSON bytes of the envelope's "data" field (after verifying the success code). Tests use it to diff the real response shape against the typed structs.

func HMACSign

func HMACSign(secret, prehash string) (string, error)

HMACSign is Bitget's default request signer:

ACCESS-SIGN = base64( HMAC-SHA256( secretKey, prehash ) )

where prehash = timestamp + method + requestPath + body (see Request.prehash).

func Subscribe

func Subscribe[T any](ctx context.Context, client WsClient, private bool, arg WsArg, cb func(*WsPush[T], error)) (done chan<- struct{}, stop <-chan struct{}, err error)

Subscribe opens a dedicated connection to the public or private gateway, logs in when private, subscribes to arg, and invokes cb for every data push. Returns a done channel (close to stop) and a stop channel (closed when the reader exits). The typed Data field of the push is decoded into *T.

func SubscribeRaw

func SubscribeRaw(ctx context.Context, client WsClient, private bool, arg WsArg, 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, for channels whose payload shape the caller wants to decode itself.

func SubscribeRawArg added in v0.20260618.1

func SubscribeRawArg(ctx context.Context, client WsClient, private bool, arg any, cb func(message []byte, err error)) (done chan<- struct{}, stop <-chan struct{}, err error)

SubscribeRawArg is like SubscribeRaw but accepts an arbitrary subscription arg value (any JSON-serializable shape), not just the v3 WsArg. The classic v2 streams use a different arg shape ({instType, channel, instId}), so they pass their own arg type here while reusing the shared connection/login/keepalive machinery.

Types

type Client

type Client interface {
	GetHttpClient() *resty.Client
	GetAPIKey() string
	GetAPISecret() string
	GetPassphrase() string
	GetLocale() string
	IsDemoTrading() bool
	GetLogger() log.Logger
	GetSignFn() SignFn
	TimestampMs() int64
}

Client is what every endpoint Service needs from a UTA/classic 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. Any params maps are merged and become the (sorted) query string, which is also part of the signed prehash.

func Post

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

Post builds a POST request. Any body maps are merged and JSON-encoded once; the exact bytes sent are the bytes signed.

func (*Request) SetBody

func (r *Request) SetBody(body any) *Request

SetBody overrides the request body with an arbitrary JSON-serializable value (used for batch endpoints whose body is an array or a nested struct rather than a flat map). The value is marshaled once and reused for signing.

func (*Request) WithSign

func (r *Request) WithSign() *Request

WithSign marks the request as private: ACCESS-* signing headers are attached at send time. Public market endpoints omit this.

type SignFn

type SignFn = func(secret, prehash string) (signature string, err error)

SignFn mirrors client.SignFn: it turns the prehash string into the ACCESS-SIGN header value, given the configured secret.

type WsAction added in v0.20260625.1

type WsAction string

WsAction classifies a data push as a full snapshot or an incremental update.

const (
	WsActionSnapshot WsAction = "snapshot" // full state
	WsActionUpdate   WsAction = "update"   // incremental change
)

type WsArg

type WsArg struct {
	InstType string `json:"instType"`
	Topic    string `json:"topic"`
	Symbol   string `json:"symbol,omitempty"`
	Coin     string `json:"coin,omitempty"`
	Interval string `json:"interval,omitempty"` // candlestick (kline) channel only
}

WsArg identifies a channel subscription. instType routes by product ("spot", "usdt-futures", "coin-futures", "usdc-futures", or "UTA" for the account channel); topic is the channel name; symbol/coin narrow it.

type WsClient

type WsClient interface {
	GetPublicURL() string
	GetPrivateURL() string
	GetAPIKey() string
	GetAPISecret() string
	GetPassphrase() string
	GetSignFn() SignFn
	GetLogger() log.Logger
	GetDialer() *websocket.Dialer
}

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

type WsError

type WsError struct {
	Code    string
	Message string
}

WsError is a Bitget WebSocket control-frame error.

func (*WsError) Error

func (e *WsError) Error() string

type WsPush

type WsPush[T any] struct {
	Action WsAction  `json:"action"`
	Arg    WsArg     `json:"arg"`
	Data   T         `json:"data"`
	Ts     time.Time `json:"ts"`
}

WsPush is the envelope Bitget pushes for a data event.

type WsTradeConn

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

WsTradeConn is a persistent, logged-in private connection for placing orders over WebSocket. Unlike the subscription channels it is request/response: each call sends an "op":"trade" frame tagged with a unique id and waits for the matching reply. One connection serves any number of concurrent calls.

func DialWsTrade

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

DialWsTrade opens the private gateway, logs in, 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 {
	Event  string    `json:"event"`
	ID     string    `json:"id"`
	Topic  string    `json:"topic"`
	Code   string    `json:"code"`
	Msg    string    `json:"msg"`
	Args   T         `json:"args"`
	ConnID string    `json:"connId"`
	Ts     time.Time `json:"ts"`
}

WsTradeResponse is the reply to a trade op. Args carries the topic-specific payload (e.g. the order acknowledgements).

func WsTradeCall

func WsTradeCall[T any](ctx context.Context, t *WsTradeConn, op, category, topic string, args []any) (*WsTradeResponse[T], error)

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

Jump to

Keyboard shortcuts

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