41 lines
676 B
Go
41 lines
676 B
Go
package client
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
type Error struct {
|
|
Err string `json:"error"`
|
|
Status int `json:"status"`
|
|
}
|
|
|
|
func (e Error) Error() string {
|
|
return e.Err
|
|
}
|
|
|
|
func (e Error) Is(target error) bool {
|
|
return e.Err == target.Error()
|
|
}
|
|
|
|
func decodeResp(resp *http.Response, v interface{}) error {
|
|
if resp.Body == nil {
|
|
return nil
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode == 200 {
|
|
if v == nil {
|
|
return nil
|
|
}
|
|
return json.NewDecoder(resp.Body).Decode(&v)
|
|
}
|
|
var e Error
|
|
err := json.NewDecoder(resp.Body).Decode(&e)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if resp.StatusCode >= 400 && resp.StatusCode <= 599 {
|
|
return e
|
|
}
|
|
return nil
|
|
}
|