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

docs: add standalone usage example #81

Merged
merged 2 commits into from
May 21, 2021
Merged
Changes from 1 commit
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
57 changes: 44 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ go get github.com/twilio/twilio-go
## Getting Started

Getting started with the Twilio API couldn't be easier. Create a
`Client` and you're ready to go.
`RestClient` and you're ready to go.

### API Credentials

The Twilio `Client` needs your Twilio credentials. You should pass these
The Twilio `RestClient` needs your Twilio credentials. You should pass these
directly to the constructor (see the code below).

```go
Expand All @@ -55,7 +55,7 @@ func main(){

```go
package main
import "github.com/twilio/twilio-go/twilio"
import "github.com/twilio/twilio-go"

func main(){
accountSid := "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
Expand All @@ -72,8 +72,8 @@ We suggest storing your credentials as environment variables and then use it in
```go
package main
import (
"github.com/twilio/twilio-go"
"os"
"github.com/twilio/twilio-go"
)

func main(){
Expand All @@ -89,8 +89,8 @@ func main(){
package main

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

func main() {
Expand Down Expand Up @@ -229,13 +229,11 @@ func main() {
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
authToken := os.Getenv("TWILIO_AUTH_SID")
serviceSid := "ZSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
friendlyName := "My Serverless func"

client := twilio.NewRestClient(accountSid, authToken)

params := &openapi.CreateFunctionParams{
FriendlyName: &friendlyName,
}
params := &openapi.CreateFunctionParams{}
params.SetFriendlyName("My Serverless func")

resp, err := client.ServerlessV1.CreateFunction(serviceSid, params)
if err != nil {
Expand Down Expand Up @@ -326,7 +324,7 @@ func main() {
params := &openapi.CreateIncomingPhoneNumberParams{}
params.SetPhoneNumber(phoneNumber)

resp, err := client.ApiV2010.CreateIncomingPhoneNumber(accountSid, params)
resp, err := client.ApiV2010.CreateIncomingPhoneNumber(params)
if err != nil {
twilioError := err.(*error.TwilioRestError)
fmt.Println(twilioError.Error())
Expand All @@ -337,6 +335,35 @@ func main() {
For more descriptive exception types, please see the [Twilio documentation](https://www.twilio.com/docs/libraries/go/usage-guide#exceptions).

## Advanced Usage
### Using Standalone Products
Don't want to import the top-level Twilio RestClient with access to the full suite of Twilio products? Use standalone product services instead:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!!

```go
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I try to run this example, I'm getting:

./main.go:19:15: defaultClient.SetAccountSid undefined (type *client.Client has no field or method SetAccountSid)
./main.go:21:20: undefined: "github.com/twilio/twilio-go/rest/api/v2010".NewDefaultApiServiceWithClient
./main.go:22:26: undefined: "github.com/twilio/twilio-go/rest/serverless/v1".NewDefaultApiServiceWithClient

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you using the main branch or pulling a released version? These changes haven't been released yet.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I think I'm pulling the released version v0.8.0. Will test against main. Thanks!

package main

import (
"os"

"github.com/twilio/twilio-go/client"
apiv2010 "github.com/twilio/twilio-go/rest/api/v2010"
eshanholtz marked this conversation as resolved.
Show resolved Hide resolved
serverless "github.com/twilio/twilio-go/rest/serverless/v1"
)

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

// Create an instance of our default BaseClient implementation
defaultClient := &client.Client{
Credentials: client.NewCredentials(accountSid, authToken),
}
defaultClient.SetAccountSid(accountSid)

coreApiService := apiv2010.NewDefaultApiServiceWithClient(defaultClient)
serverlessApiService := serverless.NewDefaultApiServiceWithClient(defaultClient)
}

```

### Using a Custom Client
```go
package main
Expand All @@ -345,12 +372,13 @@ import (
"fmt"
"os"

"github.com/twilio/twilio-go"
"github.com/twilio/twilio-go/client"
"github.com/twilio/twilio-go"
"github.com/twilio/twilio-go/client"
openapi "github.com/twilio/twilio-go/rest/api/v2010"
)

type MyClient struct {
client.Client
client.Client
}

func (c *MyClient) SendRequest(method string, rawURL string, data url.Values, headers map[string]interface{}) (*http.Response, error) {
Expand All @@ -373,6 +401,9 @@ func main() {
customClient.SetAccountSid(accountSid)

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

// You may also use custom clients with standalone product services
twilioApiV2010 := openapi.NewDefaultApiServiceWithClient(customClient)
```

## Local Usage
Expand Down