-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstocks_candles_v2.go
136 lines (118 loc) · 3.74 KB
/
stocks_candles_v2.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
package client
import (
"context"
"fmt"
"github.com/MarketDataApp/sdk-go/helpers/parameters"
"github.com/MarketDataApp/sdk-go/models"
)
// StockCandlesRequestV2 represents a request to the /v2/stocks/candles endpoint.
type StockCandlesRequestV2 struct {
*baseRequest
dateKey *parameters.DateKeyParam
resolutionParams *parameters.ResolutionParams
symbolParams *parameters.SymbolParams
}
// Resolution sets the resolution parameter for the CandlesRequest.
func (cr *StockCandlesRequestV2) Resolution(q string) *StockCandlesRequestV2 {
if cr == nil {
return nil
}
err := cr.resolutionParams.SetResolution(q)
if err != nil {
cr.Error = err
}
return cr
}
// Symbol sets the symbol parameter for the CandlesRequest.
func (cr *StockCandlesRequestV2) Symbol(q string) *StockCandlesRequestV2 {
if cr == nil {
return nil
}
err := cr.symbolParams.SetSymbol(q)
if err != nil {
cr.Error = err
}
return cr
}
// Date sets the date parameter for the CandlesRequest.
func (cr *StockCandlesRequestV2) DateKey(q string) *StockCandlesRequestV2 {
if cr == nil {
return nil
}
err := cr.dateKey.SetDateKey(q)
if err != nil {
cr.Error = err
}
return cr
}
// GetParams packs the CandlesRequest struct into a slice of interface{} and returns it.
func (cr *StockCandlesRequestV2) getParams() ([]parameters.MarketDataParam, error) {
if cr == nil {
return nil, fmt.Errorf("CandlesRequest is nil")
}
params := []parameters.MarketDataParam{cr.dateKey, cr.resolutionParams, cr.symbolParams}
return params, nil
}
// Packed sends the StockCandlesRequestV2 with the provided context and returns the StockCandlesResponse.
//
// # Parameters
//
// - ctx context.Context: The context to use for the request execution.
//
// # Returns
//
// - *models.StockCandlesResponse: A pointer to the StockCandlesResponse obtained from the request.
// - error: An error object that indicates a failure in sending the request.
func (scrV2 *StockCandlesRequestV2) Packed(ctx context.Context) (*models.StockCandlesResponse, error) {
if scrV2 == nil {
return nil, fmt.Errorf("StockCandlesRequestV2 is nil")
}
var scrResp models.StockCandlesResponse
_, err := scrV2.baseRequest.client.getFromRequest(ctx, scrV2.baseRequest, &scrResp)
if err != nil {
return nil, err
}
return &scrResp, nil
}
// Get sends the StockCandlesRequestV2 with the provided context, unpacks the StockCandlesResponse, and returns a slice of StockCandle.
// It returns an error if the request or unpacking fails.
//
// # Parameters
//
// - ctx context.Context: The context to use for the request execution.
//
// # Returns
//
// - []models.StockCandle: A slice of []Candle containing the unpacked candle data from the response.
// - error: An error object that indicates a failure in sending the request or unpacking the response.
func (scrV2 *StockCandlesRequestV2) Get(ctx context.Context) ([]models.Candle, error) {
if scrV2 == nil {
return nil, fmt.Errorf("StockCandlesRequestV2 is nil")
}
// Use the Packed method to make the request
scrResp, err := scrV2.Packed(ctx)
if err != nil {
return nil, err
}
// Unpack the data using the Unpack method in the response
data, err := scrResp.Unpack()
if err != nil {
return nil, err
}
return data, nil
}
// StockCandles creates a new CandlesRequest and associates it with the provided client.
// If no client is provided, it uses the default client.
func StockCandlesV2() *StockCandlesRequestV2 {
baseReq := newBaseRequest()
baseReq.path = endpoints[2]["stocks"]["candles"]
cr := &StockCandlesRequestV2{
baseRequest: baseReq,
dateKey: ¶meters.DateKeyParam{},
resolutionParams: ¶meters.ResolutionParams{},
symbolParams: ¶meters.SymbolParams{},
}
// Set the date to the current time
baseReq.child = cr
return cr
}