diff --git a/examples/go/go.mod b/examples/go/go.mod index d8e9e135f..8a1e73b44 100644 --- a/examples/go/go.mod +++ b/examples/go/go.mod @@ -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 ) diff --git a/examples/go/go.sum b/examples/go/go.sum index 17fb31116..9a7087045 100644 --- a/examples/go/go.sum +++ b/examples/go/go.sum @@ -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= @@ -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= @@ -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= @@ -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= @@ -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= @@ -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= @@ -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= diff --git a/examples/go/test/api_test.go b/examples/go/test/api_test.go index 32d77b329..58c76df6e 100644 --- a/examples/go/test/api_test.go +++ b/examples/go/test/api_test.go @@ -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") @@ -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) } @@ -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) @@ -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) } @@ -93,7 +94,7 @@ func TestDateTimeQueryParams(t *testing.T) { PageSize: &pageSize, } - resp, err := testClient.OpenApi.ListCallRecording("CA12345678123456781234567812345678", ¶ms) + resp, err := testApiService.ListCallRecording("CA12345678123456781234567812345678", ¶ms) expectedTrack := "DialVerb" expectedStatus := "completed" @@ -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) } @@ -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) @@ -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) @@ -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) diff --git a/examples/go/test/mock_client.go b/examples/go/test/mock_client.go index 8795ded63..c41d0c04c 100644 --- a/examples/go/test/mock_client.go +++ b/examples/go/test/mock_client.go @@ -1,13 +1,14 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: examples/go/client/base_client.go +// Source: test_client.go -// Package mock_client is a generated GoMock package. +// Package test is a generated GoMock package. package test import ( http "net/http" url "net/url" reflect "reflect" + time "time" gomock "github.com/golang/mock/gomock" ) @@ -35,61 +36,43 @@ func (m *MockBaseClient) EXPECT() *MockBaseClientMockRecorder { return m.recorder } -// Delete mocks base method. -func (m *MockBaseClient) Delete(path string, nothing interface{}, headers map[string]interface{}) (*http.Response, error) { +// AccountSid mocks base method. +func (m *MockBaseClient) AccountSid() string { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Delete", path, nothing, headers) - ret0, _ := ret[0].(*http.Response) - ret1, _ := ret[1].(error) - return ret0, ret1 + ret := m.ctrl.Call(m, "AccountSid") + ret0, _ := ret[0].(string) + return ret0 } -// Delete indicates an expected call of Delete. -func (mr *MockBaseClientMockRecorder) Delete(path, nothing, headers interface{}) *gomock.Call { +// AccountSid indicates an expected call of AccountSid. +func (mr *MockBaseClientMockRecorder) AccountSid() *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockBaseClient)(nil).Delete), path, nothing, headers) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AccountSid", reflect.TypeOf((*MockBaseClient)(nil).AccountSid)) } -// Get mocks base method. -func (m *MockBaseClient) Get(path string, queryData interface{}, headers map[string]interface{}) (*http.Response, error) { +// SendRequest mocks base method. +func (m *MockBaseClient) SendRequest(method, rawURL string, data url.Values, headers map[string]interface{}) (*http.Response, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Get", path, queryData, headers) + ret := m.ctrl.Call(m, "SendRequest", method, rawURL, data, headers) ret0, _ := ret[0].(*http.Response) ret1, _ := ret[1].(error) return ret0, ret1 } -// Get indicates an expected call of Get. -func (mr *MockBaseClientMockRecorder) Get(path, queryData, headers interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockBaseClient)(nil).Get), path, queryData, headers) -} - -// GetAccountSid mocks base method. -func (m *MockBaseClient) GetAccountSid() string { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetAccountSid") - ret0, _ := ret[0].(string) - return ret0 -} - -// GetAccountSid indicates an expected call of GetAccountSid. -func (mr *MockBaseClientMockRecorder) GetAccountSid() *gomock.Call { +// SendRequest indicates an expected call of SendRequest. +func (mr *MockBaseClientMockRecorder) SendRequest(method, rawURL, data, headers interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAccountSid", reflect.TypeOf((*MockBaseClient)(nil).GetAccountSid)) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendRequest", reflect.TypeOf((*MockBaseClient)(nil).SendRequest), method, rawURL, data, headers) } -// Post mocks base method. -func (m *MockBaseClient) Post(path string, bodyData url.Values, headers map[string]interface{}) (*http.Response, error) { +// SetTimeout mocks base method. +func (m *MockBaseClient) SetTimeout(timeout time.Duration) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Post", path, bodyData, headers) - ret0, _ := ret[0].(*http.Response) - ret1, _ := ret[1].(error) - return ret0, ret1 + m.ctrl.Call(m, "SetTimeout", timeout) } -// Post indicates an expected call of Post. -func (mr *MockBaseClientMockRecorder) Post(path, bodyData, headers interface{}) *gomock.Call { +// SetTimeout indicates an expected call of SetTimeout. +func (mr *MockBaseClientMockRecorder) SetTimeout(timeout interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Post", reflect.TypeOf((*MockBaseClient)(nil).Post), path, bodyData, headers) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetTimeout", reflect.TypeOf((*MockBaseClient)(nil).SetTimeout), timeout) } diff --git a/examples/go/test/unit_test.go b/examples/go/test/unit_test.go index 0073ebd19..98e23f602 100644 --- a/examples/go/test/unit_test.go +++ b/examples/go/test/unit_test.go @@ -17,20 +17,22 @@ 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) } @@ -38,20 +40,22 @@ func TestPathIsCorrect(t *testing.T) { 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) @@ -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) } @@ -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", ¶ms) } diff --git a/examples/go/test_client.go b/examples/go/test_client.go index c0eab0c57..3efef0c25 100644 --- a/examples/go/test_client.go +++ b/examples/go/test_client.go @@ -1,48 +1,43 @@ -package twilio +package test_client import ( "net/http" "net/url" + "time" "github.com/twilio/twilio-go/client" ) +type BaseClient interface { + AccountSid() string + SetTimeout(timeout time.Duration) + SendRequest(method string, rawURL string, data url.Values, + headers map[string]interface{}) (*http.Response, error) +} + type TestClient struct { - *client.Credentials - HTTPClient *http.Client - BaseURL string client.Client + BaseURL string +} + +func NewTestClient(username string, password string) *TestClient { + c := &TestClient{ + Client: client.Client{ + Credentials: client.NewCredentials(username, password), + }, + } + c.SetAccountSid(username) + return c } -func (testClient TestClient) getParsedUrl(path string) *url.URL { +func (tc *TestClient) getParsedUrl(path string) *url.URL { parsedUrl, _ := url.Parse(path) - baseUrl, _ := url.Parse(testClient.BaseURL) + baseUrl, _ := url.Parse(tc.BaseURL) parsedUrl.Scheme = baseUrl.Scheme parsedUrl.Host = baseUrl.Host return parsedUrl } -func (testClient TestClient) GetAccountSid() string { - return testClient.AccountSid -} - -// Post performs a POST request on the object at the provided URI in the context of the Request's BaseURL -// with the provided data as parameters. -func (testClient TestClient) Post(path string, bodyData url.Values, headers map[string]interface{}) (*http.Response, error) { - parsedUrl := testClient.getParsedUrl(path) - return testClient.Client.Post(parsedUrl.String(), bodyData, headers) -} - -// Get performs a GET request on the object at the provided URI in the context of the Request's BaseURL -// with the provided data as parameters. -func (testClient TestClient) Get(path string, queryData interface{}, headers map[string]interface{}) (*http.Response, error) { - parsedUrl := testClient.getParsedUrl(path) - return testClient.Client.Get(parsedUrl.String(), queryData, headers) -} - -// Delete performs a DELETE request on the object at the provided URI in the context of the Request's BaseURL -// with the provided data as parameters. -func (testClient TestClient) Delete(path string, data interface{}, headers map[string]interface{}) (*http.Response, error) { - parsedUrl := testClient.getParsedUrl(path) - return testClient.Client.Delete(parsedUrl.String(), data, headers) +func (tc *TestClient) SendRequest(method string, rawURL string, data url.Values, headers map[string]interface{}) (*http.Response, error) { + return tc.Client.SendRequest(method, tc.getParsedUrl(rawURL).String(), data, headers) } diff --git a/examples/go/twilio.go b/examples/go/twilio.go deleted file mode 100644 index f90bb805a..000000000 --- a/examples/go/twilio.go +++ /dev/null @@ -1,52 +0,0 @@ -package twilio // Package twilio provides bindings for Twilio's REST APIs. - -import ( - "github.com/twilio/twilio-go/client" - openapi "twilio-oai-generator/go/rest/oai" -) - -// Twilio provides access to Twilio services. -type RestClient struct { - *client.Credentials - *TestClient - common service - OpenApi *openapi.DefaultApiService -} - -type service struct { - client *RestClient -} - -type RestClientParams struct { - AccountSid string -} - -func NewRestClientWithParams(username string, password string, params RestClientParams) *RestClient { - credentials := &client.Credentials{Username: username, Password: password} - baseClient := client.Client{ - Credentials: credentials, - BaseURL: "twilio.com", - AccountSid: params.AccountSid, - } - - c := &RestClient{ - Credentials: credentials, - TestClient: &TestClient{ - Credentials: credentials, - BaseURL: "twilio.com", - Client: baseClient, - }, - } - - c.common.client = c - c.OpenApi = openapi.NewDefaultApiService(c.TestClient) - - return c -} - -// NewRestClient provides an initialized Twilio RestClient. -func NewRestClient(username string, password string) *RestClient { - return NewRestClientWithParams(username, password, RestClientParams{ - AccountSid: username, - }) -} diff --git a/src/main/resources/twilio-go/api.mustache b/src/main/resources/twilio-go/api.mustache index b9c7b24aa..c1cf198e2 100644 --- a/src/main/resources/twilio-go/api.mustache +++ b/src/main/resources/twilio-go/api.mustache @@ -5,25 +5,30 @@ package {{packageName}} import ( "encoding/json" "fmt" - twilio "github.com/twilio/twilio-go/client" "net/url" + + twilio "github.com/twilio/twilio-go/client" {{#imports}} "{{import}}" {{/imports}} ) type {{classname}}Service struct { - baseURL string - client twilio.BaseClient + baseURL string + requestHandler *twilio.RequestHandler } -func New{{classname}}Service(client twilio.BaseClient) *{{classname}}Service { +func New{{classname}}Service(requestHandler *twilio.RequestHandler) *{{classname}}Service { return &{{classname}}Service { - client: client, - baseURL: "{{#operation}}{{#-first}}{{#servers}}{{url}}{{/servers}}{{/-first}}{{/operation}}", + requestHandler: requestHandler, + baseURL: "{{#operation}}{{#-first}}{{#servers}}{{url}}{{/servers}}{{/-first}}{{/operation}}", } } +func New{{classname}}ServiceWithClient(client twilio.BaseClient) *{{classname}}Service { + return New{{classname}}Service(twilio.NewRequestHandler(client)) +} + {{#operation}} {{#hasOptionalParams}} @@ -56,7 +61,7 @@ func (c *{{{classname}}}Service) {{{nickname}}}({{#allParams}}{{#required}}{{par if params != nil && params.PathAccountSid != nil { path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) } else { - path = strings.Replace(path, "{"+"AccountSid"+"}", c.client.GetAccountSid(), -1) + path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) } {{/vendorExtensions.x-is-account-sid}} {{/pathParams}} @@ -95,7 +100,7 @@ func (c *{{{classname}}}Service) {{{nickname}}}({{#allParams}}{{#required}}{{par {{/headerParams}} {{/hasHeaderParams}} - resp, err := c.client.{{httpMethod}}(c.baseURL+path, data, headers) + resp, err := c.requestHandler.{{httpMethod}}(c.baseURL+path, data, headers) {{#returnType}} if err != nil { return nil, err