// Copyright (C) 2023 Marius Schellenberger package client import ( "encoding/json" "net/http" ) type Error struct { Err string `json:"error"` Code int `json:"code"` } func (e Error) Error() string { return e.Err } func (e Error) Is(target error) bool { return e.Err == target.Error() } type Redirect struct { Location string Code int } func (r Redirect) Error() string { return "redirect: " + r.Location } var ErrRedirect = Redirect{} func decodeResp(resp *http.Response, v any) 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) } if resp.StatusCode >= 300 && resp.StatusCode < 400 { loc := resp.Header.Get("Location") return Redirect{loc, resp.StatusCode} } var e Error err := json.NewDecoder(resp.Body).Decode(&e) if err != nil { return err } if resp.StatusCode >= 400 && resp.StatusCode < 600 { return e } return nil }