http client: split decoding of api status and payload

This commit is contained in:
Alexandre Blazart 2021-03-04 08:40:13 +01:00
parent 6f830aada4
commit 0579c4ff84
3 changed files with 37 additions and 41 deletions

View file

@ -582,14 +582,14 @@ func NewCollector(filename string, discovery fbx.FreeboxDiscovery, hostDetails,
if r, err := os.Open(filename); err == nil {
log.Info.Println("Use configuration file", filename)
defer r.Close()
result.freebox, err = fbx.NewFreeboxConnectionFromConfig(debug, r)
result.freebox, err = fbx.NewFreeboxConnectionFromConfig(r)
if err != nil {
panic(err)
}
} else {
log.Info.Println("Could not find the configuration file", filename)
newConfig = true
result.freebox, err = fbx.NewFreeboxConnectionFromServiceDiscovery(debug, discovery)
result.freebox, err = fbx.NewFreeboxConnectionFromServiceDiscovery(discovery)
if err != nil {
panic(err)
}

View file

@ -21,8 +21,8 @@ type FreeboxConnection struct {
* FreeboxConnection
*/
func NewFreeboxConnectionFromServiceDiscovery(debug bool, discovery FreeboxDiscovery) (*FreeboxConnection, error) {
client := NewFreeboxHttpClient(debug)
func NewFreeboxConnectionFromServiceDiscovery(discovery FreeboxDiscovery) (*FreeboxConnection, error) {
client := NewFreeboxHttpClient()
apiVersion, err := NewFreeboxAPIVersion(client, discovery)
if err != nil {
return nil, err
@ -46,8 +46,8 @@ func NewFreeboxConnectionFromServiceDiscovery(debug bool, discovery FreeboxDisco
}, nil
}
func NewFreeboxConnectionFromConfig(debug bool, reader io.Reader) (*FreeboxConnection, error) {
client := NewFreeboxHttpClient(debug)
func NewFreeboxConnectionFromConfig(reader io.Reader) (*FreeboxConnection, error) {
client := NewFreeboxHttpClient()
config := config{}
if err := json.NewDecoder(reader).Decode(&config); err != nil {
return nil, err

View file

@ -25,21 +25,15 @@ type FreeboxHttpClient struct {
decoder func(io.Reader, interface{}) error
}
type freeboxAPIResponse struct {
Success bool `json:"success"`
Message string `json:"msg"`
ErrorCode string `json:"error_code"`
}
type FreeboxHttpClientCallback func(*http.Request)
func jsonDecoder(reader io.Reader, out interface{}) error {
return json.NewDecoder(reader).Decode(out)
}
func jsonDebugDecoder(reader io.Reader, out interface{}) error {
data, err := ioutil.ReadAll(reader)
if err != nil {
return err
}
log.Debug.Println("HTTP result:", string(data))
return jsonDecoder(bytes.NewBuffer(data), out)
}
func NewFreeboxHttpClient(debug bool) *FreeboxHttpClient {
func NewFreeboxHttpClient() *FreeboxHttpClient {
result := &FreeboxHttpClient{
client: http.Client{
Transport: &http.Transport{
@ -49,13 +43,7 @@ func NewFreeboxHttpClient(debug bool) *FreeboxHttpClient {
},
Timeout: 10 * time.Second,
},
ctx: context.Background(),
decoder: jsonDecoder,
}
if debug {
log.Debug.Println("Debug enabled")
result.decoder = jsonDebugDecoder
ctx: context.Background(),
}
return result
@ -97,32 +85,40 @@ func (f *FreeboxHttpClient) do(req *http.Request, out interface{}) error {
if err != nil {
return err
}
defer res.Body.Close()
if err != nil {
return err
}
var body []byte
{
defer res.Body.Close()
r := struct {
Success bool `json:"success"`
Message string `json:"msg"`
ErrorCode string `json:"error_code"`
Result interface{} `json:"result"`
}{
Result: out,
body, err = ioutil.ReadAll(res.Body)
if err != nil {
return err
}
}
if err := f.decoder(res.Body, &r); err != nil {
log.Debug.Println("HTTP Result:", string(body))
apiResponse := freeboxAPIResponse{}
if err := json.Unmarshal(body, &apiResponse); err != nil {
return err
}
if r.Success == false {
switch r.ErrorCode {
if apiResponse.Success == false {
switch apiResponse.ErrorCode {
case errAuthRequired.Error():
return errAuthRequired
case errInvalidToken.Error():
return errInvalidToken
default:
return fmt.Errorf("%s %s error_code=%s msg=%s", req.Method, req.URL, r.ErrorCode, r.Message)
return fmt.Errorf("%s %s error_code=%s msg=%s", req.Method, req.URL, apiResponse.ErrorCode, apiResponse.Message)
}
}
result := struct {
Result interface{} `json:"result"`
}{
Result: out,
}
if err := json.Unmarshal(body, &result); err != nil {
return err
}
return nil
}