X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=capifcore%2Finternal%2Frestclient%2FHTTPClient.go;fp=capifcore%2Finternal%2Frestclient%2FHTTPClient.go;h=de0ad1d0ca26706802f1b598126f7d44beb18d82;hb=2effa31635f1b8349c02b4c1c546bfc67e8797d3;hp=0000000000000000000000000000000000000000;hpb=a6c6e4b1721a3cf88f927d098d32e1c11562309a;p=nonrtric%2Fplt%2Fsme.git diff --git a/capifcore/internal/restclient/HTTPClient.go b/capifcore/internal/restclient/HTTPClient.go new file mode 100644 index 0000000..de0ad1d --- /dev/null +++ b/capifcore/internal/restclient/HTTPClient.go @@ -0,0 +1,80 @@ +// - +// ========================LICENSE_START================================= +// O-RAN-SC +// %% +// Copyright (C) 2021: 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 restclient + +import ( + "bytes" + "fmt" + "io" + "net/http" +) + +const ContentTypeJSON = "application/json" +const ContentTypePlain = "text/plain" + +//go:generate mockery --name HTTPClient +type HTTPClient interface { + Do(*http.Request) (*http.Response, error) +} + +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 Put(url string, body []byte, client HTTPClient) error { + return do(http.MethodPut, url, body, ContentTypeJSON, 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 +}