TSRPC Call Cleanup (#23)

* chore: add content-length and remove argument callbacks

* chore: refactor gotsrpc client

* chore: update generated code to drain request body and re-generate
This commit is contained in:
Stefan Martinov 2020-03-17 16:56:14 +01:00 committed by GitHub
parent 33a5bca6d7
commit 3e8dbee6e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 18 deletions

View File

@ -3,7 +3,6 @@ package gotsrpc
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
@ -39,8 +38,11 @@ func NewClientWithHttpClient(client *http.Client) Client {
}
}
func newRequest(url string, contentType string, reader io.Reader, headers http.Header) (r *http.Request, err error) {
request, errRequest := http.NewRequest("POST", url, reader)
func newRequest(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)
if errRequest != nil {
return nil, errors.Wrap(errRequest, "could not create a request")
}
@ -76,15 +78,17 @@ func (c *bufferedClient) SetTransportHttpClient(client *http.Client) {
func (c *bufferedClient) Call(url string, endpoint string, method string, args []interface{}, reply []interface{}) (err error) {
// Marshall args
b := new(bytes.Buffer)
errEncode := codec.NewEncoder(b, c.handle.handle).Encode(args)
if errEncode != nil {
return errors.Wrap(errEncode, "could not encode argument")
// If no arguments are set, remove
if len(args) > 0 {
if err := codec.NewEncoder(b, c.handle.handle).Encode(args); err != nil {
return errors.Wrap(err, "could not encode argument")
}
}
// Create request
// Create post url
postURL := fmt.Sprintf("%s%s/%s", url, endpoint, method)
// Post
request, errRequest := newRequest(postURL, c.handle.contentType, b, c.headers.Clone())
if errRequest != nil {
@ -95,10 +99,10 @@ func (c *bufferedClient) Call(url string, endpoint string, method string, args [
if errDo != nil {
return errors.Wrap(errDo, "could not execute request")
}
defer resp.Body.Close()
// Check status
if resp.StatusCode != http.StatusOK {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
@ -106,13 +110,10 @@ func (c *bufferedClient) Call(url string, endpoint string, method string, args [
return fmt.Errorf("%s: %s", resp.Status, string(body))
}
var errDecode error
responseHandle := getHandlerForContentType(resp.Header.Get("Content-Type")).handle
errDecode = codec.NewDecoder(resp.Body, responseHandle).Decode(reply)
// Unmarshal reply
if errDecode != nil {
return errors.Wrap(errDecode, "could not decode response from client")
if err := codec.NewDecoder(resp.Body, responseHandle).Decode(reply); err != nil {
return errors.Wrap(err, "could not decode response from client")
}
return err
}

View File

@ -3,6 +3,8 @@
package demo
import (
io "io"
ioutil "io/ioutil"
http "net/http"
time "time"
@ -46,6 +48,7 @@ func (p *FooGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
gotsrpc.ErrorMethodNotAllowed(w)
return
}
defer io.Copy(ioutil.Discard, r.Body) // Drain Request Body
var args []interface{}
funcName := gotsrpc.GetCalledFunc(r, p.EndPoint)
@ -116,6 +119,7 @@ func (p *DemoGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
gotsrpc.ErrorMethodNotAllowed(w)
return
}
defer io.Copy(ioutil.Discard, r.Body) // Drain Request Body
var args []interface{}
funcName := gotsrpc.GetCalledFunc(r, p.EndPoint)

View File

@ -24,7 +24,7 @@ func NewDefaultFooGoTSRPCClient(url string) *HTTPFooGoTSRPCClient {
}
func NewFooGoTSRPCClient(url string, endpoint string) *HTTPFooGoTSRPCClient {
return NewFooGoTSRPCClientWithClient(url, "/service/foo", nil)
return NewFooGoTSRPCClientWithClient(url, endpoint, nil)
}
func NewFooGoTSRPCClientWithClient(url string, endpoint string, client *net_http.Client) *HTTPFooGoTSRPCClient {
@ -64,7 +64,7 @@ func NewDefaultDemoGoTSRPCClient(url string) *HTTPDemoGoTSRPCClient {
}
func NewDemoGoTSRPCClient(url string, endpoint string) *HTTPDemoGoTSRPCClient {
return NewDemoGoTSRPCClientWithClient(url, "/service/demo", nil)
return NewDemoGoTSRPCClientWithClient(url, endpoint, nil)
}
func NewDemoGoTSRPCClientWithClient(url string, endpoint string, client *net_http.Client) *HTTPDemoGoTSRPCClient {

6
go.go
View File

@ -174,9 +174,10 @@ func renderTSRPCServiceProxies(services ServiceList, fullPackageName string, pac
aliases := map[string]string{
"time": "time",
"net/http": "http",
"io": "io",
"io/ioutil": "ioutil",
"github.com/foomo/gotsrpc": "gotsrpc",
}
for _, service := range services {
// Check if we should render this service as ts rpc
// Note: remove once there's a separate gorcp generator
@ -236,6 +237,7 @@ func renderTSRPCServiceProxies(services ServiceList, fullPackageName string, pac
gotsrpc.ErrorMethodNotAllowed(w)
return
}
defer io.Copy(ioutil.Discard, r.Body) // Drain Request Body
`)
needsArgs := false
for _, method := range service.Methods {
@ -432,7 +434,7 @@ func renderTSRPCServiceClients(services ServiceList, fullPackageName string, pac
}
func New` + interfaceName + `(url string, endpoint string) *` + clientName + ` {
return New` + interfaceName + `WithClient(url, "` + service.Endpoint + `", nil)
return New` + interfaceName + `WithClient(url, endpoint, nil)
}
func New` + interfaceName + `WithClient(url string, endpoint string, client *net_http.Client) *` + clientName + ` {