Override the API version from the command line with -apiVersion
This commit is contained in:
parent
ab395a42c8
commit
c98e0a52a2
@ -627,7 +627,7 @@ func (c *Collector) toFloat(b bool) float64 {
|
|||||||
return 0
|
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{
|
result := &Collector{
|
||||||
hostDetails: hostDetails,
|
hostDetails: hostDetails,
|
||||||
}
|
}
|
||||||
@ -635,14 +635,14 @@ func NewCollector(filename string, discovery fbx.FreeboxDiscovery, hostDetails,
|
|||||||
if r, err := os.Open(filename); err == nil {
|
if r, err := os.Open(filename); err == nil {
|
||||||
log.Info.Println("Use configuration file", filename)
|
log.Info.Println("Use configuration file", filename)
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
result.freebox, err = fbx.NewFreeboxConnectionFromConfig(r)
|
result.freebox, err = fbx.NewFreeboxConnectionFromConfig(r, forceApiVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Info.Println("Could not find the configuration file", filename)
|
log.Info.Println("Could not find the configuration file", filename)
|
||||||
newConfig = true
|
newConfig = true
|
||||||
result.freebox, err = fbx.NewFreeboxConnectionFromServiceDiscovery(discovery)
|
result.freebox, err = fbx.NewFreeboxConnectionFromServiceDiscovery(discovery, forceApiVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,8 @@ type FreeboxAPIVersion struct {
|
|||||||
APIVersion string `json:"api_version"`
|
APIVersion string `json:"api_version"`
|
||||||
APIBaseURL string `json:"api_base_url"`
|
APIBaseURL string `json:"api_base_url"`
|
||||||
DeviceType string `json:"device_type"`
|
DeviceType string `json:"device_type"`
|
||||||
|
|
||||||
|
QueryApiVersion int `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -36,13 +38,17 @@ const (
|
|||||||
FreeboxDiscoveryMDNS
|
FreeboxDiscoveryMDNS
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewFreeboxAPIVersion(client *FreeboxHttpClient, discovery FreeboxDiscovery) (*FreeboxAPIVersion, error) {
|
func NewFreeboxAPIVersion(client *FreeboxHttpClient, discovery FreeboxDiscovery, forceApiVersion int) (*FreeboxAPIVersion, error) {
|
||||||
result := &FreeboxAPIVersion{}
|
result := &FreeboxAPIVersion{}
|
||||||
|
|
||||||
if err := result.getDiscovery(discovery)(client); err != nil {
|
if err := result.getDiscovery(discovery)(client); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := result.setQueryApiVersion(forceApiVersion); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
if !result.IsValid() {
|
if !result.IsValid() {
|
||||||
return nil, errors.New("could not get the API version")
|
return nil, errors.New("could not get the API version")
|
||||||
}
|
}
|
||||||
@ -61,26 +67,23 @@ func (f *FreeboxAPIVersion) IsValid() bool {
|
|||||||
f.DeviceName != "" &&
|
f.DeviceName != "" &&
|
||||||
f.APIVersion != "" &&
|
f.APIVersion != "" &&
|
||||||
f.APIBaseURL != "" &&
|
f.APIBaseURL != "" &&
|
||||||
f.DeviceType != ""
|
f.DeviceType != "" &&
|
||||||
|
f.QueryApiVersion > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FreeboxAPIVersion) GetURL(path string, miscPath ...interface{}) (string, error) {
|
func (f *FreeboxAPIVersion) GetURL(path string, miscPath ...interface{}) (string, error) {
|
||||||
if !f.IsValid() {
|
if !f.IsValid() {
|
||||||
return "", errors.New("invalid FreeboxAPIVersion")
|
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 := make([]interface{}, len(miscPath)+4)
|
||||||
args[0] = f.APIDomain
|
args[0] = f.APIDomain
|
||||||
args[1] = f.HTTPSPort
|
args[1] = f.HTTPSPort
|
||||||
args[2] = f.APIBaseURL
|
args[2] = f.APIBaseURL
|
||||||
args[3] = versionSplit[0]
|
args[3] = f.QueryApiVersion
|
||||||
if len(miscPath) > 0 {
|
if len(miscPath) > 0 {
|
||||||
copy(args[4:], miscPath)
|
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 {
|
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")
|
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
|
||||||
|
}
|
||||||
|
@ -21,9 +21,9 @@ type FreeboxConnection struct {
|
|||||||
* FreeboxConnection
|
* FreeboxConnection
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func NewFreeboxConnectionFromServiceDiscovery(discovery FreeboxDiscovery) (*FreeboxConnection, error) {
|
func NewFreeboxConnectionFromServiceDiscovery(discovery FreeboxDiscovery, forceApiVersion int) (*FreeboxConnection, error) {
|
||||||
client := NewFreeboxHttpClient()
|
client := NewFreeboxHttpClient()
|
||||||
apiVersion, err := NewFreeboxAPIVersion(client, discovery)
|
apiVersion, err := NewFreeboxAPIVersion(client, discovery, forceApiVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -46,12 +46,15 @@ func NewFreeboxConnectionFromServiceDiscovery(discovery FreeboxDiscovery) (*Free
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFreeboxConnectionFromConfig(reader io.Reader) (*FreeboxConnection, error) {
|
func NewFreeboxConnectionFromConfig(reader io.Reader, forceApiVersion int) (*FreeboxConnection, error) {
|
||||||
client := NewFreeboxHttpClient()
|
client := NewFreeboxHttpClient()
|
||||||
config := config{}
|
config := config{}
|
||||||
if err := json.NewDecoder(reader).Decode(&config); err != nil {
|
if err := json.NewDecoder(reader).Decode(&config); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if err := config.APIVersion.setQueryApiVersion(forceApiVersion); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if !config.APIVersion.IsValid() {
|
if !config.APIVersion.IsValid() {
|
||||||
return nil, fmt.Errorf("invalid api_version: %v", config.APIVersion)
|
return nil, fmt.Errorf("invalid api_version: %v", config.APIVersion)
|
||||||
}
|
}
|
||||||
|
3
main.go
3
main.go
@ -29,6 +29,7 @@ func main() {
|
|||||||
debugPtr := flag.Bool("debug", false, "enable the debug mode")
|
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")
|
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)")
|
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")
|
listenPtr := flag.String("listen", ":9091", "listen to address")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
@ -52,7 +53,7 @@ func main() {
|
|||||||
discovery = fbx.FreeboxDiscoveryHTTP
|
discovery = fbx.FreeboxDiscoveryHTTP
|
||||||
}
|
}
|
||||||
|
|
||||||
collector := NewCollector(args[0], discovery, *hostDetailsPtr, *debugPtr)
|
collector := NewCollector(args[0], discovery, *apiVersionPtr, *hostDetailsPtr, *debugPtr)
|
||||||
defer collector.Close()
|
defer collector.Close()
|
||||||
|
|
||||||
prometheus.MustRegister(collector)
|
prometheus.MustRegister(collector)
|
||||||
|
Loading…
Reference in New Issue
Block a user