From a38c8182d33684778ca3a37d5f1189090bb9d167 Mon Sep 17 00:00:00 2001 From: aarjaneiro Date: Fri, 20 Sep 2024 14:49:11 -0400 Subject: [PATCH] Public endpoints no longer require values for credentials --- call.go | 69 ++++++++++++++++++++----- get_account.go | 2 +- get_best_bid_ask.go | 2 +- get_convert_trade.go | 2 +- get_futures_balance_summary.go | 2 +- get_futures_position.go | 2 +- get_market_trades.go | 2 +- get_order.go | 2 +- get_payment_method.go | 2 +- get_perpetuals_portfolio_summary.go | 2 +- get_perpetuals_position.go | 2 +- get_portfolio_breakdown.go | 2 +- get_product.go | 2 +- get_product_book.go | 2 +- get_product_candles.go | 2 +- get_public_market_trades.go | 2 +- get_public_product.go | 2 +- get_public_product_book.go | 2 +- get_public_product_candles.go | 2 +- get_server_time.go | 2 +- get_transactions_summary.go | 2 +- list_accounts.go | 2 +- list_fills.go | 2 +- list_futures_positions.go | 2 +- list_futures_sweeps.go | 2 +- list_orders.go | 2 +- list_payment_methods.go | 2 +- list_perpetuals_positions.go | 2 +- list_portfolios.go | 2 +- list_products.go | 2 +- list_public_products.go | 2 +- test/get_public_product_candles_test.go | 45 ++++++++++++++++ 32 files changed, 132 insertions(+), 42 deletions(-) create mode 100644 test/get_public_product_candles_test.go diff --git a/call.go b/call.go index e118ae4..d362fce 100644 --- a/call.go +++ b/call.go @@ -94,10 +94,10 @@ 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, @@ -105,7 +105,18 @@ func get( 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( @@ -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( @@ -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( @@ -139,6 +150,7 @@ func call( expectedHttpStatusCode int, request, response interface{}, + isPublic bool, ) error { if client.Credentials == nil { @@ -160,6 +172,7 @@ func call( expectedHttpStatusCode: expectedHttpStatusCode, client: client, }, + isPublic, ) if resp.err != nil { @@ -173,11 +186,39 @@ 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 { @@ -185,10 +226,13 @@ func makeCall(ctx context.Context, request *apiRequest) *apiResponse { 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)) @@ -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 diff --git a/get_account.go b/get_account.go index 87a4b20..54f86b3 100644 --- a/get_account.go +++ b/get_account.go @@ -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 } diff --git a/get_best_bid_ask.go b/get_best_bid_ask.go index 8391d09..cc39a90 100644 --- a/get_best_bid_ask.go +++ b/get_best_bid_ask.go @@ -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 } diff --git a/get_convert_trade.go b/get_convert_trade.go index f66fc48..043266b 100644 --- a/get_convert_trade.go +++ b/get_convert_trade.go @@ -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 } diff --git a/get_futures_balance_summary.go b/get_futures_balance_summary.go index d3e1353..0d232bc 100644 --- a/get_futures_balance_summary.go +++ b/get_futures_balance_summary.go @@ -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 } diff --git a/get_futures_position.go b/get_futures_position.go index 5c89b3c..db07e50 100644 --- a/get_futures_position.go +++ b/get_futures_position.go @@ -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 } diff --git a/get_market_trades.go b/get_market_trades.go index 983566d..d499d4a 100644 --- a/get_market_trades.go +++ b/get_market_trades.go @@ -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 } diff --git a/get_order.go b/get_order.go index 07ba241..aef1b92 100644 --- a/get_order.go +++ b/get_order.go @@ -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 } diff --git a/get_payment_method.go b/get_payment_method.go index f835fae..10a761f 100644 --- a/get_payment_method.go +++ b/get_payment_method.go @@ -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 } diff --git a/get_perpetuals_portfolio_summary.go b/get_perpetuals_portfolio_summary.go index a7da498..6ff8ad2 100644 --- a/get_perpetuals_portfolio_summary.go +++ b/get_perpetuals_portfolio_summary.go @@ -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 } diff --git a/get_perpetuals_position.go b/get_perpetuals_position.go index 0615822..5ed0a67 100644 --- a/get_perpetuals_position.go +++ b/get_perpetuals_position.go @@ -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 } diff --git a/get_portfolio_breakdown.go b/get_portfolio_breakdown.go index 3d89cb6..cf19b19 100644 --- a/get_portfolio_breakdown.go +++ b/get_portfolio_breakdown.go @@ -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 } diff --git a/get_product.go b/get_product.go index 44a7e76..66bb82b 100644 --- a/get_product.go +++ b/get_product.go @@ -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 } diff --git a/get_product_book.go b/get_product_book.go index 12b3143..d9892e8 100644 --- a/get_product_book.go +++ b/get_product_book.go @@ -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 } diff --git a/get_product_candles.go b/get_product_candles.go index 6757aba..6be3bc2 100644 --- a/get_product_candles.go +++ b/get_product_candles.go @@ -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 } diff --git a/get_public_market_trades.go b/get_public_market_trades.go index 2af4a6b..86faa70 100644 --- a/get_public_market_trades.go +++ b/get_public_market_trades.go @@ -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 } diff --git a/get_public_product.go b/get_public_product.go index 891351b..fd75f2f 100644 --- a/get_public_product.go +++ b/get_public_product.go @@ -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 } diff --git a/get_public_product_book.go b/get_public_product_book.go index 3e7f1af..a489dec 100644 --- a/get_public_product_book.go +++ b/get_public_product_book.go @@ -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 } diff --git a/get_public_product_candles.go b/get_public_product_candles.go index ca0b439..c251fc5 100644 --- a/get_public_product_candles.go +++ b/get_public_product_candles.go @@ -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 } diff --git a/get_server_time.go b/get_server_time.go index 49d1af4..110eafb 100644 --- a/get_server_time.go +++ b/get_server_time.go @@ -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 } diff --git a/get_transactions_summary.go b/get_transactions_summary.go index e8b7ade..dd4e552 100644 --- a/get_transactions_summary.go +++ b/get_transactions_summary.go @@ -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 } diff --git a/list_accounts.go b/list_accounts.go index 116370c..3f2d123 100644 --- a/list_accounts.go +++ b/list_accounts.go @@ -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 } diff --git a/list_fills.go b/list_fills.go index bc1edc6..bd2e0da 100644 --- a/list_fills.go +++ b/list_fills.go @@ -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 } diff --git a/list_futures_positions.go b/list_futures_positions.go index ac61b52..fec2ba3 100644 --- a/list_futures_positions.go +++ b/list_futures_positions.go @@ -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 } diff --git a/list_futures_sweeps.go b/list_futures_sweeps.go index 1c8d6d0..e26a074 100644 --- a/list_futures_sweeps.go +++ b/list_futures_sweeps.go @@ -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 } diff --git a/list_orders.go b/list_orders.go index fd113ea..a9bd8a3 100644 --- a/list_orders.go +++ b/list_orders.go @@ -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 } diff --git a/list_payment_methods.go b/list_payment_methods.go index 2c0752a..fe583da 100644 --- a/list_payment_methods.go +++ b/list_payment_methods.go @@ -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 } diff --git a/list_perpetuals_positions.go b/list_perpetuals_positions.go index 820fbb9..2901754 100644 --- a/list_perpetuals_positions.go +++ b/list_perpetuals_positions.go @@ -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 } diff --git a/list_portfolios.go b/list_portfolios.go index 2456412..d155864 100644 --- a/list_portfolios.go +++ b/list_portfolios.go @@ -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 } diff --git a/list_products.go b/list_products.go index 7c6a941..275c114 100644 --- a/list_products.go +++ b/list_products.go @@ -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 } diff --git a/list_public_products.go b/list_public_products.go index 4c47fa9..849f83a 100644 --- a/list_public_products.go +++ b/list_public_products.go @@ -63,7 +63,7 @@ func (c Client) ListPublicProducts( response := &ListPublicProductsResponse{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 } diff --git a/test/get_public_product_candles_test.go b/test/get_public_product_candles_test.go new file mode 100644 index 0000000..ac5a655 --- /dev/null +++ b/test/get_public_product_candles_test.go @@ -0,0 +1,45 @@ +/** + * Copyright 2024-present Coinbase Global, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package test + +import ( + "context" + adv "github.com/coinbase-samples/advanced-trade-sdk-go" + "net/http" + "testing" + "time" +) + +func TestGetPublicProductCandles(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + client := adv.NewClient(&adv.Credentials{}, http.Client{}) + + candlesResponse, err := client.GetPublicProductCandles(ctx, &adv.GetPublicProductCandlesRequest{ + ProductId: "BTC-USD", + Granularity: "ONE_MINUTE", + }) + + if err != nil { + t.Fatal("failed to get public product candles:", err) + } + + if candlesResponse == nil || len(*candlesResponse.Candles) == 0 { + t.Fatal("no candles found or nil response") + } +}