Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/devices/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
13 changes: 9 additions & 4 deletions cmd/devices/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"

"os"
"github.com/dydx/vico-cli/pkg/auth"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -91,23 +91,28 @@ 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
// authentication refreshes when needed.
func listDevices(token string) ([]Device, error) {
req := DeviceListRequest{
Language: "en",
CountryNo: "US",
CountryNo: GetCountry(),
}

reqBody, err := json.Marshal(req)
if err != nil {
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)
}
Expand Down
7 changes: 7 additions & 0 deletions cmd/devices/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package devices

import (
"github.com/spf13/cobra"
"os"
)

var devicesCmd = &cobra.Command{
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion cmd/events/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/events/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/events/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package events

import (
"github.com/spf13/cobra"
"os"
)

var eventsCmd = &cobra.Command{
Expand All @@ -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.
Expand Down
8 changes: 7 additions & 1 deletion pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
}
Expand Down