From 0e1d73d6864e225c6958519e6e47c45aa49501d4 Mon Sep 17 00:00:00 2001 From: jagadeeshm Date: Mon, 6 Jun 2016 15:33:18 -0400 Subject: [PATCH 1/3] Implementation of Usage Records --- usage_record.go | 20 ++++++++ usage_record_list.go | 99 +++++++++++++++++++++++++++++++++++++++ usage_record_list_test.go | 20 ++++++++ 3 files changed, 139 insertions(+) create mode 100644 usage_record.go create mode 100644 usage_record_list.go create mode 100644 usage_record_list_test.go diff --git a/usage_record.go b/usage_record.go new file mode 100644 index 0000000..97494f6 --- /dev/null +++ b/usage_record.go @@ -0,0 +1,20 @@ +package twiliogo + + + +type UsageRecord struct { + Category string `json:"category"` + Description string `json:"description"` + AccountSid string `json:"account_sid"` + StartDate string `json:"start_date"` + EndDate string `json:"end_date"` + Usage string `json:"usage"` + UsageUnit string `json:"usage_unit"` + Count string `json:"count"` + CountUnit string `json:"count_unit"` + Price string `json:"price"` + PriceUnit string `json:"price_unit"` + Uri string `json:"uri"` + SubresourceUris string `json:"sub_resource_uris"` +} + diff --git a/usage_record_list.go b/usage_record_list.go new file mode 100644 index 0000000..bd68867 --- /dev/null +++ b/usage_record_list.go @@ -0,0 +1,99 @@ +package twiliogo + +import ( + "encoding/json" + "net/url" +) + +type UsageRecordList struct { + Client Client + Start int `json:"start"` + Total int `json:"total"` + NumPages int `json:"num_pages"` + Page int `json:"page"` + PageSize int `json:"page_size"` + End int `json:"end"` + Uri string `json:"uri"` + FirstPageUri string `json:"first_page_uri"` + LastPageUri string `json:"last_page_uri"` + NextPageUri string `json:"next_page_uri"` + PreviousPageUri string `json"previous_page_uri"` + UsageRecords []UsageRecord `json:"usage_records"` +} + +func GetUsageRecordList(client Client, interval string, optionals ...Optional) (*UsageRecordList, error) { + var usageRecordList *UsageRecordList + + params := url.Values{} + + for _, optional := range optionals { + param, value := optional.GetParam() + params.Set(param, value) + } + + body, err := client.get(nil, "/Usage/Records/"+interval+".json") + + if err != nil { + return nil, err + } + + usageRecordList = new(UsageRecordList) + usageRecordList.Client = client + err = json.Unmarshal(body, usageRecordList) + + return usageRecordList, err +} + +func (usageRecordList *UsageRecordList) GetUsageRecords() []UsageRecord { + return usageRecordList.UsageRecords +} + +func (currentUsageRecordList *UsageRecordList) HasNextPage() bool { + return currentUsageRecordList.NextPageUri != "" +} + +func (currentUsageRecordList *UsageRecordList) NextPage() (*UsageRecordList, error) { + if !currentUsageRecordList.HasNextPage() { + return nil, Error{"No next page"} + } + + return currentUsageRecordList.getPage(currentUsageRecordList.NextPageUri) +} + +func (currentUsageRecordList *UsageRecordList) HasPreviousPage() bool { + return currentUsageRecordList.PreviousPageUri != "" +} + +func (currentUsageRecordList *UsageRecordList) PreviousPage() (*UsageRecordList, error) { + if !currentUsageRecordList.HasPreviousPage() { + return nil, Error{"No previous page"} + } + + return currentUsageRecordList.getPage(currentUsageRecordList.NextPageUri) +} + +func (currentUsageRecordList *UsageRecordList) FirstPage() (*UsageRecordList, error) { + return currentUsageRecordList.getPage(currentUsageRecordList.FirstPageUri) +} + +func (currentUsageRecordList *UsageRecordList) LastPage() (*UsageRecordList, error) { + return currentUsageRecordList.getPage(currentUsageRecordList.LastPageUri) +} + +func (currentUsageRecordList *UsageRecordList) getPage(uri string) (*UsageRecordList, error) { + var usageRecordList *UsageRecordList + + client := currentUsageRecordList.Client + + body, err := client.get(nil, uri) + + if err != nil { + return usageRecordList, err + } + + usageRecordList = new(UsageRecordList) + usageRecordList.Client = client + err = json.Unmarshal(body, usageRecordList) + + return usageRecordList, err +} diff --git a/usage_record_list_test.go b/usage_record_list_test.go new file mode 100644 index 0000000..52ab020 --- /dev/null +++ b/usage_record_list_test.go @@ -0,0 +1,20 @@ +package twiliogo + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestIntegrationUsageRecordList(t *testing.T) { + CheckTestEnv(t) + + client := NewClient(API_KEY, API_TOKEN) + + usageRecordList, err := GetUsageRecordList(client, "LastMonth") + + if assert.Nil(t, err, "Failed to retrieve usage record list") { + usageRecords := usageRecordList.GetUsageRecords() + assert.NotNil(t, usageRecords, "Failed to retrieve usageRecords") + } +} From a8622f934c36b631121e9b436f6f67d76977a5e3 Mon Sep 17 00:00:00 2001 From: jagadeeshm Date: Mon, 6 Jun 2016 16:57:13 -0400 Subject: [PATCH 2/3] Usage Resource Price datatype modified to float64 --- usage_record.go | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/usage_record.go b/usage_record.go index 97494f6..bdb0f10 100644 --- a/usage_record.go +++ b/usage_record.go @@ -1,20 +1,17 @@ package twiliogo - - type UsageRecord struct { - Category string `json:"category"` - Description string `json:"description"` - AccountSid string `json:"account_sid"` - StartDate string `json:"start_date"` - EndDate string `json:"end_date"` - Usage string `json:"usage"` - UsageUnit string `json:"usage_unit"` - Count string `json:"count"` - CountUnit string `json:"count_unit"` - Price string `json:"price"` - PriceUnit string `json:"price_unit"` - Uri string `json:"uri"` - SubresourceUris string `json:"sub_resource_uris"` + Category string `json:"category"` + Description string `json:"description"` + AccountSid string `json:"account_sid"` + StartDate string `json:"start_date"` + EndDate string `json:"end_date"` + Usage string `json:"usage"` + UsageUnit string `json:"usage_unit"` + Count string `json:"count"` + CountUnit string `json:"count_unit"` + Price float64 `json:"price"` + PriceUnit string `json:"price_unit"` + Uri string `json:"uri"` + SubresourceUris string `json:"sub_resource_uris"` } - From 6801a9a6f4146acfd64e1b198d6dc39233b08149 Mon Sep 17 00:00:00 2001 From: Jagadeesh Meesala Date: Thu, 14 Jul 2016 14:34:23 -0400 Subject: [PATCH 3/3] Update README.md --- README.md | 83 ++++++------------------------------------------------- 1 file changed, 8 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index 08731b3..2dd4b12 100644 --- a/README.md +++ b/README.md @@ -1,69 +1,27 @@ [![Build Status](https://travis-ci.org/carlosdp/twiliogo.png?branch=master)](https://travis-ci.org/carlosdp/twiliogo) # twilio-go -The unofficial Go helper library for [Twilio](http://twilio.com). +Forked from github.com/carlosdp/twiliogo and added UsageRecords API. + + # Installation ``` bash -go get github.com/carlosdp/twiliogo +go get github.com/jagadeesh-/twiliogo ``` # Documentation - [GoDoc](http://godoc.org/github.com/carlosdp/twiliogo) # Usage -## Send a Text - -``` go -package main -import ( - "fmt" - twilio "github.com/carlosdp/twiliogo" -) - -func main() { - client := twilio.NewClient("", "") - - message, err := twilio.NewMessage(client, "3334445555", "2223334444", twilio.Body("Hello World!")) - - if err != nil { - fmt.Println(err) - } else { - fmt.Println(message.Status) - } -} -``` -## Make a Call -``` go -package main -import ( - "fmt" - twilio "github.com/carlosdp/twiliogo" -) - -func main() { - client := twilio.NewClient("", "") - - call, err := twilio.NewCall(client, "8883332222", "3334443333", nil) - - if err != nil { - fmt.Println(err) - } else { - fmt.Println("Call Queued!") - } -} -``` ## Implemented Resources -- Calls -- Messages -- IncomingPhoneNumbers (partial) +- Usage Records ## Run Tests Tests can be run using `go test`, as with most golang projects. This project also contains integration tests (where they can be done non-destructively using the API or the working Test Credential endpoints). @@ -82,32 +40,7 @@ test: go test -v ``` -## Contributing -This is a side project meant to allow for quick adoption of the Twilio API for those programming web applications with it in Go. Feel free to submit pull requests so that we can cover all of the features the Twilio API has to offer! - -## To Do -Here are a few things that the project needs in order to reach v1.0: - -1. Complete test coverage. Right now, tests cover the bare minimum of usage for each feature implemented. -2. Complete IncomingPhoneNumber functionality. -3. Implement the following resources: - - AvailablePhoneNumbers - - OutgoingCallerIds - - Applications - - ConnectApps - - AuthorizedConnectApps - - Conferences - - Queues - - Short Codes - - Recordings - - Transcriptions - - Notifications - - SIP Domains - - IpAccessControlLists - - CredentialLists - - Usage Records - - Usage Triggers - -## License -This project is licensed under the [MIT License](http://opensource.org/licenses/MIT) + + +