freebox-exporter/fbx/connection.go

118 lines
2.5 KiB
Go
Raw Permalink Normal View History

2018-09-05 03:08:48 +02:00
package fbx
import (
"encoding/json"
"fmt"
"io"
)
2021-02-25 09:04:39 +01:00
type config struct {
APIVersion *FreeboxAPIVersion `json:"api"`
AppToken string `json:"app_token"`
2018-09-05 03:08:48 +02:00
}
type FreeboxConnection struct {
2021-02-25 09:04:39 +01:00
client *FreeboxHttpClient
session *FreeboxSession
config config
2018-09-05 03:08:48 +02:00
}
/*
* FreeboxConnection
*/
func NewFreeboxConnectionFromServiceDiscovery(discovery FreeboxDiscovery, forceApiVersion int) (*FreeboxConnection, error) {
client := NewFreeboxHttpClient()
apiVersion, err := NewFreeboxAPIVersion(client, discovery, forceApiVersion)
2018-09-05 03:08:48 +02:00
if err != nil {
2021-02-25 09:04:39 +01:00
return nil, err
2018-09-05 03:08:48 +02:00
}
2021-02-25 09:04:39 +01:00
appToken, err := GetAppToken(client, apiVersion)
if err != nil {
return nil, err
2018-09-05 03:08:48 +02:00
}
2021-02-25 09:04:39 +01:00
session, err := NewFreeboxSession(appToken, client, apiVersion)
if err != nil {
return nil, err
2018-09-05 03:08:48 +02:00
}
return &FreeboxConnection{
2021-02-25 09:04:39 +01:00
client: client,
session: session,
config: config{
APIVersion: apiVersion,
AppToken: appToken,
},
}, nil
2018-09-05 03:08:48 +02:00
}
func NewFreeboxConnectionFromConfig(reader io.Reader, forceApiVersion int) (*FreeboxConnection, error) {
client := NewFreeboxHttpClient()
2021-02-25 09:04:39 +01:00
config := config{}
if err := json.NewDecoder(reader).Decode(&config); err != nil {
return nil, err
2018-09-05 03:08:48 +02:00
}
if err := config.APIVersion.setQueryApiVersion(forceApiVersion); err != nil {
return nil, err
}
2022-09-30 21:11:12 +02:00
if !config.APIVersion.IsValid() {
return nil, fmt.Errorf("invalid api_version: %v", config.APIVersion)
2018-09-05 03:08:48 +02:00
}
2021-02-25 09:04:39 +01:00
if config.AppToken == "" {
2022-09-30 21:11:12 +02:00
return nil, fmt.Errorf("invalid app_token: %s", config.AppToken)
2018-09-05 03:08:48 +02:00
}
2018-09-06 01:06:16 +02:00
2021-02-25 09:04:39 +01:00
session, err := NewFreeboxSession(config.AppToken, client, config.APIVersion)
if err != nil {
return nil, err
2018-09-05 03:08:48 +02:00
}
2018-09-06 01:06:16 +02:00
2021-02-25 09:04:39 +01:00
return &FreeboxConnection{
client: client,
session: session,
config: config,
}, nil
2018-09-05 03:08:48 +02:00
}
2021-02-25 09:04:39 +01:00
func (f *FreeboxConnection) WriteConfig(writer io.Writer) error {
return json.NewEncoder(writer).Encode(&f.config)
2018-09-05 03:08:48 +02:00
}
2021-02-25 09:04:39 +01:00
func (f *FreeboxConnection) get(path string, out interface{}) error {
return f.getInternal(path, out, false)
2018-09-05 03:08:48 +02:00
}
2021-02-25 09:04:39 +01:00
func (f *FreeboxConnection) getInternal(path string, out interface{}, retry bool) error {
url, err := f.config.APIVersion.GetURL(path)
if err != nil {
2018-09-05 03:08:48 +02:00
return err
}
2021-02-25 09:04:39 +01:00
if err := f.client.Get(url, out, f.session.AddHeader); err != nil {
if retry {
2018-09-05 03:08:48 +02:00
return err
}
2021-02-25 09:04:39 +01:00
switch err {
case errAuthRequired, errInvalidToken:
err := f.session.Refresh()
if err != nil {
return err
}
return f.getInternal(path, out, true)
default:
2018-09-06 01:06:16 +02:00
return err
}
2018-09-05 03:08:48 +02:00
}
2018-09-06 01:06:16 +02:00
return nil
2018-09-05 03:08:48 +02:00
}
2021-02-25 09:04:39 +01:00
func (f *FreeboxConnection) Close() error {
url, err := f.config.APIVersion.GetURL("login/logout/")
if err != nil {
return err
}
return f.client.Post(url, nil, nil, f.session.AddHeader)
2018-09-05 03:08:48 +02:00
}