feat: reduce usage of OpenSea api key

This commit is contained in:
Dario Gabriel Lipicar 2023-03-30 19:08:48 -03:00 committed by dlipicar
parent c791163136
commit f097d3430a
1 changed files with 14 additions and 2 deletions

View File

@ -154,6 +154,9 @@ func (o *HTTPClient) doGetRequest(url string, apiKey string) ([]byte, error) {
retryCount := 0
statusCode := http.StatusOK
// Try to do the request without an apiKey first
tmpAPIKey := ""
for {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
@ -162,8 +165,8 @@ func (o *HTTPClient) doGetRequest(url string, apiKey string) ([]byte, error) {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:96.0) Gecko/20100101 Firefox/96.0")
if len(apiKey) > 0 {
req.Header.Set("X-API-KEY", apiKey)
if len(tmpAPIKey) > 0 {
req.Header.Set("X-API-KEY", tmpAPIKey)
}
resp, err := o.client.Do(req)
@ -189,6 +192,15 @@ func (o *HTTPClient) doGetRequest(url string, apiKey string) ([]byte, error) {
continue
}
// break and error
case http.StatusForbidden:
// Request requires an apiKey, set it and retry
if tmpAPIKey == "" && apiKey != "" {
tmpAPIKey = apiKey
// sleep and retry
time.Sleep(GetRequestWaitTime)
continue
}
// break and error
default:
// break and error
}