Merge pull request #3 from wuhewuhe/master

change infra of http client
This commit is contained in:
CommaHunger 2023-11-03 15:29:46 +01:00 committed by GitHub
commit 5f754fe3d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 81 additions and 38 deletions

View File

@ -1,13 +1,13 @@
package bybit
package bybit_connector
import (
"bybit.go.api/handlers"
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/json"
"fmt"
handlers "github.com/wuhewuhe/bybit.go.api/handlers"
"io"
"log"
"net/http"
@ -31,6 +31,20 @@ type Client struct {
type doFunc func(req *http.Request) (*http.Response, error)
type ClientOption func(*Client)
func WithDebug(debug bool) ClientOption {
return func(c *Client) {
c.Debug = debug
}
}
func WithBaseURL(baseURL string) ClientOption {
return func(c *Client) {
c.BaseURL = baseURL
}
}
func currentTimestamp() int64 {
return FormatTimestamp(time.Now())
}
@ -40,6 +54,14 @@ func FormatTimestamp(t time.Time) int64 {
return t.UnixNano() / int64(time.Millisecond)
}
type ServerResponse struct {
RetCode int `json:"retCode"`
RetMsg string `json:"retMsg"`
Result interface{} `json:"result"`
RetExtInfo struct{} `json:"retExtInfo"`
Time int64 `json:"time"`
}
func PrettyPrint(i interface{}) string {
s, _ := json.MarshalIndent(i, "", "\t")
return string(s)
@ -51,21 +73,22 @@ func (c *Client) debug(format string, v ...interface{}) {
}
}
// NewClient Create client function for initialising new Bybit client
func NewClient(apiKey string, secretKey string, baseURL ...string) *Client {
url := "https://api.bybit.com"
if len(baseURL) > 0 {
url = baseURL[0]
}
return &Client{
// NewBybitHttpClient NewClient Create client function for initialising new Bybit client
func NewBybitHttpClient(apiKey string, secretKey string, options ...ClientOption) *Client {
c := &Client{
APIKey: apiKey,
SecretKey: secretKey,
BaseURL: url,
BaseURL: "https://api.bybit.com",
HTTPClient: http.DefaultClient,
Logger: log.New(os.Stderr, Name, log.LstdFlags),
}
// Apply the provided options
for _, opt := range options {
opt(c)
}
return c
}
func (c *Client) parseRequest(r *request, opts ...RequestOption) (err error) {
@ -120,7 +143,7 @@ func (c *Client) parseRequest(r *request, opts ...RequestOption) (err error) {
if queryString != "" {
fullURL = fmt.Sprintf("%s?%s", fullURL, queryString)
}
// c.debug("full url: %s, body: %s", fullURL, bodyString)
c.debug("full url: %s, body: %s", fullURL, bodyString)
r.fullURL = fullURL
r.header = header
r.body = body
@ -135,7 +158,7 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
}
req = req.WithContext(ctx)
req.Header = r.header
// c.debug("request: %#v", req)
c.debug("request: %#v", req)
f := c.do
if f == nil {
f = c.HTTPClient.Do
@ -156,15 +179,17 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
err = cerr
}
}()
// c.debug("response: %#v", res)
c.debug("response: %#v", res)
c.debug("response body: %s", string(data))
// c.debug("response status code: %d", res.StatusCode)
c.debug("response status code: %d", res.StatusCode)
if res.StatusCode >= http.StatusBadRequest {
apiErr := new(handlers.APIError)
var (
apiErr = new(handlers.APIError)
)
e := json.Unmarshal(data, apiErr)
if e != nil {
// c.debug("failed to unmarshal json: %s", e)
c.debug("failed to unmarshal json: %s", e)
}
return nil, apiErr
}

View File

@ -1,4 +1,4 @@
package bybit
package bybit_connector
import (
"crypto/hmac"

View File

@ -1,4 +1,4 @@
package bybit
package bybit_connector
const (
Name = "bybit.api.go"

View File

@ -1,9 +1,9 @@
package main
import (
bybit "bybit.go.api"
"context"
"fmt"
bybit "github.com/wuhewuhe/bybit.go.api"
)
func main() {
@ -12,12 +12,13 @@ func main() {
func ServerTime() {
client := bybit.NewClient("", "")
// set to debug mode
client.Debug = true
client := bybit.NewBybitHttpClient("", "")
// NewServerTimeService
serverTime := client.NewServerTimeService().Do(context.Background())
serverTime, err := client.NewServerTimeService().Do(context.Background())
if err != nil {
fmt.Println(err)
return
}
fmt.Println(bybit.PrettyPrint(serverTime))
}

2
go.mod
View File

@ -1,4 +1,4 @@
module bybit.go.api
module github.com/wuhewuhe/bybit.go.api
go 1.21

View File

@ -1,6 +1,9 @@
package handlers
import "fmt"
import (
"errors"
"fmt"
)
// APIError define API error when response status is 4xx or 5xx
type APIError struct {
@ -15,6 +18,7 @@ func (e APIError) Error() string {
// IsAPIError check if e is an API error
func IsAPIError(e error) bool {
_, ok := e.(*APIError)
var APIError *APIError
ok := errors.As(e, &APIError)
return ok
}

View File

@ -1,23 +1,36 @@
package bybit
package bybit_connector
import (
"context"
"encoding/json"
"net/http"
)
// Binance Check Server Time endpoint (GET /v5/market/time)
type ServerTimeResult struct {
TimeSecond string `json:"timeSecond"`
TimeNano string `json:"timeNano"`
}
// ServerTime Binance Check Server Time endpoint (GET /v5/market/time)
type ServerTime struct {
c *Client
}
// Send the request
func (s *ServerTime) Do(ctx context.Context, opts ...RequestOption) (res []byte) {
// Do Send the request
func (s *ServerTime) Do(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/v5/market/time",
secType: secTypeNone,
}
data, _ := s.c.callAPI(ctx, r, opts...)
res = data
return res
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res = new(ServerResponse)
err = json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}

View File

@ -1,4 +1,4 @@
package bybit
package bybit_connector
import (
"fmt"

View File

@ -1 +1 @@
package bybit
package bybit_connector