/
json_api_client.go
154 lines (122 loc) · 3.18 KB
/
json_api_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
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
package api_client
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/gogap/errors"
"github.com/gogap/spirit"
"github.com/mreiferson/go-httpclient"
)
var (
DefaultClientTimeout = time.Second * 30
)
const (
HeaderForwardedPayload = "X-Forwarded-Payload"
)
type HTTPAPIClient struct {
apiHeaderName string
url string
client *http.Client
}
func NewHTTPAPIClient(url string, apiHeaderName string, timeout time.Duration) APIClient {
url = strings.TrimSpace(url)
apiHeaderName = strings.TrimSpace(apiHeaderName)
if url == "" {
panic("url could not be nil")
}
if apiHeaderName == "" {
apiHeaderName = "X-Api"
}
if timeout <= 0 {
timeout = DefaultClientTimeout
}
transport := &httpclient.Transport{
ConnectTimeout: timeout,
RequestTimeout: timeout,
ResponseHeaderTimeout: timeout,
}
apiClient := HTTPAPIClient{
apiHeaderName: apiHeaderName,
url: url,
client: &http.Client{Transport: transport},
}
return &apiClient
}
func (p *HTTPAPIClient) Call(apiName string, payload spirit.Payload, v interface{}) (err error) {
apiName = strings.TrimSpace(apiName)
if apiName == "" {
err = ErrAPINameIsEmpty.New()
return
}
var payloadData interface{}
if payloadData, err = payload.GetData(); err != nil {
return
}
jsonPayload := JsonPayload{
Id: payload.Id(),
Data: payloadData,
Errors: payload.Errors(),
Context: payload.Context(),
}
var data []byte
if data, err = json.Marshal(jsonPayload); err != nil {
return
}
postBodyReader := bytes.NewReader(data)
var req *http.Request
if req, err = http.NewRequest("POST", p.url, postBodyReader); err != nil {
err = ErrAPIClientCreateNewRequestFailed.New().Append(err)
return
}
req.Header.Add(p.apiHeaderName, apiName)
req.Header.Add(HeaderForwardedPayload, "on")
var resp *http.Response
if resp, err = p.client.Do(req); err != nil {
err = ErrAPIClientSendFailed.New(errors.Params{"api": apiName, "url": p.url})
return
}
var body []byte
if resp != nil {
defer resp.Body.Close()
if bBody, e := ioutil.ReadAll(resp.Body); e != nil {
err = ErrAPIClientReadResponseBodyFailed.New(errors.Params{"api": apiName}).Append(e)
return
} else if resp.StatusCode != http.StatusOK {
err = ErrAPIClientBadStatusCode.New(errors.Params{"api": apiName, "code": resp.StatusCode})
return
} else {
body = bBody
}
if v == nil {
return
}
}
if v == nil {
return
}
var tmpResp struct {
Code uint64 `json:"code"`
ErrorId string `json:"error_id,omitempty"`
ErrorNamespace string `json:"error_namespace,omitempty"`
Message string `json:"message"`
Result interface{} `json:"result"`
}
tmpResp.Result = v
if e := json.Unmarshal(body, &tmpResp); e != nil {
err = ErrAPIClientResponseUnmarshalFailed.New(errors.Params{"api": apiName, "url": p.url}).Append(e)
return
}
if tmpResp.Code == 0 {
return
} else {
err = errors.NewErrorCode(tmpResp.ErrorId, tmpResp.Code, tmpResp.ErrorNamespace, tmpResp.Message, "", nil)
return
}
return
}
func (p *HTTPAPIClient) Cast(apiName string, payload spirit.Payload) {
go p.Call(apiName, payload, nil)
}