2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022: 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===================================
35 // HTTPClient interface
36 type HTTPClient interface {
37 Get(url string) (*http.Response, error)
39 Do(*http.Request) (*http.Response, error)
42 func CreateHttpClient(cert tls.Certificate, caCerts *x509.CertPool, timeout time.Duration) *http.Client {
45 Transport: createTransport(cert, caCerts),
49 type RequestError struct {
54 func (pe RequestError) Error() string {
55 return fmt.Sprintf("Request failed due to error response with status: %v and body: %v", pe.StatusCode, string(pe.Body))
58 func Post(url string, body []byte, contentType string, client HTTPClient) error {
59 return do(http.MethodPost, url, body, contentType, client)
62 func do(method string, url string, body []byte, contentType string, client HTTPClient) error {
63 if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil {
64 req.Header.Set("Content-Type", contentType)
65 if response, respErr := client.Do(req); respErr == nil {
66 if isResponseSuccess(response.StatusCode) {
69 return getRequestError(response)
79 func isResponseSuccess(statusCode int) bool {
80 return statusCode >= http.StatusOK && statusCode <= 299
83 func getRequestError(response *http.Response) RequestError {
84 defer response.Body.Close()
85 responseData, _ := io.ReadAll(response.Body)
86 putError := RequestError{
87 StatusCode: response.StatusCode,
93 func createTransport(cert tls.Certificate, caCerts *x509.CertPool) *http.Transport {
94 return &http.Transport{
95 TLSClientConfig: &tls.Config{
98 Certificates: []tls.Certificate{
101 InsecureSkipVerify: true,
106 func IsUrlSecure(configUrl string) bool {
107 u, _ := url.Parse(configUrl)
108 return u.Scheme == "https"