-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_client.go
107 lines (98 loc) · 3.56 KB
/
filter_client.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
/*
* MIT License
*
* Copyright (c) 2024 tochemey
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package gofigi
import (
"context"
"fmt"
"net/http"
"time"
"github.com/carlmjohnson/requests"
"golang.org/x/time/rate"
"github.com/tochemey/gofigi/model"
)
// FilterClient implements the Filter API
// reference: https://www.openfigi.com/api#post-v3-filter
type FilterClient struct {
client
}
// NewDefaultFilterClient creates a new instance of FilterClient using the http.DefaultClient
func NewDefaultFilterClient(apiKey string) *FilterClient {
// create the instance of the SearchClient
return &FilterClient{
client: client{
httpClient: http.DefaultClient,
// let us set the rate limits according the API doc for 20 requests per minute
// reference: https://www.openfigi.com/api#rate-limit
limiter: rate.NewLimiter(rate.Every(1*time.Minute/20), 20),
apiKey: apiKey,
baseURL: baseURL,
},
}
}
// NewFilterClient creates a new instance of SearchClient with the given http.Client
func NewFilterClient(apiKey string, httpClient *http.Client) *FilterClient {
// create an instance of SearchClient
return &FilterClient{
client: client{
httpClient: httpClient,
// let us set the rate limits according the API doc for 20 requests per minute
// reference: https://www.openfigi.com/api#rate-limit
limiter: rate.NewLimiter(rate.Every(1*time.Minute), 20),
apiKey: apiKey,
baseURL: baseURL,
},
}
}
// Filter fetches the list of stocks' information using the polygon API across all exchanges
// reference: https://www.openfigi.com/api#post-v3-filter
func (c FilterClient) Filter(ctx context.Context, request *model.FilterRequest) (*model.FilterResponse, error) {
// make a copy of the request before using it
requestCopy := request
// create an instance of the search response
response := new(model.FilterResponse)
// build the http request
requestBuilder := requests.
URL(c.baseURL).
Client(c.httpClient).
Path(filterResourcePath).
Method(http.MethodPost).
Header("X-OPENFIGI-APIKEY", c.apiKey).
Header("Content-Type", "application/json; charset=utf-8").
Header("Accept", "application/json; charset=utf-8").
BodyJSON(requestCopy).
ToJSON(response).
CheckStatus(http.StatusOK, http.StatusTooManyRequests)
// This is a blocking call. Honors the rate limit
err := c.limiter.Wait(ctx)
if err != nil {
return nil, fmt.Errorf("rate limiter reached: %w", err)
}
// execute the request and handle the error
if err = requestBuilder.Fetch(ctx); err != nil {
return nil, err
}
// return the response
return response, nil
}