Skip to content

Commit

Permalink
feat: add RequestHandler layer to go (twilio#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
eshanholtz authored May 20, 2021
1 parent 9b10b63 commit 931d872
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 162 deletions.
2 changes: 1 addition & 1 deletion examples/go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ go 1.16
require (
github.com/golang/mock v1.5.0
github.com/stretchr/testify v1.7.0
github.com/twilio/twilio-go v0.7.0
github.com/twilio/twilio-go v0.8.0
)
13 changes: 13 additions & 0 deletions examples/go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce/go.mod h1:o8v6yHRoi
github.com/tommy-muehle/go-mnd/v2 v2.3.1/go.mod h1:WsUAkMJMYww6l/ufffCD3m+P7LEvr8TnZn9lwVDlgzw=
github.com/twilio/twilio-go v0.7.0 h1:3vPGH6azu4I+e92Hb+8YvKFsbyzjgow4muG+lEQlE7k=
github.com/twilio/twilio-go v0.7.0/go.mod h1:8wd/x7DdvJZV5ONjJp0jLEHU0cDK5Bkr2uLSZJWM9rM=
github.com/twilio/twilio-go v0.8.0 h1:XnPJ87qUSbUIhm3BMjqGgh5aiNgobXK2CrqGcNm/omw=
github.com/twilio/twilio-go v0.8.0/go.mod h1:7KyNHcmMcBifTtf1YPaVBnXP7aE6I2RE45Alo0gjWBs=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/ultraware/funlen v0.0.3/go.mod h1:Dp4UiAus7Wdb9KUZsYWZEWiRzGuM2kXM1lPbfaF6xhA=
github.com/ultraware/whitespace v0.0.4/go.mod h1:aVMh/gQve5Maj9hQ/hg+F75lr/X5A89uZnzAmWSineA=
Expand All @@ -524,6 +526,7 @@ github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.4/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
Expand Down Expand Up @@ -587,6 +590,8 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -627,6 +632,7 @@ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwY
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand All @@ -642,6 +648,7 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -693,6 +700,9 @@ golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210217105451-b926d437f341/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210228012217-479acdf4ea46/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down Expand Up @@ -789,9 +799,12 @@ golang.org/x/tools v0.0.0-20201230224404-63754364767c/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.0.0-20210101214203-2dba1e4ea05c/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210104081019-d8d6ddbec6ee/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
Expand Down
27 changes: 14 additions & 13 deletions examples/go/test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ import (

var accountSid string
var authToken string
var testClient *twilio.RestClient
var testApiService *openapi.DefaultApiService

func TestMain(m *testing.M) {
// Do setup before the tests are run
accountSid = "AC12345678123456781234567812345678"
authToken = "CR12345678123456781234567812345678"
testClient = twilio.NewRestClient(accountSid, authToken)
testClient := test_client.NewTestClient(accountSid, authToken)
testClient.BaseURL = "http://prism_twilio:4010"
testApiService = openapi.NewDefaultApiServiceWithClient(testClient)

ret := m.Run()
os.Exit(ret)
}

func TestGet(t *testing.T) {
resp, err := testClient.OpenApi.ListCredentialAws(nil)
resp, err := testApiService.ListCredentialAws(nil)
assert.Nil(t, err)
assert.NotNil(t, resp)
assert.Equal(t, resp.Credentials[0].AccountSid, &accountSid, "AccountSid mismatch")
Expand All @@ -43,13 +44,13 @@ func TestPost(t *testing.T) {
params.FriendlyName = &friendlyName
params.Credentials = &credentials

resp, err := testClient.OpenApi.CreateCredentialAws(params)
resp, err := testApiService.CreateCredentialAws(params)
assert.Nil(t, err)
assert.NotNil(t, resp)
}

func TestDelete(t *testing.T) {
err := testClient.OpenApi.DeleteCredentialAws("CR12345678123456781234567812345678")
err := testApiService.DeleteCredentialAws("CR12345678123456781234567812345678")
assert.Nil(t, err)
}

Expand All @@ -58,7 +59,7 @@ func TestFetch(t *testing.T) {
expectedAWSSid := "CR12345678123456781234567812345678"
expectedUrl := "http://example.com"

resp, err := testClient.OpenApi.FetchCredentialAws("CR12345678123456781234567812345678")
resp, err := testApiService.FetchCredentialAws("CR12345678123456781234567812345678")
assert.Nil(t, err)
assert.NotNil(t, resp)
assert.Equal(t, resp.AccountSid, &accountSid)
Expand All @@ -72,7 +73,7 @@ func TestUpdate(t *testing.T) {
friendlyName := "MockCreds"
params.FriendlyName = &friendlyName

resp, err := testClient.OpenApi.UpdateCredentialAws(authToken, params)
resp, err := testApiService.UpdateCredentialAws(authToken, params)
assert.Nil(t, err)
assert.NotNil(t, resp)
}
Expand All @@ -93,7 +94,7 @@ func TestDateTimeQueryParams(t *testing.T) {
PageSize: &pageSize,
}

resp, err := testClient.OpenApi.ListCallRecording("CA12345678123456781234567812345678", &params)
resp, err := testApiService.ListCallRecording("CA12345678123456781234567812345678", &params)

expectedTrack := "DialVerb"
expectedStatus := "completed"
Expand All @@ -107,7 +108,7 @@ func TestDateTimeQueryParams(t *testing.T) {

func TestDateInPath(t *testing.T) {
date := "2021-01-04"
err := testClient.OpenApi.DeleteArchivedCall(date, "CA12345678123456781234567812345678")
err := testApiService.DeleteArchivedCall(date, "CA12345678123456781234567812345678")
assert.Nil(t, err)
}

Expand All @@ -118,7 +119,7 @@ func TestCustomHeaders(t *testing.T) {
params.XTwilioWebhookEnabled = &testHeader
params.RecordingStatusCallback = &testUri

resp, err := testClient.OpenApi.CreateCallRecording("CA12345678123456781234567812345678", params)
resp, err := testApiService.CreateCallRecording("CA12345678123456781234567812345678", params)

expectedSource := "Trunking"
expectedPrice := float32(100.22)
Expand All @@ -139,11 +140,11 @@ func TestRequiredParameters(t *testing.T) {
}

// StartDate and EndDate are required parameters
resp, err := testClient.OpenApi.CreateCallFeedbackSummary(nil)
resp, err := testApiService.CreateCallFeedbackSummary(nil)
assert.NotNil(t, err)
assert.Nil(t, resp)

resp, err = testClient.OpenApi.CreateCallFeedbackSummary(params)
resp, err = testApiService.CreateCallFeedbackSummary(params)

expectedCount := float32(4)

Expand All @@ -154,7 +155,7 @@ func TestRequiredParameters(t *testing.T) {
}

func TestCustomType(t *testing.T) {
resp, err := testClient.OpenApi.FetchIncomingPhoneNumber("PNFB2fe4c709Af4C1c658b25cE7DDCEbC7", nil)
resp, err := testApiService.FetchIncomingPhoneNumber("PNFB2fe4c709Af4C1c658b25cE7DDCEbC7", nil)
assert.Nil(t, err)
assert.NotNil(t, resp)
assert.Equal(t, resp.Capabilities.Fax, false)
Expand Down
63 changes: 23 additions & 40 deletions examples/go/test/mock_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 27 additions & 19 deletions examples/go/test/unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,45 @@ import (
func TestPathIsCorrect(t *testing.T) {
mockCtrl := gomock.NewController(t)
testClient := NewMockBaseClient(mockCtrl)
testClient.EXPECT().GetAccountSid().DoAndReturn(func() string {
testClient.EXPECT().AccountSid().DoAndReturn(func() string {
return "AC222222222222222222222222222222"
})
testClient.EXPECT().Get(
testClient.EXPECT().SendRequest(
gomock.Any(),
gomock.Any(),
gomock.Any(),
gomock.Any()).
DoAndReturn(func(path string, query interface{}, headers map[string]interface{}) (*http.Response, error) {
assert.Equal(t, path, "https://autopilot.twilio.com/2010-04-01/Accounts/AC222222222222222222222222222222/IncomingPhoneNumbers/PNXXXXY.json")
DoAndReturn(func(method string, rawURL string, data url.Values,
headers map[string]interface{}) (*http.Response, error) {
assert.Equal(t, rawURL, "https://autopilot.twilio.com/2010-04-01/Accounts/AC222222222222222222222222222222/IncomingPhoneNumbers/PNXXXXY.json")
return &http.Response{Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil
},
)

twilio := openapi.NewDefaultApiService(testClient)
twilio := openapi.NewDefaultApiServiceWithClient(testClient)
params := &openapi.FetchIncomingPhoneNumberParams{}
twilio.FetchIncomingPhoneNumber("PNXXXXY", params)
}

func TestAccountSidAsOptionalParam(t *testing.T) {
mockCtrl := gomock.NewController(t)
testClient := NewMockBaseClient(mockCtrl)
testClient.EXPECT().GetAccountSid().DoAndReturn(func() string {
testClient.EXPECT().AccountSid().DoAndReturn(func() string {
return "AC222222222222222222222222222222"
}).AnyTimes()
testClient.EXPECT().Get(
testClient.EXPECT().SendRequest(
gomock.Any(),
gomock.Any(),
gomock.Any(),
gomock.Any()).
DoAndReturn(func(path string, query interface{}, headers map[string]interface{}) (*http.Response, error) {
assert.Equal(t, path, "https://autopilot.twilio.com/2010-04-01/Accounts/AC444444444444444444444444444444/IncomingPhoneNumbers/PNXXXXY.json")
DoAndReturn(func(method string, rawURL string, data url.Values,
headers map[string]interface{}) (*http.Response, error) {
assert.Equal(t, rawURL, "https://autopilot.twilio.com/2010-04-01/Accounts/AC444444444444444444444444444444/IncomingPhoneNumbers/PNXXXXY.json")
return &http.Response{Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil
},
)

twilio := openapi.NewDefaultApiService(testClient)
twilio := openapi.NewDefaultApiServiceWithClient(testClient)
subAccountSid := "AC444444444444444444444444444444"
params := &openapi.FetchIncomingPhoneNumberParams{PathAccountSid: &subAccountSid}
twilio.FetchIncomingPhoneNumber("PNXXXXY", params)
Expand All @@ -69,19 +73,21 @@ func TestAddingHeader(t *testing.T) {

mockCtrl := gomock.NewController(t)
testClient := NewMockBaseClient(mockCtrl)
testClient.EXPECT().GetAccountSid().DoAndReturn(func() string {
testClient.EXPECT().AccountSid().DoAndReturn(func() string {
return accountSid
})
testClient.EXPECT().Post(
testClient.EXPECT().SendRequest(
gomock.Any(),
gomock.Any(),
gomock.Any(),
gomock.Any()).
DoAndReturn(func(path string, query interface{}, headers map[string]interface{}) (*http.Response, error) {
DoAndReturn(func(method string, rawURL string, data url.Values,
headers map[string]interface{}) (*http.Response, error) {
assert.Equal(t, headers, expectedHeader)
return &http.Response{Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil
},
)
twilio := openapi.NewDefaultApiService(testClient)
twilio := openapi.NewDefaultApiServiceWithClient(testClient)
twilio.CreateCallRecording("CA1234", params)
}

Expand All @@ -108,18 +114,20 @@ func TestQueryParams(t *testing.T) {

mockCtrl := gomock.NewController(t)
testClient := NewMockBaseClient(mockCtrl)
testClient.EXPECT().GetAccountSid().DoAndReturn(func() string {
testClient.EXPECT().AccountSid().DoAndReturn(func() string {
return accountSid
})
testClient.EXPECT().Get(
testClient.EXPECT().SendRequest(
gomock.Any(),
gomock.Any(),
gomock.Any(),
gomock.Any()).
DoAndReturn(func(path string, query interface{}, headers map[string]interface{}) (*http.Response, error) {
assert.Equal(t, query, expectedData)
DoAndReturn(func(method string, rawURL string, data url.Values,
headers map[string]interface{}) (*http.Response, error) {
assert.Equal(t, data, expectedData)
return &http.Response{Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil
},
)
twilio := openapi.NewDefaultApiService(testClient)
twilio := openapi.NewDefaultApiServiceWithClient(testClient)
twilio.ListCallRecording("CA1234", &params)
}
Loading

0 comments on commit 931d872

Please sign in to comment.