Skip to content

Commit

Permalink
Added context support
Browse files Browse the repository at this point in the history
  • Loading branch information
MarketDataApp committed Sep 10, 2024
1 parent 37583d2 commit bfa1fac
Show file tree
Hide file tree
Showing 44 changed files with 584 additions and 263 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"cSpell.words": [
"bulkcandles",
"bulkquotes",
"datekey"
"datekey",
"Ratelimit",
"resty"
]
}
5 changes: 3 additions & 2 deletions baseRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
package client

import (
"context"
"fmt"

"github.com/MarketDataApp/sdk-go/helpers/parameters"
Expand Down Expand Up @@ -265,7 +266,7 @@ func (br *baseRequest) getError() error {
//
// - *resty.Response: The raw response from the executed request.
// - error: An error object if the baseRequest is nil, or if an error occurs during the request execution.
func (request *baseRequest) Raw() (*resty.Response, error) {
func (request *baseRequest) Raw(ctx context.Context) (*resty.Response, error) {
if request == nil {
return nil, fmt.Errorf("baseRequest is nil")
}
Expand All @@ -274,6 +275,6 @@ func (request *baseRequest) Raw() (*resty.Response, error) {
return nil, fmt.Errorf("MarketDataClient is nil")
}

response, err := request.client.getRawResponse(request)
response, err := request.client.getRawResponse(ctx, request)
return response, err
}
15 changes: 10 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"strconv"
"sync"
"time"
"context"

"github.com/go-resty/resty/v2"
_ "github.com/joho/godotenv/autoload"
Expand Down Expand Up @@ -349,7 +350,7 @@ func (c *MarketDataClient) updateRateLimit(resp *resty.Response) {

// prepareAndExecuteRequest prepares the request based on the provided baseRequest and executes it.
// It returns the response from the server or an error if the request preparation or execution fails.
func (c *MarketDataClient) prepareAndExecuteRequest(br *baseRequest, result interface{}) (*resty.Response, error) {
func (c *MarketDataClient) prepareAndExecuteRequest(ctx context.Context, br *baseRequest, result interface{}) (*resty.Response, error) {

// Check for any errors in the base request.
if err := br.getError(); err != nil {
Expand Down Expand Up @@ -385,6 +386,9 @@ func (c *MarketDataClient) prepareAndExecuteRequest(br *baseRequest, result inte
return nil, err
}

// Use the provided context for the request
req = req.SetContext(ctx)

// Execute the GET request to the specified path.
resp, err := req.Get(path)
if err != nil {
Expand Down Expand Up @@ -419,9 +423,9 @@ func (c *MarketDataClient) prepareAndExecuteRequest(br *baseRequest, result inte
//
// - A pointer to a resty.Response object containing the response from the server.
// - An error object if an error occurred during the request execution or if the response contains an error.
func (c *MarketDataClient) getFromRequest(br *baseRequest, result interface{}) (*resty.Response, error) {
func (c *MarketDataClient) getFromRequest(ctx context.Context, br *baseRequest, result interface{}) (*resty.Response, error) {
// Execute the prepared request and capture the response and any error.
resp, err := c.prepareAndExecuteRequest(br, result)
resp, err := c.prepareAndExecuteRequest(ctx, br, result)
if err != nil {
// Return the response and the error if an error occurred during request execution.
return resp, err
Expand All @@ -442,14 +446,15 @@ func (c *MarketDataClient) getFromRequest(br *baseRequest, result interface{}) (
//
// # Parameters
//
// - ctx: A context.Context object to control the request's lifecycle.
// - br: A pointer to a baseRequest object containing the request details.
//
// # Returns
//
// - A pointer to a resty.Response object containing the raw response from the server.
// - An error object if an error occurred during the request execution.
func (c *MarketDataClient) getRawResponse(br *baseRequest) (*resty.Response, error) {
return c.prepareAndExecuteRequest(br, nil)
func (c *MarketDataClient) getRawResponse(ctx context.Context, br *baseRequest) (*resty.Response, error) {
return c.prepareAndExecuteRequest(ctx, br, nil)
}

// GetClient checks for an existing instance of MarketDataClient and returns it.
Expand Down
8 changes: 4 additions & 4 deletions client_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ const (
)

// getRateLimitConsumed extracts the rate limit consumed value from the response headers.
// It specifically looks for the "X-Api-RateLimit-Consumed" header and attempts to convert its value to an integer.
// It specifically looks for the "X-Api-Ratelimit-Consumed" header and attempts to convert its value to an integer.
//
// # Parameters
//
// - resp: A pointer to a resty.Response from which the header will be extracted.
//
// # Returns
//
// - int: The integer value of the "X-Api-RateLimit-Consumed" header if present and successfully converted.
// - int: The integer value of the "X-Api-Ratelimit-Consumed" header if present and successfully converted.
// - error: An error if the header is missing or if the conversion to an integer fails.
func getRateLimitConsumed(resp *resty.Response) (int, error) {
rateLimitConsumedStr := resp.Header().Get("X-Api-RateLimit-Consumed")
rateLimitConsumedStr := resp.Header().Get("X-Api-Ratelimit-Consumed")
if rateLimitConsumedStr == "" {
return 0, errors.New("error: missing 'x-Api-RateLimit-Consumed' header")
return 0, errors.New("error: missing 'x-Api-Ratelimit-Consumed' header")
}
rateLimitConsumed, err := strconv.Atoi(rateLimitConsumedStr)
if err != nil {
Expand Down
11 changes: 7 additions & 4 deletions examples/examples.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package examples

import (
"context"
"fmt"

api "github.com/MarketDataApp/sdk-go"
)

func RawHttpResponseExample() {
resp, err := api.StockQuote().Symbol("AAPL").Raw()
ctx := context.TODO()
resp, err := api.StockQuote().Symbol("AAPL").Raw(ctx)
if err != nil {
fmt.Print(err)
return
Expand All @@ -17,7 +19,8 @@ func RawHttpResponseExample() {
}

func LogExample() {
_, err := api.IndexQuotes().Symbol("VIX").FiftyTwoWeek(true).Get()
ctx := context.TODO()
_, err := api.IndexQuotes().Symbol("VIX").FiftyTwoWeek(true).Get(ctx)
if err != nil {
fmt.Print(err)
return
Expand All @@ -28,8 +31,8 @@ func LogExample() {
}

func MarketStatusExample() {

msr, err := api.MarketStatus().From("2022-01-01").To("2022-01-10").Packed()
ctx := context.TODO()
msr, err := api.MarketStatus().From("2022-01-01").To("2022-01-10").Packed(ctx)
if err != nil {
fmt.Print(err)
return
Expand Down
7 changes: 5 additions & 2 deletions examples/examples_indices.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package examples

import (
"context"
"fmt"
"time"

api "github.com/MarketDataApp/sdk-go"
)

func IndexQuoteExample() {
iqe, err := api.IndexQuotes().Symbol("VIX").FiftyTwoWeek(true).Packed()
ctx := context.TODO()
iqe, err := api.IndexQuotes().Symbol("VIX").FiftyTwoWeek(true).Packed(ctx)
if err != nil {
fmt.Print(err)
return
Expand All @@ -32,7 +34,8 @@ func IndexQuoteExample() {

func IndexCandlesExample() {
oneWeekAgo := time.Now().AddDate(0, 0, -7)
ice, err := api.IndexCandles().Resolution("D").Symbol("VIX").From(oneWeekAgo).To("today").Packed()
ctx := context.TODO()
ice, err := api.IndexCandles().Resolution("D").Symbol("VIX").From(oneWeekAgo).To("today").Packed(ctx)
if err != nil {
fmt.Print(err)
return
Expand Down
16 changes: 11 additions & 5 deletions examples/examples_options.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package examples

import (
"context"
"fmt"

api "github.com/MarketDataApp/sdk-go"
)

func OptionsChainExample() {
resp, err := api.OptionChain().UnderlyingSymbol("AAPL").Side("call").DTE(60).StrikeLimit(2).Get()
ctx := context.TODO()
resp, err := api.OptionChain().UnderlyingSymbol("AAPL").Side("call").DTE(60).StrikeLimit(2).Get(ctx)
if err != nil {
fmt.Print(err)
return
Expand All @@ -20,7 +22,8 @@ func OptionsChainExample() {
}

func OptionsQuotesExample() {
resp, err := api.OptionQuote().OptionSymbol("AAPL250117C00150000").Get()
ctx := context.TODO()
resp, err := api.OptionQuote().OptionSymbol("AAPL250117C00150000").Get(ctx)
if err != nil {
fmt.Print(err)
return
Expand All @@ -31,7 +34,8 @@ func OptionsQuotesExample() {
}

func OptionStrikesExample() {
resp, err := api.OptionStrikes().UnderlyingSymbol("AAPL").Get()
ctx := context.TODO()
resp, err := api.OptionStrikes().UnderlyingSymbol("AAPL").Get(ctx)
if err != nil {
fmt.Print(err)
return
Expand All @@ -43,7 +47,8 @@ func OptionStrikesExample() {
}

func OptionsLookupExample() {
resp, err := api.OptionLookup().UserInput("AAPL 7/28/2023 200 Call").Get()
ctx := context.TODO()
resp, err := api.OptionLookup().UserInput("AAPL 7/28/2023 200 Call").Get(ctx)
if err != nil {
fmt.Print(err)
return
Expand All @@ -53,7 +58,8 @@ func OptionsLookupExample() {
}

func OptionsExpirationsExample() {
resp, err := api.OptionsExpirations().UnderlyingSymbol("AAPL").Strike(200).Get()
ctx := context.TODO()
resp, err := api.OptionsExpirations().UnderlyingSymbol("AAPL").Strike(200).Get(ctx)
if err != nil {
fmt.Print(err)
return
Expand Down
14 changes: 9 additions & 5 deletions examples/examples_stocks.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package examples

import (
"context"
"fmt"

api "github.com/MarketDataApp/sdk-go"
)

func StockQuoteExample() {
sqe, err := api.StockQuote().Symbol("AAPL").FiftyTwoWeek(true).Packed()
ctx := context.TODO()
sqe, err := api.StockQuote().Symbol("AAPL").FiftyTwoWeek(true).Packed(ctx)
if err != nil {
fmt.Print(err)
return
Expand All @@ -29,8 +31,8 @@ func StockQuoteExample() {
}

func StockCandlesExample() {

sce, err := api.StockCandles().Resolution("1").Symbol("AAPL").From("2023-01-01").To("2023-01-04").Packed()
ctx := context.TODO()
sce, err := api.StockCandles().Resolution("1").Symbol("AAPL").From("2023-01-01").To("2023-01-04").Packed(ctx)
if err != nil {
fmt.Print(err)
return
Expand All @@ -49,7 +51,8 @@ func StockCandlesExample() {
}

func StockEarningsExample() {
see, err := api.StockEarnings().Symbol("AAPL").From("2022-01-01").To("2022-12-31").Packed()
ctx := context.TODO()
see, err := api.StockEarnings().Symbol("AAPL").From("2022-01-01").To("2022-12-31").Packed(ctx)
if err != nil {
fmt.Print(err)
return
Expand All @@ -71,7 +74,8 @@ func StockEarningsExample() {
}

func StockNewsExample() {
resp, err := api.StockNews().Symbol("AAPL").Get()
ctx := context.TODO()
resp, err := api.StockNews().Symbol("AAPL").Get(ctx)
if err != nil {
fmt.Print(err)
return
Expand Down
17 changes: 10 additions & 7 deletions examples/examples_v2.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package examples

import (
"context"
"fmt"
"log"
"sort"
Expand All @@ -11,8 +12,8 @@ import (
)

func StockCandlesV2Example() {

sce, err := api.StockCandlesV2().Resolution("1").Symbol("AAPL").DateKey("2023-01").Packed()
ctx := context.TODO()
sce, err := api.StockCandlesV2().Resolution("1").Symbol("AAPL").DateKey("2023-01").Packed(ctx)
if err != nil {
fmt.Print(err)
return
Expand All @@ -30,7 +31,8 @@ func StockCandlesV2Example() {
}

func StocksTickersV2Example() {
tickers, err := api.StockTickers().DateKey("2023-01-05").Packed()
ctx := context.TODO()
tickers, err := api.StockTickers().DateKey("2023-01-05").Packed(ctx)
if err != nil {
fmt.Print(err)
return
Expand All @@ -41,8 +43,8 @@ func StocksTickersV2Example() {

func SaveTickersToCSV(startDate, endDate string, filename string) error {
// Initialize the markets client

marketStatusResp, err := api.MarketStatus().From(startDate).To(endDate).Packed()
ctx := context.TODO()
marketStatusResp, err := api.MarketStatus().From(startDate).To(endDate).Packed(ctx)
if err != nil {
log.Fatalf("Failed to get market status: %v", err)
}
Expand Down Expand Up @@ -74,7 +76,7 @@ func SaveTickersToCSV(startDate, endDate string, filename string) error {
dateStr := date.Format("2006-01-02")

// Get the TickersResponse for the date
response, err := tickers.DateKey(dateStr).Packed()
response, err := tickers.DateKey(dateStr).Packed(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -125,7 +127,8 @@ func SaveSingleDayTickersToCSV(date time.Time, filename string) error {
dateStr := date.Format("2006-01-02")

// Get the TickersResponse for the date
response, err := tickers.DateKey(dateStr).Packed()
ctx := context.TODO()
response, err := tickers.DateKey(dateStr).Packed(ctx)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit bfa1fac

Please sign in to comment.