-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbaseRequest.go
280 lines (244 loc) · 6.66 KB
/
baseRequest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Package client provides a Go SDK for interacting with the Market Data API.
// It includes functionality for making API requests, handling responses,
// managing rate limits, and logging. The SDK supports various data types
// including stocks, options, and market status information.
//
// # Usage
//
// To use the SDK, you first need to create an instance of MarketDataClient
// using the GetClient function. This client will then be used to make
// API requests to the Market Data API.
//
// # Example
//
// client := GetClient()
// client.Debug(true) // Enable debug mode to log detailed request and response information
// quote, err := client.StockQuotes().Symbol("AAPL").Get()
//
// # Authentication
//
// The SDK uses an API token for authentication. The token can be set as an
// environment variable (MARKETDATA_TOKEN) or directly in your code. However,
// storing tokens in your code is not recommended for security reasons.
//
// # Rate Limiting
//
// The MarketDataClient automatically tracks and manages the API's rate limits.
// You can check if the rate limit has been exceeded with the RateLimitExceeded method.
//
// # Logging
//
// The SDK logs all unsuccessful (400-499 and 500-599) responses to specific log files
// based on the response status code. Successful responses (200-299) are logged when
// debug mode is enabled. Logs include detailed information such as request and response
// headers, response status code, and the response body.
//
// # Debug Mode
//
// Debug mode can be enabled by calling the Debug method on the MarketDataClient instance.
// When enabled, the SDK will log detailed information about each request and response,
// which is useful for troubleshooting.
package client
import (
"context"
"fmt"
"github.com/MarketDataApp/sdk-go/helpers/parameters"
"github.com/go-resty/resty/v2"
)
type MarketDataPacked interface {
IsValid() bool
Unpack() any
}
// baseRequest is a struct that represents a basic request in the Market Data Client package.
// It contains a request of type *resty.Request, a path of type string, a client of type *MarketDataClient,
// a child of type any, and an Error of type error.
type baseRequest struct {
request *resty.Request
path string
client *MarketDataClient
child any
Error error
}
// getParams calls the getParams method of the appropriate MarketDataRequest.
func (br *baseRequest) getParams() ([]parameters.MarketDataParam, error) {
if br == nil || br.child == nil {
return []parameters.MarketDataParam{}, nil
}
// Check if child is of type *baseRequest
if _, ok := br.child.(*baseRequest); ok {
return nil, fmt.Errorf("child is of type *baseRequest, stopping recursion")
}
if msr, ok := br.child.(*MarketStatusRequest); ok {
params, err := msr.getParams()
if err != nil {
return nil, err
}
return params, nil
}
if tr, ok := br.child.(*StockTickersRequestV2); ok {
params, err := tr.getParams()
if err != nil {
return nil, err
}
return params, nil
}
if scr, ok := br.child.(*StockCandlesRequest); ok {
params, err := scr.getParams()
if err != nil {
return nil, err
}
return params, nil
}
if sbcr, ok := br.child.(*BulkStockCandlesRequest); ok {
params, err := sbcr.getParams()
if err != nil {
return nil, err
}
return params, nil
}
if scr, ok := br.child.(*StockCandlesRequestV2); ok {
params, err := scr.getParams()
if err != nil {
return nil, err
}
return params, nil
}
if sqr, ok := br.child.(*StockQuoteRequest); ok {
params, err := sqr.getParams()
if err != nil {
return nil, err
}
return params, nil
}
if bsqr, ok := br.child.(*BulkStockQuotesRequest); ok {
params, err := bsqr.getParams()
if err != nil {
return nil, err
}
return params, nil
}
if snr, ok := br.child.(*StockNewsRequest); ok {
params, err := snr.getParams()
if err != nil {
return nil, err
}
return params, nil
}
if iqr, ok := br.child.(*IndexQuoteRequest); ok {
params, err := iqr.getParams()
if err != nil {
return nil, err
}
return params, nil
}
if ser, ok := br.child.(*StockEarningsRequest); ok {
params, err := ser.getParams()
if err != nil {
return nil, err
}
return params, nil
}
if icr, ok := br.child.(*IndicesCandlesRequest); ok {
params, err := icr.getParams()
if err != nil {
return nil, err
}
return params, nil
}
if oer, ok := br.child.(*OptionsExpirationsRequest); ok {
params, err := oer.getParams()
if err != nil {
return nil, err
}
return params, nil
}
if olr, ok := br.child.(*OptionLookupRequest); ok {
params, err := olr.getParams()
if err != nil {
return nil, err
}
return params, nil
}
if osr, ok := br.child.(*OptionStrikesRequest); ok {
params, err := osr.getParams()
if err != nil {
return nil, err
}
return params, nil
}
if oqr, ok := br.child.(*OptionQuoteRequest); ok {
params, err := oqr.getParams()
if err != nil {
return nil, err
}
return params, nil
}
if ocr, ok := br.child.(*OptionChainRequest); ok {
params, err := ocr.getParams()
if err != nil {
return nil, err
}
return params, nil
}
if fcr, ok := br.child.(*FundCandlesRequest); ok {
params, err := fcr.getParams()
if err != nil {
return nil, err
}
return params, nil
}
return []parameters.MarketDataParam{}, nil
}
// getPath returns the path of the BaseRequest.
// It returns an error if the BaseRequest is nil.
func (br *baseRequest) getPath() (string, error) {
if br == nil {
return "", fmt.Errorf("path is nil")
}
if br.path == "" {
return "", fmt.Errorf("path is empty")
}
return br.path, nil
}
// getResty returns the resty.Request for the BaseRequest.
func (br *baseRequest) getResty() *resty.Request {
return br.request
}
func newBaseRequest(clients ...*MarketDataClient) *baseRequest {
var mdClient *MarketDataClient
var err error
if len(clients) > 0 {
mdClient = clients[0]
} else {
mdClient, err = GetClient()
if mdClient == nil {
return nil
}
}
baseReq := &baseRequest{
request: mdClient.R(),
client: mdClient,
Error: err,
}
return baseReq
}
// getError returns the error of the BaseRequest.
func (br *baseRequest) getError() error {
return br.Error
}
// Raw executes the request and returns the raw resty.Response.
//
// # Returns
//
// - *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(ctx context.Context) (*resty.Response, error) {
if request == nil {
return nil, fmt.Errorf("baseRequest is nil")
}
if request.client == nil {
return nil, fmt.Errorf("MarketDataClient is nil")
}
response, err := request.client.getRawResponse(ctx, request)
return response, err
}