Skip to content

Commit

Permalink
Merge pull request #20 from sas1024/master
Browse files Browse the repository at this point in the history
Golang client: improvements
  • Loading branch information
sas1024 authored Nov 15, 2022
2 parents a4ede6e + 6ca2dc6 commit 5d23674
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
18 changes: 11 additions & 7 deletions golang/go_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ func newRPCClient(endpoint string, header http.Header, httpClient *http.Client)
}
}
func (c *rpcClient) call(ctx context.Context, methodName string, request, result interface{}) error {
func (rc *rpcClient) call(ctx context.Context, methodName string, request, result interface{}) error {
// encode params
bts, err := json.Marshal(request)
if err != nil {
return fmt.Errorf("encode params: %w", err)
}
requestID := atomic.AddUint64(&c.requestID, 1)
requestID := atomic.AddUint64(&rc.requestID, 1)
requestIDBts := json.RawMessage(strconv.Itoa(int(requestID)))
req := zenrpc.Request{
Expand All @@ -133,7 +133,7 @@ func (c *rpcClient) call(ctx context.Context, methodName string, request, result
Params: bts,
}
res, err := c.Exec(ctx, req)
res, err := rc.Exec(ctx, req)
if err != nil {
return err
}
Expand Down Expand Up @@ -177,7 +177,7 @@ func (rc *rpcClient) Exec(ctx context.Context, rpcReq zenrpc.Request) (*zenrpc.R
req.Header = rc.header.Clone()
req.Header.Add("Content-Type", "application/json")
if xRequestID, ok := ctx.Value("X-Request-Id").(string); ok && xRequestID != "" {
if xRequestID, ok := ctx.Value("X-Request-Id").(string); ok && req.Header.Get("X-Request-Id") == "" && xRequestID != "" {
req.Header.Add("X-Request-Id", xRequestID)
}
Expand All @@ -195,16 +195,20 @@ func (rc *rpcClient) Exec(ctx context.Context, rpcReq zenrpc.Request) (*zenrpc.R
return nil, fmt.Errorf("bad response (%d)", resp.StatusCode)
}
var zresp *zenrpc.Response
if rpcReq.ID == nil {
return zresp, nil
}
bb, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("response body (%s) read failed: %w", bb, err)
}
var zresp zenrpc.Response
if err = json.Unmarshal(bb, &zresp); err != nil {
if err = json.Unmarshal(bb, zresp); err != nil {
return nil, fmt.Errorf("json decode failed (%s): %w", bb, err)
}
return &zresp, nil
return zresp, nil
}
`
18 changes: 11 additions & 7 deletions golang/testdata/catalogue_client.go

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

0 comments on commit 5d23674

Please sign in to comment.