Skip to content
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

feat: support env var loading of API credentials #95

Merged
merged 5 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
68 changes: 24 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ Getting started with the Twilio API couldn't be easier. Create a

### API Credentials

The Twilio `RestClient` needs your Twilio credentials. You should pass these
directly to the constructor (see the code below).
The Twilio `RestClient` needs your Twilio credentials. You can either pass these directly to the constructor (see the code below), or use environment variables.

```go
package main
Expand All @@ -50,42 +49,37 @@ import "github.com/twilio/twilio-go"
func main() {
accountSid := "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
authToken := "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
client := twilio.NewRestClient(accountSid, authToken)
client := twilio.NewRestClientWithParams(twilio.RestClientParams{
Username: accountSid,
Password: authToken,
})
}

```
Alternatively, a `RestClient` constructor without these parameters will look for `TWILIO_ACCOUNT_SID` and `TWILIO_AUTH_TOKEN` variables inside the current environment.

We suggest storing your credentials as environment variables. Why? You'll never have to worry about committing your credentials and accidentally posting them somewhere public.
```go
package main

import "github.com/twilio/twilio-go"

func main() {
accountSid := "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
authToken := "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
subaccountSid := "ACYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
client := twilio.NewRestClientWithParams(accountSid, authToken, twilio.RestClientParams{
AccountSid: subaccountSid,
})
client := twilio.NewRestClient()
}

```

We suggest storing your credentials as environment variables and then use it in your code. Why? You'll never have to worry about committing your credentials and accidentally posting them somewhere public.

Make API calls with a SubAccount by setting the `AccountSid` field in the `twilio.RestClientParams`:
```go
package main

import (
"github.com/twilio/twilio-go"
"os"
)
import "github.com/twilio/twilio-go"

func main() {
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
client := twilio.NewRestClient(accountSid, authToken)
subaccountSid := "ACYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
client := twilio.NewRestClientWithParams(twilio.RestClientParams{
AccountSid: subaccountSid,
})
}

```

### Specify a Region and/or Edge
Expand All @@ -99,9 +93,7 @@ import (
)

func main() {
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
client := twilio.NewRestClient(accountSid, authToken)
client := twilio.NewRestClient()
client.SetRegion("au1")
client.SetEdge("sydney")
}
Expand All @@ -123,12 +115,10 @@ import (
)

