feat: add context to go client

This commit is contained in:
franklin 2021-05-21 14:42:01 +02:00
parent 5e630c0631
commit 430911ed9d
9 changed files with 101 additions and 89 deletions

View File

@ -2,6 +2,7 @@ package gotsrpc
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
@ -20,7 +21,7 @@ const (
var _ Client = &bufferedClient{}
type Client interface {
Call(url string, endpoint string, method string, args []interface{}, reply []interface{}) (err error)
Call(ctx context.Context, url string, endpoint string, method string, args []interface{}, reply []interface{}) (err error)
SetClientEncoding(encoding ClientEncoding)
SetTransportHttpClient(client *http.Client)
SetDefaultHeaders(headers http.Header)
@ -38,11 +39,11 @@ func NewClientWithHttpClient(client *http.Client) Client {
}
}
func newRequest(url string, contentType string, buffer *bytes.Buffer, headers http.Header) (r *http.Request, err error) {
func newRequest(ctx context.Context, url string, contentType string, buffer *bytes.Buffer, headers http.Header) (r *http.Request, err error) {
if buffer == nil {
buffer = &bytes.Buffer{}
}
request, errRequest := http.NewRequest("POST", url, buffer)
request, errRequest := http.NewRequestWithContext(ctx, "POST", url, buffer)
if errRequest != nil {
return nil, errors.Wrap(errRequest, "could not create a request")
}
@ -75,7 +76,7 @@ func (c *bufferedClient) SetTransportHttpClient(client *http.Client) {
}
// CallClient calls a method on the remove service
func (c *bufferedClient) Call(url string, endpoint string, method string, args []interface{}, reply []interface{}) (err error) {
func (c *bufferedClient) Call(ctx context.Context, url string, endpoint string, method string, args []interface{}, reply []interface{}) (err error) {
// Marshall args
b := new(bytes.Buffer)
@ -90,7 +91,7 @@ func (c *bufferedClient) Call(url string, endpoint string, method string, args [
// Create post url
postURL := fmt.Sprintf("%s%s/%s", url, endpoint, method)
request, errRequest := newRequest(postURL, c.handle.contentType, b, c.headers.Clone())
request, errRequest := newRequest(ctx, postURL, c.handle.contentType, b, c.headers.Clone())
if errRequest != nil {
return errRequest
}

View File

@ -1,6 +1,7 @@
package gotsrpc
import (
"context"
"net/http"
"testing"
@ -12,12 +13,12 @@ func Test_newRequest(t *testing.T) {
headers := http.Header{}
headers.Set("test", "test")
request, err := newRequest("/test", "text/html", nil, headers)
request, err := newRequest(context.Background(), "/test", "text/html", nil, headers)
assert.NoError(t, err)
assert.Equal(t, "test", request.Header.Get("test"))
})
t.Run("default", func(t *testing.T) {
request, err := newRequest("/test", "text/html", nil, nil)
request, err := newRequest(context.Background(), "/test", "text/html", nil, nil)
assert.NoError(t, err)
assert.Equal(t, "/test", request.URL.Path)
assert.Equal(t, "text/html", request.Header.Get("Accept"))

View File

@ -1,4 +1,4 @@
// Code generated by gotsrpc https://github.com/foomo/gotsrpc - DO NOT EDIT.
// Code generated by gotsrpc https://github.com/foomo/gotsrpc/v2 - DO NOT EDIT.
package demo

View File

@ -1,4 +1,4 @@
// Code generated by gotsrpc https://github.com/foomo/gotsrpc - DO NOT EDIT.
// Code generated by gotsrpc https://github.com/foomo/gotsrpc/v2 - DO NOT EDIT.
package demo

View File

@ -1,4 +1,4 @@
// Code generated by gotsrpc https://github.com/foomo/gotsrpc - DO NOT EDIT.
// Code generated by gotsrpc https://github.com/foomo/gotsrpc/v2 - DO NOT EDIT.
package demo
@ -42,7 +42,6 @@ func (p *FooGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
defer io.Copy(ioutil.Discard, r.Body) // Drain Request Body
var args []interface{}
funcName := gotsrpc.GetCalledFunc(r, p.EndPoint)
callStats := gotsrpc.GetStatsForRequest(r)
if callStats != nil {
@ -55,7 +54,7 @@ func (p *FooGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var (
arg_number int64
)
args = []interface{}{&arg_number}
args := []interface{}{&arg_number}
err := gotsrpc.LoadArgs(&args, callStats, r)
if err != nil {
gotsrpc.ErrorCouldNotLoadArgs(w)
@ -104,7 +103,6 @@ func (p *DemoGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
defer io.Copy(ioutil.Discard, r.Body) // Drain Request Body
var args []interface{}
funcName := gotsrpc.GetCalledFunc(r, p.EndPoint)
callStats := gotsrpc.GetStatsForRequest(r)
if callStats != nil {
@ -119,7 +117,7 @@ func (p *DemoGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
arg_anyList []github_com_foomo_gotsrpc_v2_demo_nested.Any
arg_anyMap map[string]github_com_foomo_gotsrpc_v2_demo_nested.Any
)
args = []interface{}{&arg_any, &arg_anyList, &arg_anyMap}
args := []interface{}{&arg_any, &arg_anyList, &arg_anyMap}
err := gotsrpc.LoadArgs(&args, callStats, r)
if err != nil {
gotsrpc.ErrorCouldNotLoadArgs(w)
@ -136,7 +134,7 @@ func (p *DemoGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var (
arg_person *Person
)
args = []interface{}{&arg_person}
args := []interface{}{&arg_person}
err := gotsrpc.LoadArgs(&args, callStats, r)
if err != nil {
gotsrpc.ErrorCouldNotLoadArgs(w)
@ -161,7 +159,7 @@ func (p *DemoGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var (
arg_name string
)
args = []interface{}{&arg_name}
args := []interface{}{&arg_name}
err := gotsrpc.LoadArgs(&args, callStats, r)
if err != nil {
gotsrpc.ErrorCouldNotLoadArgs(w)
@ -180,7 +178,7 @@ func (p *DemoGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
arg_anythingMap map[string]interface{}
arg_anythingSlice []interface{}
)
args = []interface{}{&arg_anything, &arg_anythingMap, &arg_anythingSlice}
args := []interface{}{&arg_anything, &arg_anythingMap, &arg_anythingSlice}
err := gotsrpc.LoadArgs(&args, callStats, r)
if err != nil {
gotsrpc.ErrorCouldNotLoadArgs(w)
@ -197,7 +195,7 @@ func (p *DemoGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var (
arg_intMap map[int]string
)
args = []interface{}{&arg_intMap}
args := []interface{}{&arg_intMap}
err := gotsrpc.LoadArgs(&args, callStats, r)
if err != nil {
gotsrpc.ErrorCouldNotLoadArgs(w)
@ -278,7 +276,6 @@ func (p *BarGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
defer io.Copy(ioutil.Discard, r.Body) // Drain Request Body
var args []interface{}
funcName := gotsrpc.GetCalledFunc(r, p.EndPoint)
callStats := gotsrpc.GetStatsForRequest(r)
if callStats != nil {
@ -292,7 +289,7 @@ func (p *BarGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
arg_one CustomError
arg_two *CustomError
)
args = []interface{}{&arg_one, &arg_two}
args := []interface{}{&arg_one, &arg_two}
err := gotsrpc.LoadArgs(&args, callStats, r)
if err != nil {
gotsrpc.ErrorCouldNotLoadArgs(w)
@ -311,7 +308,7 @@ func (p *BarGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
arg_customTypeString CustomTypeString
arg_CustomTypeStruct CustomTypeStruct
)
args = []interface{}{&arg_customTypeInt, &arg_customTypeString, &arg_CustomTypeStruct}
args := []interface{}{&arg_customTypeInt, &arg_customTypeString, &arg_CustomTypeStruct}
err := gotsrpc.LoadArgs(&args, callStats, r)
if err != nil {
gotsrpc.ErrorCouldNotLoadArgs(w)
@ -328,7 +325,7 @@ func (p *BarGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var (
arg_number int64
)
args = []interface{}{&arg_number}
args := []interface{}{&arg_number}
err := gotsrpc.LoadArgs(&args, callStats, r)
if err != nil {
gotsrpc.ErrorCouldNotLoadArgs(w)
@ -347,7 +344,7 @@ func (p *BarGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
arg_nested OuterNested
arg_inline OuterInline
)
args = []interface{}{&arg_inner, &arg_nested, &arg_inline}
args := []interface{}{&arg_inner, &arg_nested, &arg_inline}
err := gotsrpc.LoadArgs(&args, callStats, r)
if err != nil {
gotsrpc.ErrorCouldNotLoadArgs(w)
@ -365,7 +362,7 @@ func (p *BarGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
arg_one string
arg_two string
)
args = []interface{}{&arg_one, &arg_two}
args := []interface{}{&arg_one, &arg_two}
err := gotsrpc.LoadArgs(&args, callStats, r)
if err != nil {
gotsrpc.ErrorCouldNotLoadArgs(w)

View File

@ -1,16 +1,17 @@
// Code generated by gotsrpc https://github.com/foomo/gotsrpc - DO NOT EDIT.
// Code generated by gotsrpc https://github.com/foomo/gotsrpc/v2 - DO NOT EDIT.
package demo
import (
net_http "net/http"
go_context "context"
go_net_http "net/http"
gotsrpc "github.com/foomo/gotsrpc/v2"
github_com_foomo_gotsrpc_v2_demo_nested "github.com/foomo/gotsrpc/v2/demo/nested"
)
type FooGoTSRPCClient interface {
Hello(number int64) (retHello_0 int, clientErr error)
Hello(ctx go_context.Context, number int64) (retHello_0 int, clientErr error)
}
type HTTPFooGoTSRPCClient struct {
@ -27,31 +28,31 @@ func NewFooGoTSRPCClient(url string, endpoint string) *HTTPFooGoTSRPCClient {
return NewFooGoTSRPCClientWithClient(url, endpoint, nil)
}
func NewFooGoTSRPCClientWithClient(url string, endpoint string, client *net_http.Client) *HTTPFooGoTSRPCClient {
func NewFooGoTSRPCClientWithClient(url string, endpoint string, client *go_net_http.Client) *HTTPFooGoTSRPCClient {
return &HTTPFooGoTSRPCClient{
URL: url,
EndPoint: endpoint,
Client: gotsrpc.NewClientWithHttpClient(client),
}
}
func (tsc *HTTPFooGoTSRPCClient) Hello(number int64) (retHello_0 int, clientErr error) {
func (tsc *HTTPFooGoTSRPCClient) Hello(ctx go_context.Context, number int64) (retHello_0 int, clientErr error) {
args := []interface{}{number}
reply := []interface{}{&retHello_0}
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "Hello", args, reply)
clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "Hello", args, reply)
return
}
type DemoGoTSRPCClient interface {
Any(any github_com_foomo_gotsrpc_v2_demo_nested.Any, anyList []github_com_foomo_gotsrpc_v2_demo_nested.Any, anyMap map[string]github_com_foomo_gotsrpc_v2_demo_nested.Any) (retAny_0 github_com_foomo_gotsrpc_v2_demo_nested.Any, retAny_1 []github_com_foomo_gotsrpc_v2_demo_nested.Any, retAny_2 map[string]github_com_foomo_gotsrpc_v2_demo_nested.Any, clientErr error)
ExtractAddress(person *Person) (addr *Address, e *Err, clientErr error)
GiveMeAScalar() (amount github_com_foomo_gotsrpc_v2_demo_nested.Amount, wahr github_com_foomo_gotsrpc_v2_demo_nested.True, hier ScalarInPlace, clientErr error)
Hello(name string) (retHello_0 string, retHello_1 *Err, clientErr error)
HelloInterface(anything interface{}, anythingMap map[string]interface{}, anythingSlice []interface{}) (clientErr error)
HelloNumberMaps(intMap map[int]string) (floatMap map[float64]string, clientErr error)
HelloScalarError() (err *ScalarError, clientErr error)
MapCrap() (crap map[string][]int, clientErr error)
Nest() (retNest_0 []*github_com_foomo_gotsrpc_v2_demo_nested.Nested, clientErr error)
TestScalarInPlace() (retTestScalarInPlace_0 ScalarInPlace, clientErr error)
Any(ctx go_context.Context, any github_com_foomo_gotsrpc_v2_demo_nested.Any, anyList []github_com_foomo_gotsrpc_v2_demo_nested.Any, anyMap map[string]github_com_foomo_gotsrpc_v2_demo_nested.Any) (retAny_0 github_com_foomo_gotsrpc_v2_demo_nested.Any, retAny_1 []github_com_foomo_gotsrpc_v2_demo_nested.Any, retAny_2 map[string]github_com_foomo_gotsrpc_v2_demo_nested.Any, clientErr error)
ExtractAddress(ctx go_context.Context, person *Person) (addr *Address, e *Err, clientErr error)
GiveMeAScalar(ctx go_context.Context) (amount github_com_foomo_gotsrpc_v2_demo_nested.Amount, wahr github_com_foomo_gotsrpc_v2_demo_nested.True, hier ScalarInPlace, clientErr error)
Hello(ctx go_context.Context, name string) (retHello_0 string, retHello_1 *Err, clientErr error)
HelloInterface(ctx go_context.Context, anything interface{}, anythingMap map[string]interface{}, anythingSlice []interface{}) (clientErr error)
HelloNumberMaps(ctx go_context.Context, intMap map[int]string) (floatMap map[float64]string, clientErr error)
HelloScalarError(ctx go_context.Context) (err *ScalarError, clientErr error)
MapCrap(ctx go_context.Context) (crap map[string][]int, clientErr error)
Nest(ctx go_context.Context) (retNest_0 []*github_com_foomo_gotsrpc_v2_demo_nested.Nested, clientErr error)
TestScalarInPlace(ctx go_context.Context) (retTestScalarInPlace_0 ScalarInPlace, clientErr error)
}
type HTTPDemoGoTSRPCClient struct {
@ -68,89 +69,89 @@ func NewDemoGoTSRPCClient(url string, endpoint string) *HTTPDemoGoTSRPCClient {
return NewDemoGoTSRPCClientWithClient(url, endpoint, nil)
}
func NewDemoGoTSRPCClientWithClient(url string, endpoint string, client *net_http.Client) *HTTPDemoGoTSRPCClient {
func NewDemoGoTSRPCClientWithClient(url string, endpoint string, client *go_net_http.Client) *HTTPDemoGoTSRPCClient {
return &HTTPDemoGoTSRPCClient{
URL: url,
EndPoint: endpoint,
Client: gotsrpc.NewClientWithHttpClient(client),
}
}
func (tsc *HTTPDemoGoTSRPCClient) Any(any github_com_foomo_gotsrpc_v2_demo_nested.Any, anyList []github_com_foomo_gotsrpc_v2_demo_nested.Any, anyMap map[string]github_com_foomo_gotsrpc_v2_demo_nested.Any) (retAny_0 github_com_foomo_gotsrpc_v2_demo_nested.Any, retAny_1 []github_com_foomo_gotsrpc_v2_demo_nested.Any, retAny_2 map[string]github_com_foomo_gotsrpc_v2_demo_nested.Any, clientErr error) {
func (tsc *HTTPDemoGoTSRPCClient) Any(ctx go_context.Context, any github_com_foomo_gotsrpc_v2_demo_nested.Any, anyList []github_com_foomo_gotsrpc_v2_demo_nested.Any, anyMap map[string]github_com_foomo_gotsrpc_v2_demo_nested.Any) (retAny_0 github_com_foomo_gotsrpc_v2_demo_nested.Any, retAny_1 []github_com_foomo_gotsrpc_v2_demo_nested.Any, retAny_2 map[string]github_com_foomo_gotsrpc_v2_demo_nested.Any, clientErr error) {
args := []interface{}{any, anyList, anyMap}
reply := []interface{}{&retAny_0, &retAny_1, &retAny_2}
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "Any", args, reply)
clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "Any", args, reply)
return
}
func (tsc *HTTPDemoGoTSRPCClient) ExtractAddress(person *Person) (addr *Address, e *Err, clientErr error) {
func (tsc *HTTPDemoGoTSRPCClient) ExtractAddress(ctx go_context.Context, person *Person) (addr *Address, e *Err, clientErr error) {
args := []interface{}{person}
reply := []interface{}{&addr, &e}
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "ExtractAddress", args, reply)
clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "ExtractAddress", args, reply)
return
}
func (tsc *HTTPDemoGoTSRPCClient) GiveMeAScalar() (amount github_com_foomo_gotsrpc_v2_demo_nested.Amount, wahr github_com_foomo_gotsrpc_v2_demo_nested.True, hier ScalarInPlace, clientErr error) {
func (tsc *HTTPDemoGoTSRPCClient) GiveMeAScalar(ctx go_context.Context) (amount github_com_foomo_gotsrpc_v2_demo_nested.Amount, wahr github_com_foomo_gotsrpc_v2_demo_nested.True, hier ScalarInPlace, clientErr error) {
args := []interface{}{}
reply := []interface{}{&amount, &wahr, &hier}
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "GiveMeAScalar", args, reply)
clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "GiveMeAScalar", args, reply)
return
}
func (tsc *HTTPDemoGoTSRPCClient) Hello(name string) (retHello_0 string, retHello_1 *Err, clientErr error) {
func (tsc *HTTPDemoGoTSRPCClient) Hello(ctx go_context.Context, name string) (retHello_0 string, retHello_1 *Err, clientErr error) {
args := []interface{}{name}
reply := []interface{}{&retHello_0, &retHello_1}
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "Hello", args, reply)
clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "Hello", args, reply)
return
}
func (tsc *HTTPDemoGoTSRPCClient) HelloInterface(anything interface{}, anythingMap map[string]interface{}, anythingSlice []interface{}) (clientErr error) {
func (tsc *HTTPDemoGoTSRPCClient) HelloInterface(ctx go_context.Context, anything interface{}, anythingMap map[string]interface{}, anythingSlice []interface{}) (clientErr error) {
args := []interface{}{anything, anythingMap, anythingSlice}
reply := []interface{}{}
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "HelloInterface", args, reply)
clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "HelloInterface", args, reply)
return
}
func (tsc *HTTPDemoGoTSRPCClient) HelloNumberMaps(intMap map[int]string) (floatMap map[float64]string, clientErr error) {
func (tsc *HTTPDemoGoTSRPCClient) HelloNumberMaps(ctx go_context.Context, intMap map[int]string) (floatMap map[float64]string, clientErr error) {
args := []interface{}{intMap}
reply := []interface{}{&floatMap}
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "HelloNumberMaps", args, reply)
clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "HelloNumberMaps", args, reply)
return
}
func (tsc *HTTPDemoGoTSRPCClient) HelloScalarError() (err *ScalarError, clientErr error) {
func (tsc *HTTPDemoGoTSRPCClient) HelloScalarError(ctx go_context.Context) (err *ScalarError, clientErr error) {
args := []interface{}{}
reply := []interface{}{&err}
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "HelloScalarError", args, reply)
clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "HelloScalarError", args, reply)
return
}
func (tsc *HTTPDemoGoTSRPCClient) MapCrap() (crap map[string][]int, clientErr error) {
func (tsc *HTTPDemoGoTSRPCClient) MapCrap(ctx go_context.Context) (crap map[string][]int, clientErr error) {
args := []interface{}{}
reply := []interface{}{&crap}
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "MapCrap", args, reply)
clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "MapCrap", args, reply)
return
}
func (tsc *HTTPDemoGoTSRPCClient) Nest() (retNest_0 []*github_com_foomo_gotsrpc_v2_demo_nested.Nested, clientErr error) {
func (tsc *HTTPDemoGoTSRPCClient) Nest(ctx go_context.Context) (retNest_0 []*github_com_foomo_gotsrpc_v2_demo_nested.Nested, clientErr error) {
args := []interface{}{}
reply := []interface{}{&retNest_0}
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "Nest", args, reply)
clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "Nest", args, reply)
return
}
func (tsc *HTTPDemoGoTSRPCClient) TestScalarInPlace() (retTestScalarInPlace_0 ScalarInPlace, clientErr error) {
func (tsc *HTTPDemoGoTSRPCClient) TestScalarInPlace(ctx go_context.Context) (retTestScalarInPlace_0 ScalarInPlace, clientErr error) {
args := []interface{}{}
reply := []interface{}{&retTestScalarInPlace_0}
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "TestScalarInPlace", args, reply)
clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "TestScalarInPlace", args, reply)
return
}
type BarGoTSRPCClient interface {
CustomError(one CustomError, two *CustomError) (three CustomError, four *CustomError, clientErr error)
CustomType(customTypeInt CustomTypeInt, customTypeString CustomTypeString, CustomTypeStruct CustomTypeStruct) (retCustomType_0 *CustomTypeInt, retCustomType_1 *CustomTypeString, retCustomType_2 CustomTypeStruct, clientErr error)
Hello(number int64) (retHello_0 int, clientErr error)
Inheritance(inner Inner, nested OuterNested, inline OuterInline) (retInheritance_0 Inner, retInheritance_1 OuterNested, retInheritance_2 OuterInline, clientErr error)
Repeat(one string, two string) (three bool, four bool, clientErr error)
CustomError(ctx go_context.Context, one CustomError, two *CustomError) (three CustomError, four *CustomError, clientErr error)
CustomType(ctx go_context.Context, customTypeInt CustomTypeInt, customTypeString CustomTypeString, CustomTypeStruct CustomTypeStruct) (retCustomType_0 *CustomTypeInt, retCustomType_1 *CustomTypeString, retCustomType_2 CustomTypeStruct, clientErr error)
Hello(ctx go_context.Context, number int64) (retHello_0 int, clientErr error)
Inheritance(ctx go_context.Context, inner Inner, nested OuterNested, inline OuterInline) (retInheritance_0 Inner, retInheritance_1 OuterNested, retInheritance_2 OuterInline, clientErr error)
Repeat(ctx go_context.Context, one string, two string) (three bool, four bool, clientErr error)
}
type HTTPBarGoTSRPCClient struct {
@ -167,44 +168,44 @@ func NewBarGoTSRPCClient(url string, endpoint string) *HTTPBarGoTSRPCClient {
return NewBarGoTSRPCClientWithClient(url, endpoint, nil)
}
func NewBarGoTSRPCClientWithClient(url string, endpoint string, client *net_http.Client) *HTTPBarGoTSRPCClient {
func NewBarGoTSRPCClientWithClient(url string, endpoint string, client *go_net_http.Client) *HTTPBarGoTSRPCClient {
return &HTTPBarGoTSRPCClient{
URL: url,
EndPoint: endpoint,
Client: gotsrpc.NewClientWithHttpClient(client),
}
}
func (tsc *HTTPBarGoTSRPCClient) CustomError(one CustomError, two *CustomError) (three CustomError, four *CustomError, clientErr error) {
func (tsc *HTTPBarGoTSRPCClient) CustomError(ctx go_context.Context, one CustomError, two *CustomError) (three CustomError, four *CustomError, clientErr error) {
args := []interface{}{one, two}
reply := []interface{}{&three, &four}
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "CustomError", args, reply)
clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "CustomError", args, reply)
return
}
func (tsc *HTTPBarGoTSRPCClient) CustomType(customTypeInt CustomTypeInt, customTypeString CustomTypeString, CustomTypeStruct CustomTypeStruct) (retCustomType_0 *CustomTypeInt, retCustomType_1 *CustomTypeString, retCustomType_2 CustomTypeStruct, clientErr error) {
func (tsc *HTTPBarGoTSRPCClient) CustomType(ctx go_context.Context, customTypeInt CustomTypeInt, customTypeString CustomTypeString, CustomTypeStruct CustomTypeStruct) (retCustomType_0 *CustomTypeInt, retCustomType_1 *CustomTypeString, retCustomType_2 CustomTypeStruct, clientErr error) {
args := []interface{}{customTypeInt, customTypeString, CustomTypeStruct}
reply := []interface{}{&retCustomType_0, &retCustomType_1, &retCustomType_2}
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "CustomType", args, reply)
clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "CustomType", args, reply)
return
}
func (tsc *HTTPBarGoTSRPCClient) Hello(number int64) (retHello_0 int, clientErr error) {
func (tsc *HTTPBarGoTSRPCClient) Hello(ctx go_context.Context, number int64) (retHello_0 int, clientErr error) {
args := []interface{}{number}
reply := []interface{}{&retHello_0}
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "Hello", args, reply)
clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "Hello", args, reply)
return
}
func (tsc *HTTPBarGoTSRPCClient) Inheritance(inner Inner, nested OuterNested, inline OuterInline) (retInheritance_0 Inner, retInheritance_1 OuterNested, retInheritance_2 OuterInline, clientErr error) {
func (tsc *HTTPBarGoTSRPCClient) Inheritance(ctx go_context.Context, inner Inner, nested OuterNested, inline OuterInline) (retInheritance_0 Inner, retInheritance_1 OuterNested, retInheritance_2 OuterInline, clientErr error) {
args := []interface{}{inner, nested, inline}
reply := []interface{}{&retInheritance_0, &retInheritance_1, &retInheritance_2}
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "Inheritance", args, reply)
clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "Inheritance", args, reply)
return
}
func (tsc *HTTPBarGoTSRPCClient) Repeat(one string, two string) (three bool, four bool, clientErr error) {
func (tsc *HTTPBarGoTSRPCClient) Repeat(ctx go_context.Context, one string, two string) (three bool, four bool, clientErr error) {
args := []interface{}{one, two}
reply := []interface{}{&three, &four}
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "Repeat", args, reply)
clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "Repeat", args, reply)
return
}

View File

@ -1,6 +1,7 @@
package demo
import (
"context"
"fmt"
"math/rand"
"net/http/httptest"
@ -9,8 +10,9 @@ import (
"testing"
"time"
"github.com/foomo/gotsrpc/v2/demo/nested"
"github.com/stretchr/testify/assert"
"github.com/foomo/gotsrpc/v2/demo/nested"
)
func init() {
@ -45,8 +47,9 @@ func teardown() {
func TestDefault(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
resp, errServer, errClient := client.Hello("stefan")
resp, errServer, errClient := client.Hello(ctx, "stefan")
assert.NoError(t, errClient)
assert.Nil(t, errServer)
fmt.Println(resp)
@ -55,8 +58,10 @@ func TestDefault(t *testing.T) {
func TestHelloNumberMaps(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
intMap := map[int]string{1: "one", 2: "two", 3: "three"}
floatMap, errClient := client.HelloNumberMaps(intMap)
floatMap, errClient := client.HelloNumberMaps(ctx, intMap)
assert.NoError(t, errClient)
for f, fstr := range floatMap {
i := int(f)
@ -67,11 +72,12 @@ func TestHelloNumberMaps(t *testing.T) {
func benchmarkRequests(b *testing.B, count int) {
setup()
defer teardown()
ctx := context.Background()
person := GeneratePerson(count)
b.ResetTimer()
for n := 0; n < b.N; n++ {
client.ExtractAddress(person)
client.ExtractAddress(ctx, person)
}
}

View File

@ -1,21 +1,24 @@
package tests
import (
"context"
"net/http/httptest"
"testing"
"github.com/foomo/gotsrpc/v2/demo"
"github.com/stretchr/testify/assert"
"github.com/foomo/gotsrpc/v2/demo"
)
func TestHello(t *testing.T) {
proxy := demo.NewDefaultBarGoTSRPCProxy(&Bar{})
server := httptest.NewServer(proxy)
defer server.Close()
ctx := context.Background()
client := demo.NewDefaultBarGoTSRPCClient(server.URL)
if res, clientErr := client.Hello(10); assert.NoError(t, clientErr) {
if res, clientErr := client.Hello(ctx, 10); assert.NoError(t, clientErr) {
assert.Equal(t, 10, res)
}
}
@ -24,17 +27,18 @@ func TestCustomError(t *testing.T) {
proxy := demo.NewDefaultBarGoTSRPCProxy(&Bar{})
server := httptest.NewServer(proxy)
defer server.Close()
ctx := context.Background()
client := demo.NewDefaultBarGoTSRPCClient(server.URL)
if three, four, clientErr := client.CustomError("", nil); assert.NoError(t, clientErr) {
if three, four, clientErr := client.CustomError(ctx, "", nil); assert.NoError(t, clientErr) {
assert.Equal(t, demo.CustomError(""), three)
assert.Nil(t, four)
}
one := demo.CustomErrorDemo
two := demo.ErrCustomDemo
if three, four, clientErr := client.CustomError(one, two); assert.NoError(t, clientErr) {
if three, four, clientErr := client.CustomError(ctx, one, two); assert.NoError(t, clientErr) {
assert.NotNil(t, three)
assert.NotNil(t, four)
assert.ErrorIs(t, demo.ErrCustomDemo, two)

10
go.go
View File

@ -347,6 +347,7 @@ type goMethod struct {
func newMethodSignature(method *Method, aliases map[string]string, fullPackageName string) goMethod {
var args []string
var params []string
params = append(params, "ctx go_context.Context")
for _, a := range goMethodArgsWithoutHTTPContextRelatedArgs(method) {
args = append(args, a.Name)
params = append(params, a.Name+" "+a.Value.goType(aliases, fullPackageName))
@ -379,7 +380,8 @@ func (ms *goMethod) renderSignature() string {
func renderTSRPCServiceClients(services ServiceList, fullPackageName string, packageName string, config *config.Target, g *code) error {
aliases := map[string]string{
"github.com/foomo/gotsrpc/v2": "gotsrpc",
"net/http": "net_http",
"net/http": "go_net_http",
"context": "go_context",
}
for _, service := range services {
@ -431,7 +433,7 @@ func renderTSRPCServiceClients(services ServiceList, fullPackageName string, pac
return New` + interfaceName + `WithClient(url, endpoint, nil)
}
func New` + interfaceName + `WithClient(url string, endpoint string, client *net_http.Client) *` + clientName + ` {
func New` + interfaceName + `WithClient(url string, endpoint string, client *go_net_http.Client) *` + clientName + ` {
return &` + clientName + `{
URL: url,
EndPoint: endpoint,
@ -444,7 +446,7 @@ func renderTSRPCServiceClients(services ServiceList, fullPackageName string, pac
g.l(`func (tsc *` + clientName + `) ` + ms.renderSignature() + ` {`)
g.l(`args := []interface{}{` + strings.Join(ms.args, ", ") + `}`)
g.l(`reply := []interface{}{` + strings.Join(ms.rets, ", ") + `}`)
g.l(`clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "` + method.Name + `", args, reply)`)
g.l(`clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "` + method.Name + `", args, reply)`)
g.l(`return`)
g.l(`}`)
g.nl()
@ -778,7 +780,7 @@ func renderImports(aliases map[string]string, packageName string) string {
imports += alias + " \"" + importPath + "\"\n"
}
return `
// Code generated by gotsrpc https://github.com/foomo/gotsrpc - DO NOT EDIT.
// Code generated by gotsrpc https://github.com/foomo/gotsrpc/v2 - DO NOT EDIT.
package ` + packageName + `