Change of ECS to ICS in test env
[nonrtric.git] / dmaap-mediator-producer / internal / restclient / HTTPClient.go
index a783f7e..8ccd4b2 100644 (file)
@@ -22,9 +22,15 @@ package restclient
 
 import (
        "bytes"
+       "crypto/tls"
        "fmt"
        "io"
+       "math"
        "net/http"
+       "net/url"
+       "time"
+
+       "github.com/hashicorp/go-retryablehttp"
 )
 
 // HTTPClient interface
@@ -43,47 +49,35 @@ 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))
 }
 
-var (
-       Client HTTPClient
-)
-
-func init() {
-       Client = &http.Client{}
-}
-
-func Get(url string) ([]byte, error) {
-       if response, err := Client.Get(url); err == nil {
-               defer response.Body.Close()
-               if responseData, err := io.ReadAll(response.Body); err == nil {
-                       if isResponseSuccess(response.StatusCode) {
+func Get(url string, client HTTPClient) ([]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 {
-                               requestError := RequestError{
-                                       StatusCode: response.StatusCode,
-                                       Body:       responseData,
-                               }
-                               return nil, requestError
+                               return nil, err
                        }
                } else {
-                       return nil, err
+                       return nil, getRequestError(response)
                }
        } else {
                return nil, err
        }
 }
 
-func Put(url string, body []byte) error {
-       return do(http.MethodPut, url, body)
+func Put(url string, body []byte, client HTTPClient) error {
+       return do(http.MethodPut, url, body, client)
 }
 
-func Post(url string, body []byte) error {
-       return do(http.MethodPost, url, body)
+func Post(url string, body []byte, client HTTPClient) error {
+       return do(http.MethodPost, url, body, client)
 }
 
-func do(method string, url string, body []byte) error {
+func do(method string, url string, body []byte, client HTTPClient) error {
        if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil {
-               req.Header.Set("Content-Type", "application/json; charset=utf-8")
-               if response, respErr := Client.Do(req); respErr == nil {
+               req.Header.Set("Content-Type", "application/json")
+               if response, respErr := client.Do(req); respErr == nil {
                        if isResponseSuccess(response.StatusCode) {
                                return nil
                        } else {
@@ -110,3 +104,44 @@ func getRequestError(response *http.Response) RequestError {
        }
        return putError
 }
+
+func CreateClientCertificate(certPath string, keyPath string) (tls.Certificate, error) {
+       if cert, err := tls.LoadX509KeyPair(certPath, keyPath); err == nil {
+               return cert, nil
+       } else {
+               return tls.Certificate{}, fmt.Errorf("cannot create x509 keypair from cert file %s and key file %s due to: %v", certPath, keyPath, err)
+       }
+}
+
+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 CreateClientWithoutRetry(cert tls.Certificate, timeout time.Duration) *http.Client {
+       return &http.Client{
+               Timeout:   timeout,
+               Transport: getSecureTransportWithoutVerify(cert),
+       }
+}
+
+func getSecureTransportWithoutVerify(cert tls.Certificate) *http.Transport {
+       return &http.Transport{
+               TLSClientConfig: &tls.Config{
+                       Certificates: []tls.Certificate{
+                               cert,
+                       },
+                       InsecureSkipVerify: true,
+               },
+       }
+}
+
+func IsUrlSecure(configUrl string) bool {
+       u, _ := url.Parse(configUrl)
+       return u.Scheme == "https"
+}