|
package restapi
|
Generated documentation is available at:
https://pkg.go.dev/github.com/RedHatInsights/insights-operator-cli/restapi
Documentation in literate-programming-style is available at:
https://redhatinsights.github.io/insights-operator-cli/packages/restapi/utils.html
|
import (
"encoding/json"
"fmt"
"github.com/RedHatInsights/insights-operator-cli/types"
"io"
"log"
"net/http"
)
const (
communicationErrorWithServerErrorMessage = "Communication error with the server %v"
unableToReadResponseBodyError = "Unable to read response body"
)
|
performReadRequest function try to perform HTTP request using the HTTP GET
method and if the call is successful read the body of response.
|
func performReadRequest ( url string ) ( [ ] byte , error ) {
|
disable "G107 (CWE-88): Potential HTTP request made with variable url"
|
response , err := http . Get ( url )
if err != nil {
return nil , fmt . Errorf ( communicationErrorWithServerErrorMessage , err )
}
if response . StatusCode != http . StatusOK {
return nil , fmt . Errorf ( "Expected HTTP status 200 OK, got %d" , response . StatusCode )
}
body , readErr := io . ReadAll ( response . Body )
defer closeResponseBody ( response )
if readErr != nil {
return nil , fmt . Errorf ( unableToReadResponseBodyError )
}
return body , nil
}
|
performWriteRequest function try to perform HTTP request using the specified
HTTP method (POST, PUT, DELETE) and if the call is successful read the body
of response.
|
func performWriteRequest ( url , method string , payload io . Reader ) error {
var client http . Client
request , err := http . NewRequest ( method , url , payload )
if err != nil {
return fmt . Errorf ( "Error creating request %v" , err )
}
response , err := client . Do ( request )
if err != nil {
return fmt . Errorf ( communicationErrorWithServerErrorMessage , err )
}
if response . StatusCode != http . StatusOK && response . StatusCode != http . StatusCreated && response . StatusCode != http . StatusAccepted {
return fmt . Errorf ( "Expected HTTP status 200 OK, 201 Created or 202 Accepted, got %d" , response . StatusCode )
}
body , readErr := io . ReadAll ( response . Body )
defer closeResponseBody ( response )
if readErr != nil {
return fmt . Errorf ( unableToReadResponseBodyError )
}
return parseResponse ( body )
}
|
closeResponseBody function tries to close body of HTTP response with basic
error check
|
func closeResponseBody ( response * http . Response ) {
err := response . Body . Close ( )
if err != nil {
log . Println ( err )
}
}
|
parseResponse function tries to parse the body of HTTP response into JSON
structure that should contain at least one attribute stored under key
"Status".
|
func parseResponse ( body [ ] byte ) error {
resp := types . Response { }
err := json . Unmarshal ( body , & resp )
if err != nil {
return err
}
if resp . Status != "ok" {
return fmt . Errorf ( "Error response: %s" , resp . Status )
}
return nil
}
|