Add Tronity support (#1268)
This commit is contained in:
parent
aa4ade84ce
commit
383d450637
12 changed files with 567 additions and 49 deletions
|
|
@ -56,6 +56,14 @@ func (e StatusError) HasStatus(codes ...int) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// ResponseError turns an HTTP status code into an error
|
||||
func ResponseError(resp *http.Response) error {
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return StatusError{resp: resp}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadBody reads HTTP response and returns error on response codes other than HTTP 2xx. It closes the request body after reading.
|
||||
func ReadBody(resp *http.Response) ([]byte, error) {
|
||||
defer resp.Body.Close()
|
||||
|
|
|
|||
|
|
@ -50,8 +50,8 @@ func (r *Helper) GetBody(url string) ([]byte, error) {
|
|||
|
||||
// decodeJSON reads HTTP response and decodes JSON body if error is nil
|
||||
func decodeJSON(resp *http.Response, res interface{}) error {
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return StatusError{resp: resp}
|
||||
if err := ResponseError(resp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return json.NewDecoder(resp.Body).Decode(&res)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue