Skip to content

Commit a968a3e

Browse files
docs: basic documentation (#45)
* Added basic documentation * updated docs with env vars * remove test.go
1 parent ecd66a3 commit a968a3e

File tree

2 files changed

+78
-98
lines changed

2 files changed

+78
-98
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ install:
55

66
test:
77
go test ./...
8+
9+
fmt:
10+
gofmt -w twilio/*

README.md

Lines changed: 75 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# twilio-go
22

3+
[![Build Status](https://travis-ci.com/twilio/twilio-go.png?branch=main)](https://travis-ci.com/twilio/twilio-go)
4+
[![Learn OSS Contribution in TwilioQuest](https://img.shields.io/static/v1?label=TwilioQuest&message=Learn%20to%20contribute%21&color=F22F46&labelColor=1f243c&style=flat-square&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAAASFBMVEUAAAAZGRkcHBwjIyMoKCgAAABgYGBoaGiAgICMjIyzs7PJycnMzMzNzc3UoBfd3d3m5ubqrhfrMEDu7u739/f4vSb/3AD///9tbdyEAAAABXRSTlMAAAAAAMJrBrEAAAKoSURBVHgB7ZrRcuI6EESdyxXGYoNFvMD//+l2bSszRgyUYpFAsXOeiJGmj4NkuWx1Qeh+Ekl9DgEXOBwOx+Px5xyQhDykfgq4wG63MxxaR4ddIkg6Ul3g84vCIcjPBA5gmUMeXESrlukuoK33+33uID8TWeLAdOWsKpJYzwVMB7bOzYSGOciyUlXSn0/ABXTosJ1M1SbypZ4O4MbZuIDMU02PMbauhhHMHXbmebmALIiEbbbbbUrpF1gwE9kFfRNAJaP+FQEXCCTGyJ4ngDrjOFo3jEL5JdqjF/pueR4cCeCGgAtwmuRS6gDwaRiGvu+DMFwSBLTE3+jF8JyuV1okPZ+AC4hDFhCHyHQjdjPHUKFDlHSJkHQXMB3KpSwXNGJPcwwTdZiXlRN0gSp0zpWxNtM0beYE0nRH6QIbO7rawwXaBYz0j78gxjokDuv12gVeUuBD0MDi0OQCLvDaAho4juP1Q/jkAncXqIcCfd+7gAu4QLMACCLxpRsSuQh0igu0C9Svhi7weAGZg50L3IE3cai4IfkNZAC8dfdhsUD3CgKBVC9JE5ABAFzg4QL/taYPAAWrHdYcgfLaIgAXWJ7OV38n1LEF8tt2TH29E+QAoDoO5Ve/LtCQDmKM9kPbvCEBApK+IXzbcSJ0cIGF6e8gpcRhUDogWZ8JnaWjPXc/fNnBBUKRngiHgTUSivSzDRDgHZQOLvBQgf8rRt+VdBUUhwkU6VpJ+xcOwQUqZr+mR0kvBUgv6cB4+37hQAkXqE8PwGisGhJtN4xAHMzrsgvI7rccXqSvKh6jltGlrOHA3Xk1At3LC4QiPdX9/0ndHpGVvTjR4bZA1ypAKgVcwE5vx74ulwIugDt8e/X7JgfkucBMIAr26ndnB4UCLnDOqvteQsHlgX9N4A+c4cW3DXSPbwAAAABJRU5ErkJggg==)](https://twil.io/learn-open-source)
5+
36
## Documentation
47

58
The documentation for the Twilio API can be found [here][apidocs].
@@ -33,7 +36,21 @@ import "github.com/twilio/twilio-go"
3336

3437
accountSID := "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
3538
authToken := "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
36-
client := twilio.NewClient(s, a)
39+
40+
client := twilio.NewClient(accountSID, authToken)
41+
```
42+
43+
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.
44+
45+
```go
46+
import (
47+
"github.com/twilio/twilio-go/twilio"
48+
"os"
49+
)
50+
51+
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
52+
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
53+
client := twilio.NewClient(accountSid, authToken)
3754
```
3855

3956
### Buy a phone number
@@ -42,19 +59,28 @@ client := twilio.NewClient(s, a)
4259
package main
4360
import (
4461
"fmt"
45-
"github.com/twilio/twilio-go/twilio"
62+
twilio "github.com/twilio/twilio-go/client"
63+
openapi "github.com/twilio/twilio-go/twilio/rest/api/v2010"
64+
"os"
4665
)
4766

4867
func main() {
49-
accountSID := "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
50-
authToken := "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
51-
client := twilio.NewClient(accountSID, authToken)
52-
params := &twilio.IncomingPhoneNumberParams{PhoneNumber:twilio.String("+15017122661")}
53-
pn, err := client.IncomingPhoneNumbers.Create(params)
54-
if err != nil {
55-
fmt.Println(err)
56-
}
57-
fmt.Println(pn)
68+
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
69+
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
70+
phoneNumber := os.Getenv("TWILIO_PHONE_NUMBER")
71+
72+
client := twilio.NewClient(accountSid, authToken)
73+
74+
params := &openapi.CreateIncomingPhoneNumberParams{}
75+
params.PhoneNumber = &phoneNumber
76+
77+
resp, err := client.ApiV2010.CreateIncomingPhoneNumber(accountSid, params)
78+
if err != nil {
79+
fmt.Println(err.Error())
80+
err = nil
81+
} else {
82+
fmt.Println(resp)
83+
}
5884
}
5985
```
6086

@@ -71,10 +97,11 @@ import (
7197
func main() {
7298
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
7399
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
100+
from := os.Getenv("TWILIO_FROM_PHONE_NUMBER")
101+
to := os.Getenv("TWILIO_TO_PHONE_NUMBER")
102+
74103
client := twilio.NewClient(accountSid, authToken)
75104

76-
from := "9999999999"
77-
to := "4444444444"
78105
text := "Hello there"
79106

80107
params := &openapi.CreateMessageParams{}
@@ -94,23 +121,24 @@ func main() {
94121
```
95122

96123
### Make a call
97-
98124
``` go
99125
package main
100126

101127
import (
102128
"fmt"
129+
twilio "github.com/twilio/twilio-go/client"
103130
openapi "github.com/twilio/twilio-go/twilio/rest/api/v2010"
104-
"github.com/twilio/twilio-go/twilio"
105131
"os"
106132
)
133+
107134
func main() {
108135
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
109136
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
137+
from := os.Getenv("TWILIO_FROM_PHONE_NUMBER")
138+
to := os.Getenv("TWILIO_TO_PHONE_NUMBER")
139+
110140
client := twilio.NewClient(accountSid, authToken)
111141

112-
from := "9999999999"
113-
to := "4444444444"
114142
callurl := "http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient"
115143

116144
params := &openapi.CreateCallParams{}
@@ -132,106 +160,55 @@ func main() {
132160
```go
133161
package main
134162
import (
135-
"fmt"
136-
"github.com/twilio/twilio-go/framework/error"
137-
"github.com/twilio/twilio-go/twilio"
163+
"fmt"
164+
twilio "github.com/twilio/twilio-go/client"
165+
"github.com/twilio/twilio-go/framework/error"
166+
openapi "github.com/twilio/twilio-go/twilio/rest/api/v2010"
167+
"os"
138168
)
139169

140170
func main() {
141-
accountSID := "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
142-
authToken := "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
143-
client := twilio.NewClient(accountSID, authToken)
144-
params := &twilio.IncomingPhoneNumberParams{PhoneNumber:twilio.String("+15017122661")}
145-
pn, err := client.IncomingPhoneNumbers.Create(params)
146-
if err != nil {
147-
twilioError := err.(*error.TwilioRestError)
148-
fmt.Println(twilioError.Error())
149-
}
150-
fmt.Println(pn)
151-
}
171+
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
172+
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
173+
phoneNumber := os.Getenv("TWILIO_PHONE_NUMBER")
174+
175+
client := twilio.NewClient(accountSid, authToken)
176+
177+
params := &openapi.CreateIncomingPhoneNumberParams{}
178+
params.PhoneNumber = &phoneNumber
179+
180+
resp, err := client.ApiV2010.CreateIncomingPhoneNumber(accountSid, params)
181+
if err != nil {
182+
twilioError := err.(*error.TwilioRestError)
183+
fmt.Println(twilioError.Error())
184+
}
185+
fmt.Println(resp)
152186
```
153187
188+
For more descriptive exception types, please see the [Twilio documentation](https://www.twilio.com/docs/libraries/go/usage-guide#exceptions).
189+
154190
### Building
155191
To build *twilio-go* run:
156192
157193
```bash
158-
go build
194+
go build ./...
159195
```
160196
161197
### Testing
162198
163199
To execute the test suite run:
164200
165201
```bash
166-
go test [-v]
202+
go test ./...
167203
```
168204
169-
### Service Coverage
170-
*twilio-go* provides clients for:
171-
- [Available Phone Number Local](https://www.twilio.com/docs/phone-numbers/api/availablephonenumberlocal-resource)
172-
- [Chat Role](https://www.twilio.com/docs/chat/rest/role-resource)
173-
- [Chat Service](https://www.twilio.com/docs/chat/rest/service-resource)
174-
- [Flex Flow](https://www.twilio.com/docs/flex/flow)
175-
- [Incoming Phone Number](https://www.twilio.com/docs/phone-numbers/api/incomingphonenumber-resource)
176-
- [Proxy Phone Number](https://www.twilio.com/docs/proxy/api/phone-number)
177-
- [Proxy Service](https://www.twilio.com/docs/proxy/api/service)
178-
- [Runtime Environment](https://www.twilio.com/docs/runtime/functions-assets-api/api/environment)
179-
- [Runtime Service](https://www.twilio.com/docs/runtime/functions-assets-api/api/service)
180-
- [Studio Flow](https://www.twilio.com/docs/studio/rest-api/v2/flow)
181-
- [Sync Service](https://www.twilio.com/docs/sync/api/service)
182-
- [TaskRouter Activity](https://www.twilio.com/docs/taskrouter/api/activity)
183-
- [TaskRouter TaskQueue](https://www.twilio.com/docs/taskrouter/api/task-queue)
184-
- [TaskRouter Workflow](https://www.twilio.com/docs/taskrouter/api/workflow)
185-
- [TaskRouter Workspace](https://www.twilio.com/docs/taskrouter/api/workspace)
186-
187-
### Code Organization
188-
In general each service's client is implemented in a namesake file (e.g. [Chat Service](https://www.twilio.com/docs/chat/rest/service-resource) is in **chat_service.go**)
189-
Large services are split between files and will share the same prefix (e.g. **taskrouter_activity.go** and **taskrouter_taskqueue.go**).
190-
Files for testing are appended with `_test` (e.g. **sync_service_test.go**).
191-
**twilio.go** ties the library together by defining the `Twilio` struct and the `Client` constructor `NewClient`.
192-
The `Client` structure promotes memory reuse between service clients, and provides the `baseURL` to service clients to allow redirecting requests to non-production domains.
193-
194-
Under the hood the Twilio Client relies on the private functionality within **internal/twilio.go**.
195-
The main functionality is captured by `SendRequest` and `doWithErr`.
196-
`SendRequest` and `doWithErr` facilitate network requests and allow the library to provide an `http.Response` and `error` object.
197-
`SendRequest` performs the required encoding and request configuration to comply with Twilio's HTTP standards.
198-
Go's built in struct marshalling is not compatible with Twilio's requirements for form encoding so encoding is performed by the `form` package forked from https://github.com/ajg/form.
199-
`doWithErr` executes the request from `SendRequest`, parses the error (if any), and returns the result.
200-
The `error` returned by `doWithErr` may be the `TwilioRestError` object, which is meant to parse Twilio-specific error messages from the body of responses to failed requests.
201-
202-
### Creating and Updating Resources ###
203-
204-
All structs for Twilio resources use pointer values for all non-repeated fields.
205-
Pointer values allow users to distinguish between unset fields (`null`) and those set to a zero-value.
206-
`twilio-go` provides helper functions that easily create these pointers for string,
207-
bool, float, time, and int values. For example:
205+
### Getting help
208206
209-
```go
210-
// create a new incoming phone number
211-
params := &twilio.IncomingPhoneNumberParams{
212-
PhoneNumber: twilio.String("+15017122661")
213-
}
214-
p, err := client.IncomingPhoneNumbers.Create(params)
215-
```
216-
Users who have worked with protocol buffers should find this pattern familiar.
217-
218-
### Inspirations and References ###
219-
While the overall code structure and client interface is modeled after existing Twilio SDKs, we found additional inspiration in some of the following Go SDKs:
220-
- [Github](https://github.com/google/go-github)
221-
- [DigitalOcean](https://github.com/digitalocean/godo)
222-
- [Terraform](https://github.com/hashicorp/go-tfe)
223-
224-
These existing SDKs influenced things like our module pattern (using one module instead of breaking everything into separate resource modules) and unit testing strategy.
225-
226-
### Behavioral Notes and Future Improvements
227-
Things that we wanted to address had we been given more time:
228-
- [ ] **Parameter Validation** - *twilio-go* provides no built-in parameter validation.
229-
- [ ] **Enums**- Properties that are of an enumberable type are defaulted to a string
230-
- [ ] **Retry functionality** - There is a 10 second timeout for requests with no retry functionality.
231-
232-
### Code Style
233-
The code is styled with `go fmt` and adheres to [Go's Style Guide](https://github.com/golang/go/wiki/CodeReviewComments) wherever possible.
234-
We use `golangci-lint` for linting locally and as part of the PR process (this will catch most violations of the above guidelines).
207+
If you need help installing or using the library, please check the [Twilio Support Help Center](https://support.twilio.com) first, and [file a support ticket](https://twilio.com/help/contact) if you don't find an answer to your question.
235208
209+
All the code [here](twilio/rest) was generated by [twilio-oai-generator](https://github.com/twilio/twilio-oai-generator) by leveraging [openapi-generator](https://github.com/OpenAPITools/openapi-generator) and [twilio-oai](https://github.com/twilio/twilio-oai). If you find an issue with the generation or the openapi specs, please go ahead and open an issue or a PR against the relevant repositories.
236210
237211
[apidocs]: https://www.twilio.com/docs/api
212+
[twiml]: https://www.twilio.com/docs/api/twiml
213+
[libdocs]: https://twilio.github.io/twilio-go
214+

0 commit comments

Comments
 (0)