Skip to content

Commit bfa1fac

Browse files
committed
Added context support
1 parent 37583d2 commit bfa1fac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+584
-263
lines changed

.vscode/settings.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"cSpell.words": [
33
"bulkcandles",
44
"bulkquotes",
5-
"datekey"
5+
"datekey",
6+
"Ratelimit",
7+
"resty"
68
]
79
}

baseRequest.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
package client
4242

4343
import (
44+
"context"
4445
"fmt"
4546

4647
"github.com/MarketDataApp/sdk-go/helpers/parameters"
@@ -265,7 +266,7 @@ func (br *baseRequest) getError() error {
265266
//
266267
// - *resty.Response: The raw response from the executed request.
267268
// - error: An error object if the baseRequest is nil, or if an error occurs during the request execution.
268-
func (request *baseRequest) Raw() (*resty.Response, error) {
269+
func (request *baseRequest) Raw(ctx context.Context) (*resty.Response, error) {
269270
if request == nil {
270271
return nil, fmt.Errorf("baseRequest is nil")
271272
}
@@ -274,6 +275,6 @@ func (request *baseRequest) Raw() (*resty.Response, error) {
274275
return nil, fmt.Errorf("MarketDataClient is nil")
275276
}
276277

277-
response, err := request.client.getRawResponse(request)
278+
response, err := request.client.getRawResponse(ctx, request)
278279
return response, err
279280
}

client.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"strconv"
3535
"sync"
3636
"time"
37+
"context"
3738

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

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

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

389+
// Use the provided context for the request
390+
req = req.SetContext(ctx)
391+
388392
// Execute the GET request to the specified path.
389393
resp, err := req.Get(path)
390394
if err != nil {
@@ -419,9 +423,9 @@ func (c *MarketDataClient) prepareAndExecuteRequest(br *baseRequest, result inte
419423
//
420424
// - A pointer to a resty.Response object containing the response from the server.
421425
// - An error object if an error occurred during the request execution or if the response contains an error.
422-
func (c *MarketDataClient) getFromRequest(br *baseRequest, result interface{}) (*resty.Response, error) {
426+
func (c *MarketDataClient) getFromRequest(ctx context.Context, br *baseRequest, result interface{}) (*resty.Response, error) {
423427
// Execute the prepared request and capture the response and any error.
424-
resp, err := c.prepareAndExecuteRequest(br, result)
428+
resp, err := c.prepareAndExecuteRequest(ctx, br, result)
425429
if err != nil {
426430
// Return the response and the error if an error occurred during request execution.
427431
return resp, err
@@ -442,14 +446,15 @@ func (c *MarketDataClient) getFromRequest(br *baseRequest, result interface{}) (
442446
//
443447
// # Parameters
444448
//
449+
// - ctx: A context.Context object to control the request's lifecycle.
445450
// - br: A pointer to a baseRequest object containing the request details.
446451
//
447452
// # Returns
448453
//
449454
// - A pointer to a resty.Response object containing the raw response from the server.
450455
// - An error object if an error occurred during the request execution.
451-
func (c *MarketDataClient) getRawResponse(br *baseRequest) (*resty.Response, error) {
452-
return c.prepareAndExecuteRequest(br, nil)
456+
func (c *MarketDataClient) getRawResponse(ctx context.Context, br *baseRequest) (*resty.Response, error) {
457+
return c.prepareAndExecuteRequest(ctx, br, nil)
453458
}
454459

455460
// GetClient checks for an existing instance of MarketDataClient and returns it.

client_helper.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ const (
5050
)
5151

5252
// getRateLimitConsumed extracts the rate limit consumed value from the response headers.
53-
// It specifically looks for the "X-Api-RateLimit-Consumed" header and attempts to convert its value to an integer.
53+
// It specifically looks for the "X-Api-Ratelimit-Consumed" header and attempts to convert its value to an integer.
5454
//
5555
// # Parameters
5656
//
5757
// - resp: A pointer to a resty.Response from which the header will be extracted.
5858
//
5959
// # Returns
6060
//
61-
// - int: The integer value of the "X-Api-RateLimit-Consumed" header if present and successfully converted.
61+
// - int: The integer value of the "X-Api-Ratelimit-Consumed" header if present and successfully converted.
6262
// - error: An error if the header is missing or if the conversion to an integer fails.
6363
func getRateLimitConsumed(resp *resty.Response) (int, error) {
64-
rateLimitConsumedStr := resp.Header().Get("X-Api-RateLimit-Consumed")
64+
rateLimitConsumedStr := resp.Header().Get("X-Api-Ratelimit-Consumed")
6565
if rateLimitConsumedStr == "" {
66-
return 0, errors.New("error: missing 'x-Api-RateLimit-Consumed' header")
66+
return 0, errors.New("error: missing 'x-Api-Ratelimit-Consumed' header")
6767
}
6868
rateLimitConsumed, err := strconv.Atoi(rateLimitConsumedStr)
6969
if err != nil {

examples/examples.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package examples
22

33
import (
4+
"context"
45
"fmt"
56

67
api "github.com/MarketDataApp/sdk-go"
78
)
89

910
func RawHttpResponseExample() {
10-
resp, err := api.StockQuote().Symbol("AAPL").Raw()
11+
ctx := context.TODO()
12+
resp, err := api.StockQuote().Symbol("AAPL").Raw(ctx)
1113
if err != nil {
1214
fmt.Print(err)
1315
return
@@ -17,7 +19,8 @@ func RawHttpResponseExample() {
1719
}
1820

1921
func LogExample() {
20-
_, err := api.IndexQuotes().Symbol("VIX").FiftyTwoWeek(true).Get()
22+
ctx := context.TODO()
23+
_, err := api.IndexQuotes().Symbol("VIX").FiftyTwoWeek(true).Get(ctx)
2124
if err != nil {
2225
fmt.Print(err)
2326
return
@@ -28,8 +31,8 @@ func LogExample() {
2831
}
2932

3033
func MarketStatusExample() {
31-
32-
msr, err := api.MarketStatus().From("2022-01-01").To("2022-01-10").Packed()
34+
ctx := context.TODO()
35+
msr, err := api.MarketStatus().From("2022-01-01").To("2022-01-10").Packed(ctx)
3336
if err != nil {
3437
fmt.Print(err)
3538
return

examples/examples_indices.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package examples
22

33
import (
4+
"context"
45
"fmt"
56
"time"
67

78
api "github.com/MarketDataApp/sdk-go"
89
)
910

1011
func IndexQuoteExample() {
11-
iqe, err := api.IndexQuotes().Symbol("VIX").FiftyTwoWeek(true).Packed()
12+
ctx := context.TODO()
13+
iqe, err := api.IndexQuotes().Symbol("VIX").FiftyTwoWeek(true).Packed(ctx)
1214
if err != nil {
1315
fmt.Print(err)
1416
return
@@ -32,7 +34,8 @@ func IndexQuoteExample() {
3234

3335
func IndexCandlesExample() {
3436
oneWeekAgo := time.Now().AddDate(0, 0, -7)
35-
ice, err := api.IndexCandles().Resolution("D").Symbol("VIX").From(oneWeekAgo).To("today").Packed()
37+
ctx := context.TODO()
38+
ice, err := api.IndexCandles().Resolution("D").Symbol("VIX").From(oneWeekAgo).To("today").Packed(ctx)
3639
if err != nil {
3740
fmt.Print(err)
3841
return

examples/examples_options.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package examples
22

33
import (
4+
"context"
45
"fmt"
56

67
api "github.com/MarketDataApp/sdk-go"
78
)
89

910
func OptionsChainExample() {
10-
resp, err := api.OptionChain().UnderlyingSymbol("AAPL").Side("call").DTE(60).StrikeLimit(2).Get()
11+
ctx := context.TODO()
12+
resp, err := api.OptionChain().UnderlyingSymbol("AAPL").Side("call").DTE(60).StrikeLimit(2).Get(ctx)
1113
if err != nil {
1214
fmt.Print(err)
1315
return
@@ -20,7 +22,8 @@ func OptionsChainExample() {
2022
}
2123

2224
func OptionsQuotesExample() {
23-
resp, err := api.OptionQuote().OptionSymbol("AAPL250117C00150000").Get()
25+
ctx := context.TODO()
26+
resp, err := api.OptionQuote().OptionSymbol("AAPL250117C00150000").Get(ctx)
2427
if err != nil {
2528
fmt.Print(err)
2629
return
@@ -31,7 +34,8 @@ func OptionsQuotesExample() {
3134
}
3235

3336
func OptionStrikesExample() {
34-
resp, err := api.OptionStrikes().UnderlyingSymbol("AAPL").Get()
37+
ctx := context.TODO()
38+
resp, err := api.OptionStrikes().UnderlyingSymbol("AAPL").Get(ctx)
3539
if err != nil {
3640
fmt.Print(err)
3741
return
@@ -43,7 +47,8 @@ func OptionStrikesExample() {
4347
}
4448

4549
func OptionsLookupExample() {
46-
resp, err := api.OptionLookup().UserInput("AAPL 7/28/2023 200 Call").Get()
50+
ctx := context.TODO()
51+
resp, err := api.OptionLookup().UserInput("AAPL 7/28/2023 200 Call").Get(ctx)
4752
if err != nil {
4853
fmt.Print(err)
4954
return
@@ -53,7 +58,8 @@ func OptionsLookupExample() {
5358
}
5459

5560
func OptionsExpirationsExample() {
56-
resp, err := api.OptionsExpirations().UnderlyingSymbol("AAPL").Strike(200).Get()
61+
ctx := context.TODO()
62+
resp, err := api.OptionsExpirations().UnderlyingSymbol("AAPL").Strike(200).Get(ctx)
5763
if err != nil {
5864
fmt.Print(err)
5965
return

examples/examples_stocks.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package examples
22

33
import (
4+
"context"
45
"fmt"
56

67
api "github.com/MarketDataApp/sdk-go"
78
)
89

910
func StockQuoteExample() {
10-
sqe, err := api.StockQuote().Symbol("AAPL").FiftyTwoWeek(true).Packed()
11+
ctx := context.TODO()
12+
sqe, err := api.StockQuote().Symbol("AAPL").FiftyTwoWeek(true).Packed(ctx)
1113
if err != nil {
1214
fmt.Print(err)
1315
return
@@ -29,8 +31,8 @@ func StockQuoteExample() {
2931
}
3032

3133
func StockCandlesExample() {
32-
33-
sce, err := api.StockCandles().Resolution("1").Symbol("AAPL").From("2023-01-01").To("2023-01-04").Packed()
34+
ctx := context.TODO()
35+
sce, err := api.StockCandles().Resolution("1").Symbol("AAPL").From("2023-01-01").To("2023-01-04").Packed(ctx)
3436
if err != nil {
3537
fmt.Print(err)
3638
return
@@ -49,7 +51,8 @@ func StockCandlesExample() {
4951
}
5052

5153
func StockEarningsExample() {
52-
see, err := api.StockEarnings().Symbol("AAPL").From("2022-01-01").To("2022-12-31").Packed()
54+
ctx := context.TODO()
55+
see, err := api.StockEarnings().Symbol("AAPL").From("2022-01-01").To("2022-12-31").Packed(ctx)
5356
if err != nil {
5457
fmt.Print(err)
5558
return
@@ -71,7 +74,8 @@ func StockEarningsExample() {
7174
}
7275

7376
func StockNewsExample() {
74-
resp, err := api.StockNews().Symbol("AAPL").Get()
77+
ctx := context.TODO()
78+
resp, err := api.StockNews().Symbol("AAPL").Get(ctx)
7579
if err != nil {
7680
fmt.Print(err)
7781
return

examples/examples_v2.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package examples
22

33
import (
4+
"context"
45
"fmt"
56
"log"
67
"sort"
@@ -11,8 +12,8 @@ import (
1112
)
1213

1314
func StockCandlesV2Example() {
14-
15-
sce, err := api.StockCandlesV2().Resolution("1").Symbol("AAPL").DateKey("2023-01").Packed()
15+
ctx := context.TODO()
16+
sce, err := api.StockCandlesV2().Resolution("1").Symbol("AAPL").DateKey("2023-01").Packed(ctx)
1617
if err != nil {
1718
fmt.Print(err)
1819
return
@@ -30,7 +31,8 @@ func StockCandlesV2Example() {
3031
}
3132

3233
func StocksTickersV2Example() {
33-
tickers, err := api.StockTickers().DateKey("2023-01-05").Packed()
34+
ctx := context.TODO()
35+
tickers, err := api.StockTickers().DateKey("2023-01-05").Packed(ctx)
3436
if err != nil {
3537
fmt.Print(err)
3638
return
@@ -41,8 +43,8 @@ func StocksTickersV2Example() {
4143

4244
func SaveTickersToCSV(startDate, endDate string, filename string) error {
4345
// Initialize the markets client
44-
45-
marketStatusResp, err := api.MarketStatus().From(startDate).To(endDate).Packed()
46+
ctx := context.TODO()
47+
marketStatusResp, err := api.MarketStatus().From(startDate).To(endDate).Packed(ctx)
4648
if err != nil {
4749
log.Fatalf("Failed to get market status: %v", err)
4850
}
@@ -74,7 +76,7 @@ func SaveTickersToCSV(startDate, endDate string, filename string) error {
7476
dateStr := date.Format("2006-01-02")
7577

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

127129
// Get the TickersResponse for the date
128-
response, err := tickers.DateKey(dateStr).Packed()
130+
ctx := context.TODO()
131+
response, err := tickers.DateKey(dateStr).Packed(ctx)
129132
if err != nil {
130133
return err
131134
}

0 commit comments

Comments
 (0)