common

package
v2.3.3 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2022 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AmountToLotSize

func AmountToLotSize(lot float64, precision int, amount float64) float64

AmountToLotSize converts an amount to a lot sized amount

func FormatTimestamp added in v2.3.2

func FormatTimestamp(t time.Time) int64

FormatTimestamp formats a time into Unix timestamp in milliseconds, as requested by Binance.

func IsAPIError

func IsAPIError(e error) bool

IsAPIError check if e is an API error

func MapHasAllKeys added in v2.3.2

func MapHasAllKeys(m map[string]interface{}, keys ...string) bool

func MapHasKeys added in v2.3.2

func MapHasKeys(m map[string]interface{}, keys ...string) bool

func ToJSONList

func ToJSONList(v []byte) []byte

ToJSONList convert v to json list if v is a map

Types

type APIError

type APIError struct {
	Status  int    `json:"status"`
	Code    int64  `json:"code"`
	Message string `json:"msg"`
}

APIError define API error when response status is 4xx or 5xx

func (APIError) Error

func (e APIError) Error() string

Error return error code and message

type Client added in v2.3.2

type Client interface {
	GetTimeOffset() int64
	UpdateTimeOffset(offset int64)
	UpdateDoFunc(f DoFunc)
	UpdateHTTPClient(hc *http.Client)
	CallAPIBytes(ctx context.Context, r *Request, opts ...RequestOption) (data []byte, err error)
	CallAPI(ctx context.Context, r *Request, result interface{}, opts ...RequestOption) (err error)
}

func NewClient added in v2.3.2

func NewClient(apiKey, secretKey, baseURL, userAgent string, httpClient *http.Client,
	logger Logger,
) Client

type DefaultLogger added in v2.3.2

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

func NewDefaultLogger added in v2.3.2

func NewDefaultLogger(level LogLevel, p Printf) *DefaultLogger

func (*DefaultLogger) Debugw added in v2.3.2

func (d *DefaultLogger) Debugw(msg string, keyAndValues ...interface{})

func (*DefaultLogger) Infow added in v2.3.2

func (d *DefaultLogger) Infow(msg string, keyAndValues ...interface{})

func (*DefaultLogger) SetLevel added in v2.3.2

func (d *DefaultLogger) SetLevel(level LogLevel)

func (*DefaultLogger) Warningw added in v2.3.2

func (d *DefaultLogger) Warningw(msg string, keyAndValues ...interface{})

type DoFunc added in v2.3.2

type DoFunc func(req *http.Request) (*http.Response, error)

type LogLevel added in v2.3.2

type LogLevel int
const (
	LogDebug LogLevel = iota + 1
	LogInfo
	LogWarning
)

type Logger added in v2.3.2

type Logger interface {
	Debugw(msg string, keyAndValues ...interface{})
	Infow(msg string, keyAndValues ...interface{})
	Warningw(msg string, keyAndValues ...interface{})
}

type MockWebsocketSession added in v2.3.2

type MockWebsocketSession interface {
	WebsocketSession
	MockProcessMessage(data []byte) error
}

func NewMockWebsocketSession added in v2.3.2

func NewMockWebsocketSession(handler WebsocketSessionHandler) MockWebsocketSession

type Params added in v2.3.2

type Params map[string]interface{}

type PriceLevel

type PriceLevel struct {
	Price    string
	Quantity string
}

PriceLevel is a common structure for bids and asks in the order book.

func (*PriceLevel) Parse

func (p *PriceLevel) Parse() (float64, float64, error)

Parse parses this PriceLevel's Price and Quantity and returns them both. It also returns an error if either fails to parse.

type PriceLevelArray added in v2.3.2

type PriceLevelArray []string

func (PriceLevelArray) Parse added in v2.3.2

func (p PriceLevelArray) Parse() (float64, float64, error)

Parse parses this PriceLevelArray Price and Quantity and returns them both. It also returns an error if either fails to parse.

type Printf added in v2.3.2

type Printf interface {
	Printf(format string, v ...interface{})
}

type Request added in v2.3.2

type Request struct {
	ID         uint64
	Method     string
	Endpoint   string
	SecType    SecType
	Query      url.Values
	Form       url.Values
	RecvWindow int64
	Header     http.Header
}

Request define an API Request

func NewDeleteRequest added in v2.3.2

func NewDeleteRequest(endpoint string, secType SecType) *Request

func NewDeleteRequestAPIKey added in v2.3.2

func NewDeleteRequestAPIKey(endpoint string) *Request

func NewDeleteRequestSigned added in v2.3.2

func NewDeleteRequestSigned(endpoint string) *Request

func NewGetRequest added in v2.3.2

func NewGetRequest(endpoint string, secType SecType) *Request

func NewGetRequestAPIKey added in v2.3.2

func NewGetRequestAPIKey(endpoint string) *Request

func NewGetRequestPublic added in v2.3.2

func NewGetRequestPublic(endpoint string) *Request

func NewGetRequestSigned added in v2.3.2

func NewGetRequestSigned(endpoint string) *Request

func NewPostRequest added in v2.3.2

func NewPostRequest(endpoint string, secType SecType) *Request

func NewPostRequestAPIKey added in v2.3.2

func NewPostRequestAPIKey(endpoint string) *Request

func NewPostRequestSigned added in v2.3.2

func NewPostRequestSigned(endpoint string) *Request

