import (
"bytes"
+ "crypto/tls"
"fmt"
"io"
+ "math"
"net/http"
+ "net/url"
"time"
+
+ "github.com/hashicorp/go-retryablehttp"
)
type RequestError struct {
Body []byte
}
-func (pe RequestError) Error() string {
- return fmt.Sprintf("Request failed due to error response with status: %v and body: %v", pe.StatusCode, string(pe.Body))
+func (e RequestError) Error() string {
+ return fmt.Sprintf("error response with status: %v and body: %v", e.StatusCode, string(e.Body))
}
// HTTPClient interface
Do(*http.Request) (*http.Response, error)
}
-var (
- Client HTTPClient
-)
+func PutWithoutAuth(url string, body []byte, client HTTPClient) error {
+ return do(http.MethodPut, url, body, client)
+}
-func init() {
- Client = &http.Client{
- Timeout: time.Second * 5,
- }
+func Put(url string, body string, client HTTPClient, userName string, password string) error {
+ return do(http.MethodPut, url, []byte(body), client, userName, password)
}
-func Get(url string) ([]byte, error) {
- if response, err := Client.Get(url); err == nil {
- if isResponseSuccess(response.StatusCode) {
- defer response.Body.Close()
- if responseData, err := io.ReadAll(response.Body); err == nil {
- return responseData, nil
- } else {
- return nil, err
- }
- } else {
- return nil, getResponseError(response)
- }
+func Delete(url string, client HTTPClient) error {
+ return do(http.MethodDelete, url, nil, client)
+}
+
+func CreateClientCertificate(certPath string, keyPath string) (tls.Certificate, error) {
+ if cert, err := tls.LoadX509KeyPair(certPath, keyPath); err == nil {
+ return cert, nil
} else {
- return nil, err
+ return tls.Certificate{}, fmt.Errorf("cannot create x509 keypair from cert file %s and key file %s due to: %v", certPath, keyPath, err)
}
}
-func PutWithoutAuth(url string, body []byte) error {
- return do(http.MethodPut, url, body)
+func CreateRetryClient(cert tls.Certificate) *http.Client {
+ rawRetryClient := retryablehttp.NewClient()
+ rawRetryClient.RetryWaitMax = time.Minute
+ rawRetryClient.RetryMax = math.MaxInt
+ rawRetryClient.HTTPClient.Transport = getSecureTransportWithoutVerify(cert)
+
+ client := rawRetryClient.StandardClient()
+ return client
}
-func Put(url string, body string, userName string, password string) error {
- return do(http.MethodPut, url, []byte(body), userName, password)
+func IsUrlSecure(configUrl string) bool {
+ u, _ := url.Parse(configUrl)
+ return u.Scheme == "https"
}
-func Delete(url string) error {
- return do(http.MethodDelete, url, nil)
+func getSecureTransportWithoutVerify(cert tls.Certificate) *http.Transport {
+ return &http.Transport{
+ TLSClientConfig: &tls.Config{
+ Certificates: []tls.Certificate{
+ cert,
+ },
+ InsecureSkipVerify: true,
+ },
+ }
}
-func do(method string, url string, body []byte, userInfo ...string) error {
+func do(method string, url string, body []byte, client HTTPClient, userInfo ...string) error {
if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil {
if body != nil {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
if len(userInfo) > 0 {
req.SetBasicAuth(userInfo[0], userInfo[1])
}
- if response, respErr := Client.Do(req); respErr == nil {
+ if response, respErr := client.Do(req); respErr == nil {
if isResponseSuccess(response.StatusCode) {
return nil
} else {