Handle more error response codes for bluelink api

This commit is contained in:
andig 2020-10-18 13:33:45 +02:00
parent 42dc963c95
commit 0a1f7caf55
2 changed files with 12 additions and 2 deletions

View file

@ -41,6 +41,16 @@ func (e StatusError) StatusCode() int {
return e.resp.StatusCode
}
// HasStatus returns true if the respose's status code matches any of the given codes
func (e StatusError) HasStatus(codes ...int) bool {
for _, code := range codes {
if e.resp.StatusCode == code {
return true
}
}
return false
}
// ReadBody reads HTTP response and returns error on response codes other than HTTP 2xx
func ReadBody(resp *http.Response) ([]byte, error) {
defer resp.Body.Close()

View file

@ -293,8 +293,8 @@ func (v *API) getStatus() (float64, error) {
err = v.DoJSON(req, &resp)
if err != nil {
resp := v.LastResponse()
if resp != nil && resp.StatusCode == http.StatusForbidden {
// handle http 401, 403
if se, ok := err.(request.StatusError); ok && se.HasStatus(http.StatusUnauthorized, http.StatusForbidden) {
err = errAuthFail
}
}