2 // ========================LICENSE_START=================================
5 // Copyright (C) 2021: Nordix Foundation
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
33 "github.com/hashicorp/go-retryablehttp"
36 // HTTPClient interface
37 type HTTPClient interface {
38 Get(url string) (*http.Response, error)
40 Do(*http.Request) (*http.Response, error)
43 type RequestError struct {
48 func (pe RequestError) Error() string {
49 return fmt.Sprintf("Request failed due to error response with status: %v and body: %v", pe.StatusCode, string(pe.Body))
52 func Get(url string, client HTTPClient) ([]byte, error) {
53 if response, err := client.Get(url); err == nil {
54 if isResponseSuccess(response.StatusCode) {
55 defer response.Body.Close()
56 if responseData, err := io.ReadAll(response.Body); err == nil {
57 return responseData, nil
62 return nil, getRequestError(response)
69 func Put(url string, body []byte, client HTTPClient) error {
70 return do(http.MethodPut, url, body, client)
73 func Post(url string, body []byte, client HTTPClient) error {
74 return do(http.MethodPost, url, body, client)
77 func do(method string, url string, body []byte, client HTTPClient) error {
78 if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil {
79 req.Header.Set("Content-Type", "application/json")
80 if response, respErr := client.Do(req); respErr == nil {
81 if isResponseSuccess(response.StatusCode) {
84 return getRequestError(response)
94 func isResponseSuccess(statusCode int) bool {
95 return statusCode >= http.StatusOK && statusCode <= 299
98 func getRequestError(response *http.Response) RequestError {
99 defer response.Body.Close()
100 responseData, _ := io.ReadAll(response.Body)
101 putError := RequestError{
102 StatusCode: response.StatusCode,
108 func CreateClientCertificate(certPath string, keyPath string) (tls.Certificate, error) {
109 if cert, err := tls.LoadX509KeyPair(certPath, keyPath); err == nil {
112 return tls.Certificate{}, fmt.Errorf("cannot create x509 keypair from cert file %s and key file %s due to: %v", certPath, keyPath, err)
116 func CreateRetryClient(cert tls.Certificate) *http.Client {
117 rawRetryClient := retryablehttp.NewClient()
118 rawRetryClient.RetryWaitMax = time.Minute
119 rawRetryClient.RetryMax = math.MaxInt
120 rawRetryClient.HTTPClient.Transport = getSecureTransportWithoutVerify(cert)
122 client := rawRetryClient.StandardClient()
126 func CreateClientWithoutRetry(cert tls.Certificate, timeout time.Duration) *http.Client {
129 Transport: getSecureTransportWithoutVerify(cert),
133 func getSecureTransportWithoutVerify(cert tls.Certificate) *http.Transport {
134 return &http.Transport{
135 TLSClientConfig: &tls.Config{
136 Certificates: []tls.Certificate{
139 InsecureSkipVerify: true,
144 func IsUrlSecure(configUrl string) bool {
145 u, _ := url.Parse(configUrl)
146 return u.Scheme == "https"