Skip to content

Allow to configure iot-api's url #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 17, 2022
Merged
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 .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