X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=provider%2Fhandler%2Fcommon_handler.go;fp=provider%2Fhandler%2Fcommon_handler.go;h=73952ee42eed7a076e38948bd13e2c096e915d73;hb=b873255143602920b7b0728392fbd8fe304b73d3;hp=0000000000000000000000000000000000000000;hpb=777987abe351d45394cda0404f863147e83cb53e;p=nonrtric%2Fplt%2Fsme.git diff --git a/provider/handler/common_handler.go b/provider/handler/common_handler.go new file mode 100644 index 0000000..73952ee --- /dev/null +++ b/provider/handler/common_handler.go @@ -0,0 +1,81 @@ +// - +// +// ========================LICENSE_START================================= +// O-RAN-SC +// %% +// Copyright (C) 2023: Nordix Foundation +// %% +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ========================LICENSE_END=================================== +package handler + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" +) + +func makeRequest(method, url string, headers map[string]string, data interface{}) ([]byte, error) { + client := &http.Client{} + + // Create a new HTTP request with the specified method and URL + req, err := http.NewRequest(method, url, nil) + if err != nil { + return nil, err + } + + // Set any headers specified in the headers map + for k, v := range headers { + req.Header.Set(k, v) + } + + // If there is data to send, marshal it to JSON and set it as the request body + if data != nil { + jsonBytes, err := json.Marshal(data) + if err != nil { + return nil, err + } + req.Body = io.NopCloser(bytes.NewReader(jsonBytes)) + } + + // Send the request and get the response + if resp, err := client.Do(req); err == nil { + if isResponseSuccess(resp.StatusCode) { + defer resp.Body.Close() + + // Read the response body + respBody, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + return respBody, nil + } else { + return nil, getRequestError(resp) + } + } else { + return nil, err + } +} + +func isResponseSuccess(statusCode int) bool { + return statusCode >= http.StatusOK && statusCode <= 299 +} + +func getRequestError(response *http.Response) error { + defer response.Body.Close() + responseData, _ := io.ReadAll(response.Body) + + return fmt.Errorf("message: %v code: %v", string(responseData), response.StatusCode) +}