Documentation
¶
Index ¶
- func DialPrivateLoggedIn(ctx context.Context, client WsClient) (*websocket.Conn, error)
- func Do[T any](r *Request) (resp *T, err error)
- func DoRaw(r *Request) ([]byte, error)
- func DoRawData(r *Request) ([]byte, error)
- func HMACSign(secret, prehash string) (string, error)
- func Subscribe[T any](ctx context.Context, client WsClient, private bool, arg WsArg, ...) (done chan<- struct{}, stop <-chan struct{}, err error)
- func SubscribeRaw(ctx context.Context, client WsClient, private bool, arg WsArg, ...) (done chan<- struct{}, stop <-chan struct{}, err error)
- func SubscribeRawArg(ctx context.Context, client WsClient, private bool, arg any, ...) (done chan<- struct{}, stop <-chan struct{}, err error)
- type Client
- type Request
- type SignFn
- type WsAction
- type WsArg
- type WsClient
- type WsError
- type WsPush
- type WsTradeConn
- type WsTradeResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DialPrivateLoggedIn ¶ added in v0.20260618.1
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Post builds a POST request. Any body maps are merged and JSON-encoded once; the exact bytes sent are the bytes signed.
type SignFn ¶
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.
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 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.