diff --git a/elk/global/readjson.go b/elk/global/readjson.go new file mode 100644 index 0000000000000000000000000000000000000000..edd3a6c6bec14c8b74657082baef5de232f67076 --- /dev/null +++ b/elk/global/readjson.go @@ -0,0 +1,38 @@ +package global + +import ( + "encoding/json" + "fmt" + "io" + "net/http" +) + +func ReadJSONResponse(r *http.Response, v any) error { + b, err := io.ReadAll(r.Body) + if err != nil { + return fmt.Errorf("reading response body: %w", err) + } + + if r.StatusCode != http.StatusOK { + return fmt.Errorf("response status: %d, body: %s", r.StatusCode, string(b)) + } + + err = json.Unmarshal(b, v) + if err != nil { + return fmt.Errorf("unmarshalling response json: %w", err) + } + return nil +} + +func ReadFileJSON(path string, v any) error { + bytes, err := FileReadBytes(path) + if err != nil { + return fmt.Errorf("reading file: %w", err) + } + + err = json.Unmarshal(bytes, v) + if err != nil { + return fmt.Errorf("unmarshalling file json: %w", err) + } + return nil +}