Fetch of authorization token
[nonrtric.git] / auth-token-fetch / HTTPClient.go
index a765461..5b0d167 100644 (file)
 package main
 
 import (
-       "bytes"
        "crypto/tls"
        "crypto/x509"
-       "fmt"
-       "io"
 
        "net/http"
-       "net/url"
        "time"
 )
 
-// HTTPClient interface
-type HTTPClient interface {
-       Get(url string) (*http.Response, error)
-
-       Do(*http.Request) (*http.Response, error)
-}
-
 func CreateHttpClient(cert tls.Certificate, caCerts *x509.CertPool, timeout time.Duration) *http.Client {
        return &http.Client{
                Timeout:   timeout,
@@ -46,50 +35,6 @@ func CreateHttpClient(cert tls.Certificate, caCerts *x509.CertPool, timeout time
        }
 }
 
-type RequestError struct {
-       StatusCode int
-       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 Post(url string, body []byte, contentType string, client HTTPClient) error {
-       return do(http.MethodPost, url, body, contentType, client)
-}
-
-func do(method string, url string, body []byte, contentType string, client HTTPClient) error {
-       if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil {
-               req.Header.Set("Content-Type", contentType)
-               if response, respErr := client.Do(req); respErr == nil {
-                       if isResponseSuccess(response.StatusCode) {
-                               return nil
-                       } else {
-                               return getRequestError(response)
-                       }
-               } else {
-                       return respErr
-               }
-       } else {
-               return reqErr
-       }
-}
-
-func isResponseSuccess(statusCode int) bool {
-       return statusCode >= http.StatusOK && statusCode <= 299
-}
-
-func getRequestError(response *http.Response) RequestError {
-       defer response.Body.Close()
-       responseData, _ := io.ReadAll(response.Body)
-       putError := RequestError{
-               StatusCode: response.StatusCode,
-               Body:       responseData,
-       }
-       return putError
-}
-
 func createTransport(cert tls.Certificate, caCerts *x509.CertPool) *http.Transport {
        return &http.Transport{
                TLSClientConfig: &tls.Config{
@@ -102,8 +47,3 @@ func createTransport(cert tls.Certificate, caCerts *x509.CertPool) *http.Transpo
                },
        }
 }
-
-func IsUrlSecure(configUrl string) bool {
-       u, _ := url.Parse(configUrl)
-       return u.Scheme == "https"
-}