func NewPutRequest added in v2.3.2

func NewPutRequest(endpoint string, secType SecType) *Request

func NewPutRequestAPIKey added in v2.3.2

func NewPutRequestAPIKey(endpoint string) *Request

func NewPutRequestSigned added in v2.3.2

func NewPutRequestSigned(endpoint string) *Request

func NewRequest added in v2.3.2

func NewRequest(method, endpoint string, secType SecType) *Request

func (*Request) AddQuery added in v2.3.2

func (r *Request) AddQuery(key string, value interface{}) *Request

AddQuery add param with key/value to query string

func (*Request) SetForm added in v2.3.2

func (r *Request) SetForm(key string, value interface{}) *Request

SetForm set param with key/value to Request form body

func (*Request) SetFormParams added in v2.3.2

func (r *Request) SetFormParams(m Params) *Request

SetFormParams set Params with key/values to Request form body

func (*Request) SetQuery added in v2.3.2

func (r *Request) SetQuery(key string, value interface{}) *Request

SetQuery set param with key/value to query string

func (*Request) SetQueryParams added in v2.3.2

func (r *Request) SetQueryParams(m Params) *Request

SetQueryParams set Params with key/values to query string

func (*Request) Validate added in v2.3.2

func (r *Request) Validate() (err error)

type RequestOption added in v2.3.2

type RequestOption func(*Request)

RequestOption define option type for Request

func WithHeader added in v2.3.2

func WithHeader(key, value string, replace bool) RequestOption

WithHeader set or add a Header value to the Request

func WithHeaders added in v2.3.2

func WithHeaders(header http.Header) RequestOption

WithHeaders set or replace the headers of the Request

func WithRecvWindow added in v2.3.2

func WithRecvWindow(recvWindow int64) RequestOption

WithRecvWindow set recvWindow param for the Request

type SecType added in v2.3.2

type SecType int
const (
	SecTypeNone SecType = iota
	SecTypeAPIKey
	SecTypeSigned // if the 'timestamp' parameter is required
)

type WebsocketClient added in v2.3.2

type WebsocketClient interface {
	Loop(f WebsocketMessageCallback) error
	Delay() time.Duration
	Ping()
	Write(data []byte)
}

func WebsocketDial added in v2.3.2

func WebsocketDial(ctx context.Context, url string, httpClient *http.Client) (
	out WebsocketClient, err error,
)

func WebsocketDialProxy added in v2.3.2

func WebsocketDialProxy(ctx context.Context, url string, proxyURL *url.URL) (
	out WebsocketClient, err error,
)

type WebsocketMessageCallback added in v2.3.2

type WebsocketMessageCallback func(messageType websocket.MessageType, data []byte) error

type WebsocketReply added in v2.3.2

type WebsocketReply struct {
	ID      uint64 `json:"id"`
	Code    int64  `json:"code"`
	Message string `json:"msg"`
	Result  interface{}
}

func (*WebsocketReply) OK added in v2.3.2

func (wsr *WebsocketReply) OK() error

type WebsocketRequest added in v2.3.2

type WebsocketRequest struct {
	ID     uint64      `json:"id"`
	Method string      `json:"method"`
	Params interface{} `json:"params"`
}

type WebsocketSession added in v2.3.2

type WebsocketSession interface {
	// Loop hold this websocket connection until the connection disconnected or got error
	// from message processor. return error same as SessionHandler.OnClose function.
	Loop() (err error)
	// RunLoop create new go routine and call IOLoop function.
	RunLoop() chan error
	Subscribe(ctx context.Context, streams ...string) (reply *WebsocketReply, err error)
	SubscribeNoReply(ctx context.Context, streams ...string) (err error)
	RegisterMessageHandler(factory WebsocketSessionMessageFactory, callback WebsocketSessionMessageCallback,
		checker ...WebsocketSessionMessageChecker)
	RequireMapHasAllKeys(keys ...string) WebsocketSessionMessageChecker
	RequireMapKeyValue(key, value string) WebsocketSessionMessageChecker
}

func NewWebsocketSession added in v2.3.2

func NewWebsocketSession(client WebsocketClient, handler WebsocketSessionHandler) WebsocketSession

type WebsocketSessionHandler added in v2.3.2

type WebsocketSessionHandler interface {
	OnUnknownMessage([]byte, interface{}) error
	OnClose(err error)
}

type WebsocketSessionMessageCallback added in v2.3.2

type WebsocketSessionMessageCallback func(m interface{})

func WebsocketSessionMessageHandlerBuild added in v2.3.2

func WebsocketSessionMessageHandlerBuild[T any](f func(*T)) WebsocketSessionMessageCallback

type WebsocketSessionMessageChecker added in v2.3.2

type WebsocketSessionMessageChecker func(m interface{}) bool

type WebsocketSessionMessageFactory added in v2.3.2

type WebsocketSessionMessageFactory func() interface{}

func WebsocketSessionMessageFactoryBuild added in v2.3.2

func WebsocketSessionMessageFactoryBuild[T any]() WebsocketSessionMessageFactory

type WebsocketSubscribe added in v2.3.2

type WebsocketSubscribe interface {
	Run(ctx context.Context)
	Subscribe(topics ...string) bool
}

func NewWebsocketSubscribe added in v2.3.2

func NewWebsocketSubscribe(wsc WebsocketClient) WebsocketSubscribe

Jump to

Keyboard shortcuts

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