From c98e0a52a25bf40650aceeb3a28e7cd2ba76e1b5 Mon Sep 17 00:00:00 2001 From: Alexandre Blazart Date: Fri, 30 Sep 2022 22:08:14 +0200 Subject: [PATCH] Override the API version from the command line with -apiVersion --- collector.go | 6 +++--- fbx/api_version.go | 36 ++++++++++++++++++++++++++++-------- fbx/connection.go | 9 ++++++--- main.go | 3 ++- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/collector.go b/collector.go index 5c69520..4b99965 100644 --- a/collector.go +++ b/collector.go @@ -627,7 +627,7 @@ func (c *Collector) toFloat(b bool) float64 { return 0 } -func NewCollector(filename string, discovery fbx.FreeboxDiscovery, hostDetails, debug bool) *Collector { +func NewCollector(filename string, discovery fbx.FreeboxDiscovery, forceApiVersion int, hostDetails, debug bool) *Collector { result := &Collector{ hostDetails: hostDetails, } @@ -635,14 +635,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(r) + result.freebox, err = fbx.NewFreeboxConnectionFromConfig(r, forceApiVersion) if err != nil { panic(err) } } else { log.Info.Println("Could not find the configuration file", filename) newConfig = true - result.freebox, err = fbx.NewFreeboxConnectionFromServiceDiscovery(discovery) + result.freebox, err = fbx.NewFreeboxConnectionFromServiceDiscovery(discovery, forceApiVersion) if err != nil { panic(err) } diff --git a/fbx/api_version.go b/fbx/api_version.go index a538274..7322339 100644 --- a/fbx/api_version.go +++ b/fbx/api_version.go @@ -20,6 +20,8 @@ type FreeboxAPIVersion struct { APIVersion string `json:"api_version"` APIBaseURL string `json:"api_base_url"` DeviceType string `json:"device_type"` + + QueryApiVersion int `json:"-"` } const ( @@ -36,13 +38,17 @@ const ( FreeboxDiscoveryMDNS ) -func NewFreeboxAPIVersion(client *FreeboxHttpClient, discovery FreeboxDiscovery) (*FreeboxAPIVersion, error) { +func NewFreeboxAPIVersion(client *FreeboxHttpClient, discovery FreeboxDiscovery, forceApiVersion int) (*FreeboxAPIVersion, error) { result := &FreeboxAPIVersion{} if err := result.getDiscovery(discovery)(client); err != nil { return nil, err } + if err := result.setQueryApiVersion(forceApiVersion); err != nil { + return nil, err + } + if !result.IsValid() { return nil, errors.New("could not get the API version") } @@ -61,26 +67,23 @@ func (f *FreeboxAPIVersion) IsValid() bool { f.DeviceName != "" && f.APIVersion != "" && f.APIBaseURL != "" && - f.DeviceType != "" + f.DeviceType != "" && + f.QueryApiVersion > 0 } func (f *FreeboxAPIVersion) GetURL(path string, miscPath ...interface{}) (string, error) { if !f.IsValid() { return "", errors.New("invalid FreeboxAPIVersion") } - versionSplit := strings.Split(f.APIVersion, ".") - if len(versionSplit) != 2 { - return "", fmt.Errorf("could not decode the api version \"%s\"", f.APIVersion) - } args := make([]interface{}, len(miscPath)+4) args[0] = f.APIDomain args[1] = f.HTTPSPort args[2] = f.APIBaseURL - args[3] = versionSplit[0] + args[3] = f.QueryApiVersion if len(miscPath) > 0 { copy(args[4:], miscPath) } - return fmt.Sprintf("https://%s:%d%sv%s/"+path, args...), nil + return fmt.Sprintf("https://%s:%d%sv%d/"+path, args...), nil } func (f *FreeboxAPIVersion) getDiscovery(discovery FreeboxDiscovery) func(client *FreeboxHttpClient) error { @@ -170,3 +173,20 @@ func (f *FreeboxAPIVersion) newFreeboxAPIVersionMDNS(*FreeboxHttpClient) error { return errors.New("MDNS timeout") } + +func (f *FreeboxAPIVersion) setQueryApiVersion(forceApiVersion int) error { + versionSplit := strings.Split(f.APIVersion, ".") + if len(versionSplit) != 2 { + return fmt.Errorf("could not decode the api version \"%s\"", f.APIVersion) + } + if apiVersionFromDiscovery, err := strconv.Atoi(versionSplit[0]); err != nil { + return err + } else if forceApiVersion > apiVersionFromDiscovery { + return fmt.Errorf("could use the api version %d which is higher than %d", forceApiVersion, apiVersionFromDiscovery) + } else if forceApiVersion > 0 { + f.QueryApiVersion = forceApiVersion + } else { + f.QueryApiVersion = apiVersionFromDiscovery + } + return nil +} diff --git a/fbx/connection.go b/fbx/connection.go index 3d82732..553739c 100644 --- a/fbx/connection.go +++ b/fbx/connection.go @@ -21,9 +21,9 @@ type FreeboxConnection struct { * FreeboxConnection */ -func NewFreeboxConnectionFromServiceDiscovery(discovery FreeboxDiscovery) (*FreeboxConnection, error) { +func NewFreeboxConnectionFromServiceDiscovery(discovery FreeboxDiscovery, forceApiVersion int) (*FreeboxConnection, error) { client := NewFreeboxHttpClient() - apiVersion, err := NewFreeboxAPIVersion(client, discovery) + apiVersion, err := NewFreeboxAPIVersion(client, discovery, forceApiVersion) if err != nil { return nil, err } @@ -46,12 +46,15 @@ func NewFreeboxConnectionFromServiceDiscovery(discovery FreeboxDiscovery) (*Free }, nil } -func NewFreeboxConnectionFromConfig(reader io.Reader) (*FreeboxConnection, error) { +func NewFreeboxConnectionFromConfig(reader io.Reader, forceApiVersion int) (*FreeboxConnection, error) { client := NewFreeboxHttpClient() config := config{} if err := json.NewDecoder(reader).Decode(&config); err != nil { return nil, err } + if err := config.APIVersion.setQueryApiVersion(forceApiVersion); err != nil { + return nil, err + } if !config.APIVersion.IsValid() { return nil, fmt.Errorf("invalid api_version: %v", config.APIVersion) } diff --git a/main.go b/main.go index 028f199..674be04 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,7 @@ func main() { debugPtr := flag.Bool("debug", false, "enable the debug mode") hostDetailsPtr := flag.Bool("hostDetails", false, "get details about the hosts connected to wifi and ethernet. This increases the number of metrics") httpDiscoveryPtr := flag.Bool("httpDiscovery", false, "use http://mafreebox.freebox.fr/api_version to discover the Freebox at the first run (by default: use mDNS)") + apiVersionPtr := flag.Int("apiVersion", 0, "Force the API version (by default use the latest one)") listenPtr := flag.String("listen", ":9091", "listen to address") flag.Parse() @@ -52,7 +53,7 @@ func main() { discovery = fbx.FreeboxDiscoveryHTTP } - collector := NewCollector(args[0], discovery, *hostDetailsPtr, *debugPtr) + collector := NewCollector(args[0], discovery, *apiVersionPtr, *hostDetailsPtr, *debugPtr) defer collector.Close() prometheus.MustRegister(collector)