Skip to content

Commit

Permalink
Allow to configure iot-api's url (#59)
Browse files Browse the repository at this point in the history
* Update config file name

* Allow to configure iot-api's url
  • Loading branch information
Giuseppe Lumia authored and polldo committed Sep 2, 2022
1 parent d13e640 commit 0341b64
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ arduino-cloud-cli
arduino-cloud-cli.exe

# Configuration file
config.yaml
arduino-cloud-credentials.*

# Provisioning binaries and metadata
firmware/binaries
Expand Down
8 changes: 7 additions & 1 deletion internal/iot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,13 @@ func (cl *Client) DashboardDelete(id string) error {
}

func (cl *Client) setup(client, secret, organization string) error {
baseURL := "https://api2.arduino.cc"
if url := os.Getenv("IOT_API_URL"); url != "" {
baseURL = url
}

// Get the access token in exchange of client_id and client_secret
tok, err := token(client, secret)
tok, err := token(client, secret, baseURL)
if err != nil {
err = fmt.Errorf("cannot retrieve token given client and secret: %w", err)
return err
Expand All @@ -367,6 +372,7 @@ func (cl *Client) setup(client, secret, organization string) error {
if organization != "" {
config.DefaultHeader = map[string]string{"X-Organization": organization}
}
config.BasePath = baseURL + "/iot"
cl.api = iotclient.NewAPIClient(config)

return nil
Expand Down
5 changes: 3 additions & 2 deletions internal/iot/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ import (
cc "golang.org/x/oauth2/clientcredentials"
)

func token(client, secret string) (*oauth2.Token, error) {
func token(client, secret, baseURL string) (*oauth2.Token, error) {
// We need to pass the additional "audience" var to request an access token
additionalValues := url.Values{}
additionalValues.Add("audience", "https://api2.arduino.cc/iot")
// Set up OAuth2 configuration
config := cc.Config{
ClientID: client,
ClientSecret: secret,
TokenURL: "https://api2.arduino.cc/iot/v1/clients/token",
TokenURL: baseURL + "/iot/v1/clients/token",
EndpointParams: additionalValues,
}

// Get the access token in exchange of client_id and client_secret
t, err := config.Token(context.Background())
if err != nil && strings.Contains(err.Error(), "401") {
Expand Down

0 comments on commit 0341b64

Please sign in to comment.