Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mberbero committed Apr 17, 2023
1 parent 28c7380 commit f8a171b
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions kervan.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func (t *API) get(path string, payload interface{}, response interface{}, header
return t.do(req, response)
}

type ErrorResponse struct {
Error string `json:"error"`
}

func (t *API) do(req *http.Request, response interface{}) error {
req.Header.Set("Authorization", "Bearer "+t.Token)
client := &http.Client{
Expand All @@ -80,6 +84,14 @@ func (t *API) do(req *http.Request, response interface{}) error {
decode := json.NewDecoder(bytes.NewReader(body))
decode.DisallowUnknownFields()
decode.UseNumber()
if resp.StatusCode != http.StatusOK {
var errorResponse ErrorResponse
if err := decode.Decode(&errorResponse); err != nil {
return err
}
return fmt.Errorf("error while calling api: %s", errorResponse.Error)
}

if err := decode.Decode(response); err != nil {
return err
}
Expand All @@ -103,8 +115,7 @@ func (t *API) GetVersion() (*GetVersionResponse, error) {
headers := map[string]string{
"x-ktp": t.Token,
}
err := t.get("/vlcs/version", nil, &response, headers)
if err != nil {
if err := t.get("/vlcs/version", nil, &response, headers); err != nil {
return nil, fmt.Errorf("error while getting version: %w", err)
}
return &response, nil
Expand Down

0 comments on commit f8a171b

Please sign in to comment.