mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-18 14:54:23 +08:00
add config management commands
This commit is contained in:
43
cli/functions/http_client.go
Normal file
43
cli/functions/http_client.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package functions
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Request[T any](method, route string, payload any) *T {
|
||||
requestURL := "http://localhost:3000"
|
||||
var (
|
||||
req *http.Request
|
||||
err error
|
||||
)
|
||||
if payload == nil {
|
||||
req, err = http.NewRequest(method, requestURL+route, nil)
|
||||
} else {
|
||||
payloadBytes, jsonErr := json.Marshal(payload)
|
||||
if jsonErr != nil {
|
||||
log.Fatalf("Error in request JSON marshalling: %s", err)
|
||||
}
|
||||
req, err = http.NewRequest(method, requestURL+route, bytes.NewReader(payloadBytes))
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatalf("Client could not create request: %s", err)
|
||||
}
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Fatalf("Client error making http request: %s", err)
|
||||
}
|
||||
|
||||
resBodyBytes, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
log.Fatalf("Client could not read response body: %s", err)
|
||||
}
|
||||
body := new(T)
|
||||
if err := json.Unmarshal(resBodyBytes, body); err != nil {
|
||||
log.Fatalf("Error unmarshalling JSON: %s", err)
|
||||
}
|
||||
return body
|
||||
}
|
Reference in New Issue
Block a user