Skip to content

Commit 0341b64

Browse files
Giuseppe Lumiapolldo
Giuseppe Lumia
authored andcommitted
Allow to configure iot-api's url (#59)
* Update config file name * Allow to configure iot-api's url
1 parent d13e640 commit 0341b64

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

Diff for: .gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ arduino-cloud-cli
33
arduino-cloud-cli.exe
44

55
# Configuration file
6-
config.yaml
6+
arduino-cloud-credentials.*
77

88
# Provisioning binaries and metadata
99
firmware/binaries

Diff for: internal/iot/client.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,13 @@ func (cl *Client) DashboardDelete(id string) error {
353353
}
354354

355355
func (cl *Client) setup(client, secret, organization string) error {
356+
baseURL := "https://api2.arduino.cc"
357+
if url := os.Getenv("IOT_API_URL"); url != "" {
358+
baseURL = url
359+
}
360+
356361
// Get the access token in exchange of client_id and client_secret
357-
tok, err := token(client, secret)
362+
tok, err := token(client, secret, baseURL)
358363
if err != nil {
359364
err = fmt.Errorf("cannot retrieve token given client and secret: %w", err)
360365
return err
@@ -367,6 +372,7 @@ func (cl *Client) setup(client, secret, organization string) error {
367372
if organization != "" {
368373
config.DefaultHeader = map[string]string{"X-Organization": organization}
369374
}
375+
config.BasePath = baseURL + "/iot"
370376
cl.api = iotclient.NewAPIClient(config)
371377

372378
return nil

Diff for: internal/iot/token.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@ import (
2727
cc "golang.org/x/oauth2/clientcredentials"
2828
)
2929

30-
func token(client, secret string) (*oauth2.Token, error) {
30+
func token(client, secret, baseURL string) (*oauth2.Token, error) {
3131
// We need to pass the additional "audience" var to request an access token
3232
additionalValues := url.Values{}
3333
additionalValues.Add("audience", "https://api2.arduino.cc/iot")
3434
// Set up OAuth2 configuration
3535
config := cc.Config{
3636
ClientID: client,
3737
ClientSecret: secret,
38-
TokenURL: "https://api2.arduino.cc/iot/v1/clients/token",
38+
TokenURL: baseURL + "/iot/v1/clients/token",
3939
EndpointParams: additionalValues,
4040
}
41+
4142
// Get the access token in exchange of client_id and client_secret
4243
t, err := config.Token(context.Background())
4344
if err != nil && strings.Contains(err.Error(), "401") {

0 commit comments

Comments
 (0)