Skip to content

Commit

Permalink
bitfinex add version into request
Browse files Browse the repository at this point in the history
  • Loading branch information
dasbd72 committed Mar 2, 2024
1 parent 292675d commit c11a665
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
10 changes: 5 additions & 5 deletions bitfinex/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type Client struct {
}

const (
basePublicApiURL = "https://api-pub.bitfinex.com/v2"
basePrivateApiURL = "https://api.bitfinex.com/v2"
basePublicApiURL = "https://api-pub.bitfinex.com"
basePrivateApiURL = "https://api.bitfinex.com"

apiKeyHeader = "bfx-apikey"
nonceHeader = "bfx-nonce"
Expand Down Expand Up @@ -84,7 +84,7 @@ func (c *Client) CallAPI(ctx context.Context, r *Request, opts ...RequestOption)

func (c *Client) getHttpRequest(ctx context.Context, r *Request) (*http.Request, error) {
apiEndpoint := c.publicApiEndpoint
path := fmt.Sprintf("%s%s", r.endpoint, r.subEndpoint)
path := r.endpoint
query := url.Values{}
header := http.Header{}
body := ""
Expand Down Expand Up @@ -112,10 +112,10 @@ func (c *Client) getHttpRequest(ctx context.Context, r *Request) (*http.Request,
nonce := currentTimestamp()
header.Set(apiKeyHeader, c.apiKey)
header.Set(nonceHeader, nonce)
header.Set(signatureHeader, sign(c.apiSecret, fmt.Sprintf("/api/v2%s%s%s", path, nonce, body)))
header.Set(signatureHeader, sign(c.apiSecret, fmt.Sprintf("/api%s%s%s%s", r.version, path, nonce, body)))
}
// create request
req, err := http.NewRequestWithContext(ctx, r.method, fmt.Sprintf("%s%s", apiEndpoint, path), bytes.NewBuffer([]byte(body)))
req, err := http.NewRequestWithContext(ctx, r.method, fmt.Sprintf("%s%s%s", apiEndpoint, r.version, path), bytes.NewBuffer([]byte(body)))
if err != nil {
return nil, err
}
Expand Down
23 changes: 17 additions & 6 deletions bitfinex/request.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package bitfinex

import "net/http"
import (
"net/http"
)

type SecType int

const (
// Version1 is for v1 API
Version1 = "/v1"
// Version2 is for v2 API
Version2 = "/v2"
)

const (
// SecTypePublic is for public API
SecTypePublic SecType = iota
Expand All @@ -13,17 +22,18 @@ const (

// Request define an API request, build with Request_builder
type Request struct {
method string
endpoint string
subEndpoint string
secType SecType
params map[string]interface{}
method string
endpoint string
version string
secType SecType
params map[string]interface{}
}

// Request_builder define a builder for Request
type Request_builder struct {
Method string
Endpoint string
Version string
SecType SecType
Params map[string]interface{}
}
Expand All @@ -39,6 +49,7 @@ func (b Request_builder) Build() *Request {
return &Request{
method: b.Method,
endpoint: b.Endpoint,
version: b.Version,
secType: b.SecType,
params: b.Params,
}
Expand Down
2 changes: 2 additions & 0 deletions bitfinex/v2_funding_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (c *Client) GetFundingStats(ctx context.Context, symbol string, opts ...Req
res, err := c.CallAPI(ctx, Request_builder{
Method: http.MethodGet,
Endpoint: fmt.Sprintf("/funding/stats/%s/hist", symbol),
Version: Version2,
SecType: SecTypePublic,
}.Build(), opts...)
if err != nil {
Expand Down Expand Up @@ -120,6 +121,7 @@ func (c *Client) GetActiveFundingOffers(ctx context.Context, symbol string, opts
res, err := c.CallAPI(ctx, Request_builder{
Method: http.MethodPost,
Endpoint: fmt.Sprintf("/auth/r/funding/offers/%s", symbol),
Version: Version2,
SecType: SecTypePrivate,
}.Build(), opts...)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions bitfinex/v2_wallet_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (c *Client) GetWallets(ctx context.Context, opts ...RequestOption) (*GetWal
res, err := c.CallAPI(ctx, Request_builder{
Method: http.MethodPost,
Endpoint: "/auth/r/wallets",
Version: Version2,
SecType: SecTypePrivate,
}.Build(), opts...)
if err != nil {
Expand Down

0 comments on commit c11a665

Please sign in to comment.