-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhttp_client.go
87 lines (74 loc) · 1.72 KB
/
http_client.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
// Code generated by kun; DO NOT EDIT.
// github.com/RussellLuo/kun
package helloworld
import (
"context"
"net/http"
"net/url"
"strings"
"github.com/RussellLuo/kun/pkg/httpcodec"
)
type HTTPClient struct {
codecs httpcodec.Codecs
httpClient *http.Client
scheme string
host string
pathPrefix string
}
func NewHTTPClient(codecs httpcodec.Codecs, httpClient *http.Client, baseURL string) (*HTTPClient, error) {
u, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
return &HTTPClient{
codecs: codecs,
httpClient: httpClient,
scheme: u.Scheme,
host: u.Host,
pathPrefix: strings.TrimSuffix(u.Path, "/"),
}, nil
}
func (c *HTTPClient) SayHello(ctx context.Context, name string) (message string, err error) {
codec := c.codecs.EncodeDecoder("SayHello")
path := "/messages"
u := &url.URL{
Scheme: c.scheme,
Host: c.host,
Path: c.pathPrefix + path,
}
reqBody := struct {
Name string `json:"name"`
}{
Name: name,
}
reqBodyReader, headers, err := codec.EncodeRequestBody(&reqBody)
if err != nil {
return "", err
}
_req, err := http.NewRequestWithContext(ctx, "POST", u.String(), reqBodyReader)
if err != nil {
return "", err
}
for k, v := range headers {
_req.Header.Set(k, v)
}
_resp, err := c.httpClient.Do(_req)
if err != nil {
return "", err
}
defer _resp.Body.Close()
if _resp.StatusCode < http.StatusOK || _resp.StatusCode > http.StatusNoContent {
var respErr error
err := codec.DecodeFailureResponse(_resp.Body, &respErr)
if err == nil {
err = respErr
}
return "", err
}
respBody := &SayHelloResponse{}
err = codec.DecodeSuccessResponse(_resp.Body, respBody.Body())
if err != nil {
return "", err
}
return respBody.Message, nil
}