Skip to content
This repository was archived by the owner on Aug 3, 2020. It is now read-only.
Open
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
83 changes: 8 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
@@ -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("<ACCOUNT_SID>", "<AUTH_TOKEN>")

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("<ACCOUNT_SID>", "<AUTH_TOKEN>")

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).
Expand All @@ -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)




17 changes: 17 additions & 0 deletions usage_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +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 float64 `json:"price"`
PriceUnit string `json:"price_unit"`
Uri string `json:"uri"`
SubresourceUris string `json:"sub_resource_uris"`
}
99 changes: 99 additions & 0 deletions usage_record_list.go
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 20 additions & 0 deletions usage_record_list_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}