-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmarkets_status.go
275 lines (252 loc) · 11.6 KB
/
markets_status.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
// Package client includes types and methods to access the Market Status endpoint. Retrieve current, future, and historical open / closed status information.
//
// # Making Requests
//
// Utilize [MarketStatusRequest] to make requests to the endpoint through one of the three supported execution methods:
//
// | Method | Execution | Return Type | Description |
// |------------|---------------|-----------------------------|-----------------------------------------------------------------------------------------------------------------|
// | **Get** | Direct | `[]MarketStatusReport` | Directly returns a slice of `[]MarketStatusReport`, facilitating individual access to each market status entry. |
// | **Packed** | Intermediate | `*MarketStatusResponse` | Returns a packed `*MarketStatusResponse` object. Must be unpacked to access the `[]MarketStatusReport` slice. |
// | **Raw** | Low-level | `*resty.Response` | Offers the raw `*resty.Response` for utmost flexibility. Direct access to raw JSON or `*http.Response`. |
package client
import (
"context"
"fmt"
"github.com/MarketDataApp/sdk-go/helpers/parameters"
"github.com/MarketDataApp/sdk-go/models"
"github.com/go-resty/resty/v2"
)
// MarketStatusRequest represents a request to the [/v1/markets/status/] endpoint for market status information.
// It encapsulates parameters for country, and date to be used in the request.
// This struct provides methods such as Country(), Date(), From(), To(), and Countback() to set these parameters respectively.
//
// # Generated By
//
// - MarketStatus() *MarketStatusRequest: MarketStatus creates a new *MarketStatusRequest and returns a pointer to the request allowing for method chaining.
//
// # Setter Methods
//
// - Country(string) *MarketStatusRequest: Sets the country parameter for the request.
// - Date(interface{}) *MarketStatusRequest: Sets the date parameter for the request.
// - From(interface{}) *MarketStatusRequest: Sets the 'from' date parameter for the request.
// - To(interface{}) *MarketStatusRequest: Sets the 'to' date parameter for the request.
// - Countback(int) *MarketStatusRequest: Sets the countback parameter for the request.
//
// # Execution Methods
//
// These methods are used to send the request in different formats or retrieve the data.
// They handle the actual communication with the API endpoint.
//
// - Get() ([]MarketStatusReport, error): Sends the request, unpacks the response, and returns the data in a user-friendly format.
// - Packed() (*MarketStatusResponse, error): Returns a struct that contains equal-length slices of primitives. This packed response mirrors Market Data's JSON response.
// - Raw() (*resty.Response, error): Sends the request as is and returns the raw HTTP response.
//
// [/v1/markets/status/]: https://www.marketdata.app/docs/api/markets/status
type MarketStatusRequest struct {
*baseRequest
countryParams *parameters.CountryParams
universalParams *parameters.UniversalParams
dateParams *parameters.DateParams
}
// getParams packs the MarketStatusRequest struct into a slice of interface{} and returns it.
// This method is used to gather all the parameters set in the MarketStatusRequest into a single slice
// for easier manipulation and usage in subsequent requests.
//
// # Returns
//
// - []parameters.MarketDataParam: A slice containing all the parameters set in the MarketStatusRequest.
// - error: An error object indicating failure to pack the parameters, nil if successful.
func (msr *MarketStatusRequest) getParams() ([]parameters.MarketDataParam, error) {
if msr.countryParams == nil {
return nil, fmt.Errorf("required struct CountryParams doesn't exist")
}
if msr.universalParams == nil {
return nil, fmt.Errorf("required struct UniversalParams doesn't exist")
}
if msr.dateParams == nil {
return nil, fmt.Errorf("required struct DateParams doesn't exist")
}
params := []parameters.MarketDataParam{msr.countryParams, msr.universalParams, msr.dateParams}
return params, nil
}
// Country sets the country parameter of the MarketStatusRequest.
// This method is used to specify the country for which the market status is requested.
// It modifies the countryParams field of the MarketStatusRequest instance to store the country value.
//
// # Parameters
//
// - q: A string representing the country to be set.
//
// # Returns
//
// - *MarketStatusRequest: This method returns a pointer to the MarketStatusRequest instance it was called on. This allows for method chaining, where multiple setter methods can be called in a single statement. If the receiver (*MarketStatusRequest) is nil, it returns nil to prevent a panic.
func (msr *MarketStatusRequest) Country(q string) *MarketStatusRequest {
err := msr.countryParams.SetCountry(q)
if err != nil {
msr.baseRequest.Error = err
}
return msr
}
// Date sets the date parameter of the MarketStatusRequest.
// This method is used to specify the date for which the market status is requested.
// It modifies the dateParams field of the MarketStatusRequest instance to store the date value.
//
// # Parameters
//
// - interface{}: An interface{} that represents the starting date. It can be a string, a time.Time object, a Unix timestamp or any other type that the underlying dates package can process.
//
// # Returns
//
// - *MarketStatusRequest: This method returns a pointer to the MarketStatusRequest instance it was called on. This allows for method chaining. If the receiver (*MarketStatusRequest) is nil, it returns nil to prevent a panic.
func (msr *MarketStatusRequest) Date(q interface{}) *MarketStatusRequest {
err := msr.dateParams.SetDate(q)
if err != nil {
msr.baseRequest.Error = err
}
return msr
}
// From sets the 'from' date parameter of the MarketStatusRequest.
// This method is used to specify the starting date of the period for which the market status is requested.
// It modifies the dateParams field of the MarketStatusRequest instance to store the 'from' date value.
//
// # Parameters
//
// - interface{}: The 'from' date to be set.
//
// # Returns
//
// - *MarketStatusRequest: This method returns a pointer to the MarketStatusRequest instance it was called on. This allows for method chaining. If the receiver (*MarketStatusRequest) is nil, it returns nil to prevent a panic.
func (msr *MarketStatusRequest) From(q interface{}) *MarketStatusRequest {
err := msr.dateParams.SetFrom(q)
if err != nil {
msr.baseRequest.Error = err
}
return msr
}
// To sets the 'to' date parameter of the MarketStatusRequest.
// This method is used to specify the ending date of the period for which the market status is requested.
// It modifies the dateParams field of the MarketStatusRequest instance to store the 'to' date value.
//
// # Parameters
//
// - interface{}: The 'to' date to be set.
//
// # Returns
//
// - *MarketStatusRequest: This method returns a pointer to the MarketStatusRequest instance it was called on. This allows for method chaining. If the receiver (*MarketStatusRequest) is nil, it returns nil to prevent a panic.
func (msr *MarketStatusRequest) To(q interface{}) *MarketStatusRequest {
err := msr.dateParams.SetTo(q)
if err != nil {
msr.baseRequest.Error = err
}
return msr
}
// Countback sets the countback parameter for the MarketStatusRequest. It specifies the number of days to return, counting backwards from the 'to' date.
//
// # Parameters
//
// - int: The number of days to return before `to`.
//
// # Returns
//
// - *MarketStatusRequest: A pointer to the MarketStatusRequest instance to allow for method chaining.
func (msr *MarketStatusRequest) Countback(q int) *MarketStatusRequest {
err := msr.dateParams.SetCountback(q)
if err != nil {
msr.baseRequest.Error = err
}
return msr
}
// Raw executes the MarketStatusRequest with the provided context and returns the raw *resty.Response.
// The *resty.Response can be directly used to access the raw JSON or *http.Response for further processing.
//
// # Parameters
//
// - ctx context.Context: The context to use for the request execution.
//
// # Returns
//
// - *resty.Response: The raw HTTP response from the executed MarketStatusRequest.
// - error: An error object if the MarketStatusRequest is nil or if an error occurs during the request execution.
func (msr *MarketStatusRequest) Raw(ctx context.Context) (*resty.Response, error) {
return msr.baseRequest.Raw(ctx)
}
// Packed sends the MarketStatusRequest with the provided context and returns the MarketStatusResponse.
// This method checks if the MarketStatusRequest receiver is nil, returning an error if true.
// It proceeds to send the request and returns the MarketStatusResponse along with any error encountered during the request.
//
// # Parameters
//
// - ctx context.Context: The context to use for the request execution.
//
// # Returns
//
// - *models.MarketStatusResponse: A pointer to the *MarketStatusResponse obtained from the request.
// - error: An error object that indicates a failure in sending the request.
func (msr *MarketStatusRequest) Packed(ctx context.Context) (*models.MarketStatusResponse, error) {
if msr == nil {
return nil, fmt.Errorf("MarketStatusRequest is nil")
}
var msrResp models.MarketStatusResponse
_, err := msr.baseRequest.client.getFromRequest(ctx, msr.baseRequest, &msrResp)
if err != nil {
return nil, err
}
return &msrResp, nil
}
// Get sends the MarketStatusRequest with the provided context, unpacks the MarketStatusResponse, and returns a slice of MarketStatusReport.
// It returns an error if the request or unpacking fails. This method is crucial for obtaining the actual market status data
// from the market status request. The method first checks if the MarketStatusRequest receiver is nil, which would
// result in an error as the request cannot be sent. It then proceeds to send the request using the Packed method.
// Upon receiving the response, it unpacks the data into a slice of MarketStatusReport using the Unpack method from the response.
//
// # Parameters
//
// - ctx context.Context: The context to use for the request execution.
//
// # Returns
//
// - []models.MarketStatusReport: A slice of MarketStatusReport containing the unpacked market status data from the response.
// - error: An error object that indicates a failure in sending the request or unpacking the response.
func (msr *MarketStatusRequest) Get(ctx context.Context) ([]models.MarketStatusReport, error) {
if msr == nil {
return nil, fmt.Errorf("MarketStatusRequest is nil")
}
// Use the Packed method to make the request
msrResp, err := msr.Packed(ctx)
if err != nil {
return nil, err
}
// Unpack the data using the Unpack method in the response
data, err := msrResp.Unpack()
if err != nil {
return nil, err
}
return data, nil
}
// MarketStatus creates a new MarketStatusRequest. This function initializes the request
// with default parameters for country, universal, and date, and sets the request path based on
// the predefined endpoints for market status.
//
// # Returns
//
// - *MarketStatusRequest: A pointer to the newly created *MarketStatusRequest with default parameters.
func MarketStatus() *MarketStatusRequest {
baseReq := newBaseRequest()
msr := &MarketStatusRequest{
baseRequest: baseReq,
countryParams: ¶meters.CountryParams{},
universalParams: ¶meters.UniversalParams{},
dateParams: ¶meters.DateParams{},
}
baseReq.child = msr
msr.Country("US") // Set default country value to "US"
path, ok := endpoints[1]["markets"]["status"]
if !ok {
msr.baseRequest.Error = fmt.Errorf("path not found for market status")
return msr
}
msr.baseRequest.path = path
return msr
}