Skip to content

Commit

Permalink
feat: support seperate auth sid vs account sid; repo change cleanup
Browse files Browse the repository at this point in the history
Add support for seperate SID used in auth vs the account SID used in
requests and the like. Changed a number of items related to change of
repo. Fixed tests.
  • Loading branch information
caseyh committed Dec 28, 2020
1 parent ef2f135 commit a497bd6
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.test
annotate.json
coverage.out
.idea
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
The MIT License (MIT)

Copyright (c) 2020 Casey Hancock <casey@hancock.tech>
Copyright (c) 2013 Alif Rachmawadi <subosito@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
# Twilio

[![Build Status](https://travis-ci.org/subosito/twilio.svg?branch=master)](https://travis-ci.org/subosito/twilio)
[![Coverage Status](https://img.shields.io/codecov/c/github/subosito/twilio.svg)](https://codecov.io/gh/subosito/twilio)
[![GoDoc](https://godoc.org/github.com/subosito/twilio?status.svg)](https://godoc.org/github.com/subosito/twilio)

Simple Twilio API wrapper in Go.
Simple Twilio API wrapper in Go. Forked from [subosito/twilio](https://github.com/caseyh/twilio) to allow better credential hygiene.

## Usage

As usual you can `go get` the twilio package by issuing:

```bash
$ go get github.com/subosito/twilio
$ go get github.com/caseyh/twilio
```

Then you can use it on your application:
Expand All @@ -23,7 +19,7 @@ import (
"log"
"net/url"

"github.com/subosito/twilio"
"github.com/caseyh/twilio"
)

var (
Expand Down
10 changes: 9 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@ type Client struct {

// Credentials which is used for authentication during API request
AccountSid string
AuthSid string
AuthToken string

// Services used for communicating with different parts of the Twilio API
Messages *MessageService
}

func (c *Client) authSid() string {
if c.AuthSid != "" {
return c.AuthSid
}
return c.AccountSid
}

// NewClient returns a new Twilio API client. This will load default http.Client if httpClient is nil.
func NewClient(accountSid, authToken string, httpClient *http.Client) *Client {
if httpClient == nil {
Expand Down Expand Up @@ -76,7 +84,7 @@ func (c *Client) NewRequest(method, urlStr string, body io.Reader) (*http.Reques
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
}

req.SetBasicAuth(c.AccountSid, c.AuthToken)
req.SetBasicAuth(c.authSid(), c.AuthToken)

req.Header.Add("User-Agent", c.UserAgent)
req.Header.Add("Accept", "application/json")
Expand Down
5 changes: 5 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ func TestNewClient(t *testing.T) {
c := NewClient(accountSid, authToken, nil)
assert.Equal(t, c.BaseURL.String(), apiBaseURL)
assert.Equal(t, c.UserAgent, userAgent)
assert.Equal(t, c.AccountSid, accountSid)
assert.Equal(t, c.AuthToken, authToken)
assert.Equal(t, c.authSid(), accountSid)
c.AuthSid = "cdf123"
assert.Equal(t, c.authSid(), c.AuthSid)
}

func TestNewRequest(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions price_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func testPrice(t *testing.T) {
var p Price
err := p.UnmarshalJSON([]byte(`0.74`))
if err != nil {
t.Error("Price.UnmarshalJSON returned an error %q", err)
t.Errorf("Price.UnmarshalJSON returned an error %q", err)
}

want := 0.74
Expand All @@ -22,7 +22,7 @@ func TestPrice_UnmarshalJSON_string(t *testing.T) {
var p Price
err := p.UnmarshalJSON([]byte(`"0.74"`))
if err != nil {
t.Error("Price.UnmarshalJSON returned an error %q", err)
t.Errorf("Price.UnmarshalJSON returned an error %q", err)
}

want := 0.74
Expand All @@ -35,7 +35,7 @@ func TestPrice_UnmarshalJSON_null(t *testing.T) {
var p Price
err := p.UnmarshalJSON([]byte(`null`))
if err != nil {
t.Error("Price.UnmarshalJSON returned an error %q", err)
t.Errorf("Price.UnmarshalJSON returned an error %q", err)
}

want := 0.0
Expand Down
4 changes: 2 additions & 2 deletions timestamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestTimestamp_UnmarshalJSON_string(t *testing.T) {
m := &Timestamp{}
err := m.UnmarshalJSON([]byte("Wed, 18 Aug 2010 20:01:40 +0000"))
if err != nil {
t.Error("Price.UnmarshalJSON returned an error %q", err)
t.Errorf("Price.UnmarshalJSON returned an error %q", err)
}

want := parseTimestamp("Wed, 18 Aug 2010 20:01:40 +0000")
Expand All @@ -34,7 +34,7 @@ func TestTimestamp_UnmarshalJSON_badString(t *testing.T) {
m := &Timestamp{}
err := m.UnmarshalJSON([]byte("foo/02/03"))
if err != nil {
t.Error("Price.UnmarshalJSON returned an error %q", err)
t.Errorf("Price.UnmarshalJSON returned an error %q", err)
}

want := Timestamp{}
Expand Down
2 changes: 1 addition & 1 deletion twilio.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ const (
apiVersion = "2010-04-01"
apiFormat = "json"
version = "0.1.0"
userAgent = "subosito/twilio//" + version
userAgent = "caseyh/twilio//" + version
)

0 comments on commit a497bd6

Please # to comment.