/
go_template.go
167 lines (133 loc) · 3.82 KB
/
go_template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package golang
// goTpl contains template for Go client
const goTpl = `// Code generated from jsonrpc schema by rpc_client_generator; DO NOT EDIT.
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"io/ioutil"
"sync/atomic"
"strconv"
"github.com/semrush/zenrpc"
)
type Client struct {
rpcClient *rpcClient
{{ range .NamespaceNames }}
{{ title . }} *{{ title .}}{{ end }}}
func NewDefaultClient(endpoint string) *Client {
return NewClient(endpoint, http.Header{}, &http.Client{})
}
func NewClient(endpoint string, header http.Header, httpClient *http.Client) *Client {
c := &Client{
rpcClient: newRPCClient(endpoint, header, httpClient),
}
{{ range .NamespaceNames }}
c.{{ title . }} = NewClient{{ title .}}(c.rpcClient){{ end }}
return c
}
{{ range .Models }}
type {{ .Name }} struct {
{{ range .Fields }}{{ title .Name }} {{ .GoType }} ` + "`json:\"{{ .Name }},omitempty\"`" + `
{{ end }}
}
{{ end }}
{{ range .NamespaceNames }}
{{ $lTitle := . }}{{ $namespace := . }}
type {{ title . }} struct {
client *rpcClient
}
func NewClient{{ title . }} (client *rpcClient) *{{ title . }} {
return &{{ title . }}{
client: client,
}
}
{{ range $.NamespaceMethodNames .}} {{ $method := $.Method $namespace . }}
func (c *{{ title $lTitle}}) {{ title . }}(ctx context.Context, {{ range $method.Params }}{{ .Name }} {{ .GoType }}, {{ end }}) ( {{ if $method.HasResult }} res {{ $method.Result.GoType }}, {{ else }}/*tak-tak*/ {{end}} err error) {
_req := struct {
{{ range $method.Params }}{{ title .Name }} {{ .GoType }}
{{ end }}
} {
{{ range $method.Params }}{{ title .Name }}: {{ .Name }}, {{ end }}
}
err = c.client.call(ctx, "{{ $namespace }}.{{ . }}", _req, {{ if $method.HasResult }} &res {{ else }} nil {{ end }})
return
}
{{ end }}
{{ end }}
type rpcClient struct {
endpoint string
cl *http.Client
requestID uint64
header http.Header
}
func newRPCClient(endpoint string, header http.Header, httpClient *http.Client) *rpcClient {
return &rpcClient{
endpoint: endpoint,
header: header,
cl: httpClient,
}
}
func (c *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)
requestIDBts := json.RawMessage(strconv.Itoa(int(requestID)))
req := zenrpc.Request{
Version: zenrpc.Version,
ID: &requestIDBts,
Method: methodName,
Params: bts,
}
res, err := c.Exec(ctx, req)
if err != nil {
return err
}
if res.Error != nil {
return res.Error
}
if res == nil || res.Result == nil {
return nil
}
return json.Unmarshal(*res.Result, result)
}
// Exec makes http request to jsonrpc endpoint and returns json rpc response.
func (rc *rpcClient) Exec(ctx context.Context, rpcReq zenrpc.Request) (*zenrpc.Response, error) {
c, err := json.Marshal(rpcReq)
if err != nil {
return nil, fmt.Errorf("json marshal call failed: %w", err)
}
buf := bytes.NewReader(c)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, rc.endpoint, buf)
if err != nil {
return nil, fmt.Errorf("create request failed: %w", err)
}
req.Header = rc.header
req.Header.Add("Content-Type", "application/json")
// Do request
resp, err := rc.cl.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, fmt.Errorf("make request failed: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad response (%d)", resp.StatusCode)
}
bb, err := ioutil.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 {
return nil, fmt.Errorf("json decode failed (%s): %w", bb, err)
}
return &zresp, nil
}
`