func main() {
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
phoneNumber := "AVAILABLE_TWILIO_PHONE_NUMBER"
subaccountSid := os.Getenv("TWILIO_SUBACCOUNT_SID")

client := twilio.NewRestClientWithParams(accountSid, authToken, twilio.RestClientParams{
client := twilio.NewRestClientWithParams(twilio.RestClientParams{
AccountSid: subaccountSid,
})

Expand Down Expand Up @@ -159,13 +149,11 @@ import (
)

func main() {
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
from := os.Getenv("TWILIO_FROM_PHONE_NUMBER")
to := os.Getenv("TWILIO_TO_PHONE_NUMBER")
subaccountSid := os.Getenv("TWILIO_SUBACCOUNT_SID")

client := twilio.NewRestClientWithParams(accountSid, authToken, twilio.RestClientParams{
client := twilio.NewRestClientWithParams(twilio.RestClientParams{
AccountSid: subaccountSid,
})

Expand Down Expand Up @@ -197,12 +185,10 @@ import (
)

func main() {
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
from := os.Getenv("TWILIO_FROM_PHONE_NUMBER")
to := os.Getenv("TWILIO_TO_PHONE_NUMBER")

client := twilio.NewRestClient(accountSid, authToken)
client := twilio.NewRestClient()

params := &openapi.CreateCallParams{}
params.SetTo(to)
Expand Down Expand Up @@ -232,11 +218,9 @@ import (
)

func main() {
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
authToken := os.Getenv("TWILIO_AUTH_SID")
serviceSid := "ZSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

client := twilio.NewRestClient(accountSid, authToken)
client := twilio.NewRestClient()

params := &openapi.CreateFunctionParams{}
params.SetFriendlyName("My Serverless func")
Expand Down Expand Up @@ -266,9 +250,6 @@ import (
)

func main() {
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
authToken := os.Getenv("TWILIO_AUTH_TOKEN")

var jsonStr = `{
"description":"Twilio Studio flow service",
"initial_state":"Trigger",
Expand All @@ -292,7 +273,7 @@ func main() {
definition := make(map[string]interface{})
_ = json.Unmarshal([]byte(jsonStr), &definition)

client := twilio.NewRestClient(accountSid, authToken)
client := twilio.NewRestClient()
params := &openapi.CreateFlowParams{
Definition: &definition,
}
Expand Down Expand Up @@ -323,11 +304,9 @@ import (
)

func main() {
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
phoneNumber := os.Getenv("TWILIO_PHONE_NUMBER")

client := twilio.NewRestClient(accountSid, authToken)
client := twilio.NewRestClient()

params := &openapi.CreateIncomingPhoneNumberParams{}
params.SetPhoneNumber(phoneNumber)
Expand Down Expand Up @@ -361,6 +340,7 @@ func main() {
authToken := os.Getenv("TWILIO_AUTH_TOKEN")

// Create an instance of our default BaseClient implementation
// You will need to provide your API credentials to the Client manually
defaultClient := &client.Client{
Credentials: client.NewCredentials(accountSid, authToken),
}
Expand Down Expand Up @@ -409,7 +389,7 @@ func main() {
}
customClient.SetAccountSid(accountSid)

twilioClient := twilio.NewRestClientWithParams(accountSid, authToken, twilio.RestClientParams{Client: customClient})
twilioClient := twilio.NewRestClientWithParams(twilio.RestClientParams{Client: customClient})

// You may also use custom clients with standalone product services
twilioApiV2010 := openapi.NewApiServiceWithClient(customClient)
Expand Down
22 changes: 22 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client_test

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -155,3 +156,24 @@ func TestClient_SetTimeoutCreatesClient(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
}

func TestClient_UnicodeResponse(t *testing.T) {
mockServer := httptest.NewServer(http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
d := map[string]interface{}{
"testing-unicode": "Ω≈ç√, 💩",
}
encoder := json.NewEncoder(writer)
err := encoder.Encode(&d)
if err != nil {
t.Error(err)
}
}))
defer mockServer.Close()

client := NewClient("user", "pass")
resp, _ := client.SendRequest("get", mockServer.URL, nil, nil) //nolint:bodyclose
assert.Equal(t, 200, resp.StatusCode)
body, _ := ioutil.ReadAll(resp.Body)
assert.Equal(t, "{\"testing-unicode\":\"Ω≈ç√, 💩\"}\n", string(body))
}
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@ go 1.16

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-lintpack/lintpack v0.5.2 // indirect
github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6 // indirect
github.com/golangci/go-tools v0.0.0-20190318055746-e32c54105b7c // indirect
github.com/golangci/goconst v0.0.0-20180610141641-041c5f2b40f3 // indirect
github.com/golangci/gocyclo v0.0.0-20180528134321-2becd97e67ee // indirect
github.com/golangci/golangci-lint v1.39.0 // indirect
github.com/golangci/gosec v0.0.0-20190211064107-66fb7fc33547 // indirect
github.com/golangci/ineffassign v0.0.0-20190609212857-42439a7714cc // indirect
github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21 // indirect
github.com/klauspost/cpuid v1.2.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pkg/errors v0.9.1
github.com/shirou/gopsutil v0.0.0-20180427012116-c95755e4bcd7 // indirect
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 // indirect
github.com/stretchr/testify v1.7.0
golang.org/x/tools v0.1.5 // indirect
gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect
sourcegraph.com/sqs/pbtypes v0.0.0-20180604144634-d3ebe8f20ae4 // indirect
)
Loading