Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public endpoints no longer require values for credentials #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 57 additions & 12 deletions call.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,29 @@ func post(
request,
response interface{},
) error {
return call(ctx, client, path, query, http.MethodPost, http.StatusOK, request, response)
return callPrivate(ctx, client, path, query, http.MethodPost, http.StatusOK, request, response)
}

func get(
func getPrivate(
ctx context.Context,
client Client,
path,
query string,
request,
response interface{},
) error {
return call(ctx, client, path, query, http.MethodGet, http.StatusOK, request, response)
return callPrivate(ctx, client, path, query, http.MethodGet, http.StatusOK, request, response)
}

func getPublic(
ctx context.Context,
client Client,
path,
query string,
request,
response interface{},
) error {
return callPublic(ctx, client, path, query, http.MethodGet, http.StatusOK, request, response)
}

func put(
Expand All @@ -116,7 +127,7 @@ func put(
request,
response interface{},
) error {
return call(ctx, client, path, query, http.MethodPut, http.StatusOK, request, response)
return callPrivate(ctx, client, path, query, http.MethodPut, http.StatusOK, request, response)
}

func del(
Expand All @@ -127,7 +138,7 @@ func del(
request,
response interface{},
) error {
return call(ctx, client, path, query, http.MethodDelete, http.StatusOK, request, response)
return callPrivate(ctx, client, path, query, http.MethodDelete, http.StatusOK, request, response)
}

func call(
Expand All @@ -139,6 +150,7 @@ func call(
expectedHttpStatusCode int,
request,
response interface{},
isPublic bool,
) error {

if client.Credentials == nil {
Expand All @@ -160,6 +172,7 @@ func call(
expectedHttpStatusCode: expectedHttpStatusCode,
client: client,
},
isPublic,
)

if resp.err != nil {
Expand All @@ -173,22 +186,53 @@ func call(
return nil
}

func makeCall(ctx context.Context, request *apiRequest) *apiResponse {
func callPublic(
ctx context.Context,
client Client,
path,
query,
httpMethod string,
expectedHttpStatusCode int,
request,
response interface{},
) error {
return call(ctx, client, path, query, httpMethod, expectedHttpStatusCode, request, response, true)
}

func callPrivate(
ctx context.Context,
client Client,
path,
query,
httpMethod string,
expectedHttpStatusCode int,
request,
response interface{},
) error {
return call(ctx, client, path, query, httpMethod, expectedHttpStatusCode, request, response, false)
}

func makeCall(ctx context.Context, request *apiRequest, isPublic bool) *apiResponse {
response := &apiResponse{
request: request,
}

var jwtToken string

callUrl := fmt.Sprintf("%s%s%s", request.client.HttpBaseUrl, request.path, request.query)
parsedUrl, err := url.Parse(callUrl)
if err != nil {
response.err = fmt.Errorf("invalid URL: %s - %w", callUrl, err)
return response
}

jwtToken, err := generateJwt(request.httpMethod, parsedUrl.Path, parsedUrl.Host, request.client.Credentials.AccessKey, request.client.Credentials.PrivatePemKey)
if err != nil {
response.err = fmt.Errorf("failed to generate JWT: %w", err)
return response
if !isPublic {
var err error
jwtToken, err = generateJwt(request.httpMethod, parsedUrl.Path, parsedUrl.Host, request.client.Credentials.AccessKey, request.client.Credentials.PrivatePemKey)
if err != nil {
response.err = fmt.Errorf("failed to generate JWT: %w", err)
return response
}
}

req, err := http.NewRequestWithContext(ctx, request.httpMethod, callUrl, bytes.NewReader(request.body))
Expand All @@ -198,8 +242,9 @@ func makeCall(ctx context.Context, request *apiRequest) *apiResponse {
}

req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", jwtToken))

if !isPublic {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", jwtToken))
}
res, err := request.client.HttpClient.Do(req)
if err != nil {
response.err = err
Expand Down
2 changes: 1 addition & 1 deletion get_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c Client) GetAccount(

response := &GetAccountResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_best_bid_ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (c Client) GetBestBidAsk(

response := &GetBestBidAskResponse{Request: request}

if err := get(ctx, c, path, queryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, queryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_convert_trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (c Client) GetConvertTrade(

response := &GetConvertTradeResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_futures_balance_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (c Client) GetFuturesBalanceSummary(

response := &GetFuturesBalanceSummaryResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_futures_position.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c Client) GetFuturesPosition(

response := &GetFuturesPositionResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_market_trades.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (c Client) GetMarketTrades(
queryParams = appendQueryParam(queryParams, "end", request.End)
}

if err := get(ctx, c, path, queryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, queryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (c Client) GetOrder(

response := &GetOrderResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_payment_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c Client) GetPaymentMethod(

response := &GetPaymentMethodResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_perpetuals_portfolio_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c Client) GetPerpetualsPortfolioSummary(

response := &GetPerpetualsPortfolioSummaryResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_perpetuals_position.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c Client) GetPerpetualsPosition(

response := &GetPerpetualsPositionResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_portfolio_breakdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c Client) GetPortfolioBreakdown(

response := &GetPortfolioBreakdownResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_product.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c Client) GetProduct(

response := &GetProductResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_product_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c Client) GetProductBook(

response := &GetProductBookResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_product_candles.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (c Client) GetProductCandles(

queryParams = appendQueryParam(queryParams, "end", request.End)

if err := get(ctx, c, path, queryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, queryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_public_market_trades.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (c Client) GetPublicMarketTrades(
queryParams = appendQueryParam(queryParams, "end", request.End)
}

if err := get(ctx, c, path, queryParams, request, response); err != nil {
if err := getPublic(ctx, c, path, queryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_public_product.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c Client) GetPublicProduct(

response := &GetPublicProductResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPublic(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_public_product_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c Client) GetPublicProductBook(

response := &GetPublicProductBookResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPublic(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_public_product_candles.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (c Client) GetPublicProductCandles(

queryParams = appendQueryParam(queryParams, "end", request.End)

if err := get(ctx, c, path, queryParams, request, response); err != nil {
if err := getPublic(ctx, c, path, queryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_server_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c Client) GetServerTime(

response := &GetServerTimeResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion get_transactions_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c Client) GetTransactionsSummary(

response := &GetTransactionsSummaryResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion list_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c Client) ListAccounts(

response := &ListAccountsResponse{Request: request}

if err := get(ctx, c, path, queryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, queryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion list_fills.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c Client) ListFills(

response := &ListFillsResponse{Request: request}

if err := get(ctx, c, path, queryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, queryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion list_futures_positions.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (c Client) ListFuturesPositions(

response := &ListFuturesPositionsResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion list_futures_sweeps.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (c Client) ListFuturesSweeps(

response := &ListFuturesSweepsResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion list_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (c Client) ListOrders(

response := &ListOrdersResponse{Request: request}

if err := get(ctx, c, path, queryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, queryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion list_payment_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (c Client) ListPaymentMethods(

response := &ListPaymentMethodsResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion list_perpetuals_positions.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c Client) ListPerpetualsPositions(

response := &ListPerpetualsPositionsResponse{Request: request}

if err := get(ctx, c, path, emptyQueryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, emptyQueryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion list_portfolios.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c Client) ListPortfolios(

response := &ListPortfoliosResponse{Request: request}

if err := get(ctx, c, path, queryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, queryParams, request, response); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion list_products.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c Client) ListProducts(

response := &ListProductsResponse{Request: request}

if err := get(ctx, c, path, queryParams, request, response); err != nil {
if err := getPrivate(ctx, c, path, queryParams, request, response); err != nil {
return nil, err
}

Expand Down
Loading