/
tpl_httpclient.go
84 lines (70 loc) · 2.78 KB
/
tpl_httpclient.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
package genhandler
import "text/template"
var clientTemplate = template.Must(template.New("http-client").Funcs(funcMap).Option().Parse(`
{{ range $svc := .Services }}
{{ if $svc | hasBindings -}}
type {{ $svc.GetName | goTypeName }}_httpClient struct {
c *{{ pkg "http" }}Client
host string
}
// New{{ $svc.GetName | goTypeName }}HTTPClient creates new HTTP client for {{ $svc.GetName | goTypeName }}Server.
// Pass addr in format "http://host[:port]".
func New{{ $svc.GetName | goTypeName }}HTTPClient(c *{{ pkg "http" }}Client,addr string) *{{ $svc.GetName | goTypeName }}_httpClient {
if {{ pkg "strings" }}HasSuffix(addr, "/") {
addr = addr[:len(addr)-1]
}
return &{{ $svc.GetName | goTypeName }}_httpClient{c:c,host:addr}
}
{{ end }}
{{ range $m := $svc.Methods }}
{{ if $m.Bindings }}
{{ with $b := index $m.Bindings 0 }}
func (c *{{ $svc.GetName | goTypeName }}_httpClient) {{ $m.GetName | goTypeName }}(ctx {{ pkg "context" }}Context,in *{{ $m.RequestType.GoType $m.Service.File.GoPkg.Path | goTypeName }},opts ...{{ pkg "grpc" }}CallOption) (*{{ $m.ResponseType.GoType $m.Service.File.GoPkg.Path | goTypeName }},error) {
mw,err := {{ pkg "httpclient" }}NewMiddlewareGRPC(opts)
if err != nil {
return nil,err
}
path := pattern_goclay_{{ $svc.GetName | goTypeName }}_{{ $m.GetName }}_{{ $b.Index }}_builder(in)
buf := {{ pkg "bytes" }}NewBuffer(nil)
m := {{ pkg "httpruntime" }}DefaultMarshaler(nil)
{{ if $b.Body }}
if err = m.Marshal(buf, {{.Body.AssignableExpr "in"}}); err != nil {
return nil, {{ pkg "errors" }}Wrap(err, "can't marshal request")
}
{{ end }}
req, err := {{ pkg "http" }}NewRequest("{{ $b.HTTPMethod }}", c.host+path, buf)
if err != nil {
return nil, {{ pkg "errors" }}Wrap(err, "can't initiate HTTP request")
}
req = req.WithContext(ctx)
req.Header.Add("Accept", m.ContentType())
req,err = mw.ProcessRequest(req)
if err != nil {
return nil,err
}
rsp, err := c.c.Do(req)
if err != nil {
return nil, {{ pkg "errors" }}Wrap(err, "error from client")
}
defer rsp.Body.Close()
rsp,err = mw.ProcessResponse(rsp)
if err != nil {
return nil,err
}
if rsp.StatusCode >= 400 {
b,_ := {{ pkg "ioutil" }}ReadAll(rsp.Body)
return nil,{{ pkg "errors" }}Errorf("%v %v: server returned HTTP %v: '%v'",req.Method,req.URL.String(),rsp.StatusCode,string(b))
}
ret := {{$m.ResponseType.GoType $m.Service.File.GoPkg.Path | goTypeName }}{}
{{ if $b | ResponseBody }}
err = m.Unmarshal(rsp.Body, &{{ .ResponseBody.AssignableExpr "ret"}})
{{ else }}
err = m.Unmarshal(rsp.Body, &ret)
{{ end }}
return &ret, {{ pkg "errors" }}Wrap(err, "can't unmarshal response")
}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
`))