status-go/services/wallet/thirdparty/http_client.go

99 lines
1.9 KiB
Go
Raw Normal View History

package thirdparty
2024-04-01 13:39:17 +00:00
import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"net/http"
netUrl "net/url"
"time"
)
const requestTimeout = 5 * time.Second
const maxNumOfRequestRetries = 5
2024-04-01 13:39:17 +00:00
type BasicCreds struct {
User string
Password string
}
2024-04-01 13:39:17 +00:00
type HTTPClient struct {
client *http.Client
}
func NewHTTPClient() *HTTPClient {
return &HTTPClient{
client: &http.Client{
Timeout: requestTimeout,
},
}
}
func (c *HTTPClient) DoGetRequest(ctx context.Context, url string, params netUrl.Values, creds *BasicCreds) ([]byte, error) {
2024-04-01 13:39:17 +00:00
if len(params) > 0 {
url = url + "?" + params.Encode()
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
if creds != nil {
req.SetBasicAuth(creds.User, creds.Password)
}
var resp *http.Response
for i := 0; i < maxNumOfRequestRetries; i++ {
resp, err = c.client.Do(req)
if err == nil || i == maxNumOfRequestRetries-1 {
break
}
time.Sleep(200 * time.Millisecond)
}
2024-04-01 13:39:17 +00:00
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func (c *HTTPClient) DoPostRequest(ctx context.Context, url string, params map[string]interface{}, creds *BasicCreds) ([]byte, error) {
2024-04-01 13:39:17 +00:00
jsonData, err := json.Marshal(params)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, err
}
if creds != nil {
req.SetBasicAuth(creds.User, creds.Password)
}
2024-04-01 13:39:17 +00:00
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")
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}