From bfa38552e1994fc85e99865252606b9fd0cd6107 Mon Sep 17 00:00:00 2001 From: Vincent Verbist Date: Sun, 14 Dec 2025 10:31:29 +0100 Subject: [PATCH] Adds region support Introduces environment variables for base URL and country. Uses the environment variables if set, otherwise defaults to US region. This allows the CLI to be used with different Vicohome regions. --- cmd/devices/get.go | 2 +- cmd/devices/list.go | 13 +++++++++---- cmd/devices/root.go | 7 +++++++ cmd/events/get.go | 2 +- cmd/events/list.go | 2 +- cmd/events/root.go | 8 +++++++- pkg/auth/auth.go | 8 +++++++- 7 files changed, 33 insertions(+), 9 deletions(-) diff --git a/cmd/devices/get.go b/cmd/devices/get.go index 0c9f24a..c00b057 100644 --- a/cmd/devices/get.go +++ b/cmd/devices/get.go @@ -90,7 +90,7 @@ func getDevice(token string, serialNumber string) (Device, error) { return Device{}, fmt.Errorf("error marshaling request: %w", err) } - httpReq, err := http.NewRequest("POST", "https://api-us.vicohome.io/device/selectsingledevice", bytes.NewBuffer(reqBody)) + httpReq, err := http.NewRequest("POST", GetBaseURL()+"/device/selectsingledevice", bytes.NewBuffer(reqBody)) if err != nil { return Device{}, fmt.Errorf("error creating request: %w", err) } diff --git a/cmd/devices/list.go b/cmd/devices/list.go index 1551e7e..e19e0e3 100644 --- a/cmd/devices/list.go +++ b/cmd/devices/list.go @@ -5,7 +5,7 @@ import ( "encoding/json" "fmt" "net/http" - + "os" "github.com/dydx/vico-cli/pkg/auth" "github.com/spf13/cobra" ) @@ -91,7 +91,12 @@ var listCmd = &cobra.Command{ func init() { listCmd.Flags().StringVar(&outputFormat, "format", "table", "Output format (table or json)") } - +func GetCountry() string { +if v := os.Getenv("VICOHOME_COUNTRY"); v != "" { +return v +} +return "US" +} // listDevices fetches all devices associated with the user's account from the Vicohome API. // It takes an authentication token and returns a slice of Device objects and any error encountered. // This function handles the API request, response parsing, and error handling including @@ -99,7 +104,7 @@ func init() { func listDevices(token string) ([]Device, error) { req := DeviceListRequest{ Language: "en", - CountryNo: "US", + CountryNo: GetCountry(), } reqBody, err := json.Marshal(req) @@ -107,7 +112,7 @@ func listDevices(token string) ([]Device, error) { return nil, fmt.Errorf("error marshaling request: %w", err) } - httpReq, err := http.NewRequest("POST", "https://api-us.vicohome.io/device/listuserdevices", bytes.NewBuffer(reqBody)) + httpReq, err := http.NewRequest("POST", GetBaseURL()+"/device/listuserdevices", bytes.NewBuffer(reqBody)) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) } diff --git a/cmd/devices/root.go b/cmd/devices/root.go index 0fe557c..ab1ad9d 100644 --- a/cmd/devices/root.go +++ b/cmd/devices/root.go @@ -6,6 +6,7 @@ package devices import ( "github.com/spf13/cobra" + "os" ) var devicesCmd = &cobra.Command{ @@ -19,6 +20,12 @@ func init() { devicesCmd.AddCommand(listCmd) devicesCmd.AddCommand(getCmd) } +func GetBaseURL() string { +if v := os.Getenv("VICOHOME_BASE_URL"); v != "" { +return v +} +return "https://api-us.vicohome.io" +} // GetDevicesCmd returns the devices command that provides access to device-related subcommands. // This function is called by the root command to add device functionality to the CLI. diff --git a/cmd/events/get.go b/cmd/events/get.go index 328581f..494f05e 100644 --- a/cmd/events/get.go +++ b/cmd/events/get.go @@ -91,7 +91,7 @@ func getEvent(token string, traceID string) (Event, error) { return Event{}, fmt.Errorf("error marshaling request: %w", err) } - httpReq, err := http.NewRequest("POST", "https://api-us.vicohome.io/library/newselectsinglelibrary", bytes.NewBuffer(reqBody)) + httpReq, err := http.NewRequest("POST", GetBaseURL()+"/library/newselectsinglelibrary", bytes.NewBuffer(reqBody)) if err != nil { return Event{}, fmt.Errorf("error creating request: %w", err) } diff --git a/cmd/events/list.go b/cmd/events/list.go index e99ca01..8004ec4 100644 --- a/cmd/events/list.go +++ b/cmd/events/list.go @@ -160,7 +160,7 @@ func fetchEvents(token string, request Request) ([]Event, error) { return nil, fmt.Errorf("error marshaling request: %w", err) } - req, err := http.NewRequest("POST", "https://api-us.vicohome.io/library/newselectlibrary", bytes.NewBuffer(reqBody)) + req, err := http.NewRequest("POST", GetBaseURL()+"/library/newselectlibrary", bytes.NewBuffer(reqBody)) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) } diff --git a/cmd/events/root.go b/cmd/events/root.go index 6808127..4df617d 100644 --- a/cmd/events/root.go +++ b/cmd/events/root.go @@ -6,6 +6,7 @@ package events import ( "github.com/spf13/cobra" + "os" ) var eventsCmd = &cobra.Command{ @@ -20,7 +21,12 @@ func init() { eventsCmd.AddCommand(getCmd) eventsCmd.AddCommand(searchCmd) } - +func GetBaseURL() string { +if v := os.Getenv("VICOHOME_BASE_URL"); v != "" { +return v +} +return "https://api-us.vicohome.io" +} // GetEventsCmd returns the events command that provides access to event-related subcommands. // This function is called by the root command to add event functionality to the CLI. // It returns the events command with all subcommands (list, get, search) already attached. diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index 36e2f0e..9eacad4 100644 --- a/pkg/auth/auth.go +++ b/pkg/auth/auth.go @@ -99,6 +99,12 @@ func Authenticate() (string, error) { return token, nil } +func GetBaseURL() string { +if v := os.Getenv("VICOHOME_BASE_URL"); v != "" { +return v +} +return "https://api-us.vicohome.io" +} // authenticateDirectly performs authentication to the Vicohome API without using the token cache. // It retrieves credentials from environment variables (VICOHOME_EMAIL and VICOHOME_PASSWORD), @@ -128,7 +134,7 @@ func authenticateDirectly() (string, error) { return "", fmt.Errorf("error marshaling login request: %w", err) } - req, err := http.NewRequest("POST", "https://api-us.vicohome.io/account/login", bytes.NewBuffer(reqBody)) + req, err := http.NewRequest("POST", GetBaseURL()+"/account/login", bytes.NewBuffer(reqBody)) if err != nil { return "", fmt.Errorf("error creating request: %w", err) }