/
http.go
162 lines (146 loc) · 3.73 KB
/
http.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
package http
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"contrib.go.opencensus.io/exporter/stackdriver/propagation"
"go.opencensus.io/plugin/ochttp"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
)
type ResponseData struct {
Data json.RawMessage `json:"data,omitempty"`
}
type ResponseStatus struct {
Success bool `json:"success"`
ErrorMessage string `json:"error_msg,omitempty"`
}
// Response API response struct
type Response struct {
ResponseStatus
ResponseData
}
type ResponseMap map[string]interface{}
type Decoder interface {
Decode(body io.ReadCloser, res interface{}) error
}
type Unmarshaler interface {
Unmarshal(byte interface{}, data proto.Message) error
}
type APIErrorHandler interface {
HandleAPIError(res interface{}) error
}
type Client interface {
Get(ctx context.Context, url string, data proto.Message) error
Post(ctx context.Context, url string, body interface{}, data proto.Message) error
}
type client struct{}
func NewClient() *client {
return new(client)
}
func createHttpClient() *http.Client {
return &http.Client{
Transport: &ochttp.Transport{
Propagation: &propagation.HTTPFormat{},
},
}
}
//Get send GET HTTP request
func (h *client) Get(ctx context.Context, url string, data proto.Message) error {
httpClient := createHttpClient()
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return errors.Wrap(err, "http.NewRequest(\"GET\", url, nil)")
}
req = req.WithContext(ctx)
resp, err := httpClient.Do(req)
if err != nil {
return errors.Wrap(err, "client.Do(req)")
}
defer resp.Body.Close()
var res Response
if err = h.Decode(resp.Body, &res); err != nil {
return err
}
if err = h.HandleAPIError(res); err != nil {
return err
}
if err = h.Unmarshal(res, data); err != nil {
return err
}
return nil
}
//Post send POST HTTP request
func (h *client) Post(ctx context.Context, url string, body interface{}, data proto.Message) error {
var res ResponseMap
jsonValue, err := json.Marshal(body)
if err != nil {
return errors.Wrapf(err, "json.Marshal(body). body: %+v", body)
}
httpClient := createHttpClient()
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonValue))
if err != nil {
return errors.Wrap(err, "http.NewRequest(\"POST\", url, bytes.NewBuffer(jsonValue))")
}
req.Header.Set("Content-Type", "application/json")
req = req.WithContext(ctx)
resp, err := httpClient.Do(req)
if err != nil {
return errors.Wrapf(err, "httpClient.Do(req) req: %+v", req)
}
defer resp.Body.Close()
if err = h.Decode(resp.Body, &res); err != nil {
return err
}
if err = h.HandleAPIError(res); err != nil {
return err
}
if err = h.Unmarshal(res, data); err != nil {
return err
}
return nil
}
func (h *client) Decode(body io.ReadCloser, res interface{}) error {
err := json.NewDecoder(body).Decode(res)
if err != nil {
return errors.Wrapf(err, "json.NewDecoder(resp.Body).Decode(&res)")
}
return nil
}
func (h *client) Unmarshal(res interface{}, data proto.Message) error {
bs, err := json.Marshal(res)
if err != nil {
return errors.Wrapf(err, "json.Marshal(r). res: %+v", res)
}
um := jsonpb.Unmarshaler{}
err = um.Unmarshal(strings.NewReader(string(bs)), data)
if err != nil {
return errors.Wrapf(err, "json.Unmarshal. data: %s", string(bs))
}
return nil
}
func (h *client) HandleAPIError(res interface{}) error {
var (
success bool
errorMessage string
)
switch v := res.(type) {
case Response:
success = v.Success
errorMessage = v.ErrorMessage
case ResponseMap:
success = v["success"].(bool)
if v["error_msg"] != nil {
errorMessage = v["error_msg"].(string)
}
}
if !success {
return fmt.Errorf("API error: %s", errorMessage)
}
return nil
}