base_service_gateway.go
4.0 KB
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
168
169
170
171
172
173
174
package api
import (
rawjson "encoding/json"
"fmt"
"github.com/linmadan/egglib-go/utils/json"
"net/http"
"strings"
"time"
"github.com/beego/beego/v2/client/httplib"
)
type MessageCode struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
// Response 统一消息返回格式
type Response struct {
MessageCode
Data rawjson.RawMessage `json:"data"`
}
type BaseServiceGateway struct {
ConnectTimeout time.Duration
ReadWriteTimeout time.Duration
host string
Interceptor func(msg string)
ServiceName string
}
type Request struct {
Url string
Method string
Param interface{}
}
func (gateway BaseServiceGateway) CreateRequest(url string, method string, options *RequestOptions) *httplib.BeegoHTTPRequest {
var request *httplib.BeegoHTTPRequest
method = strings.ToUpper(method)
switch method {
case http.MethodGet:
request = httplib.Get(url)
case http.MethodPost:
request = httplib.Post(url)
case http.MethodPut:
request = httplib.Put(url)
case http.MethodDelete:
request = httplib.Delete(url)
case http.MethodHead:
request = httplib.Head(url)
default:
request = httplib.Get(url)
}
if len(options.Header) > 0 {
for k, v := range options.Header {
request.Header(k, strings.Join(v, ";"))
}
}
for k, v := range options.FileMap {
request.PostFile(k, v)
}
return request.SetTimeout(gateway.ConnectTimeout, gateway.ReadWriteTimeout)
}
func (gateway BaseServiceGateway) GetResponseData(result Response, data interface{}) error {
err := json.Unmarshal(result.Data, data)
if err != nil {
return NewErrCodeMsg(gateway.ServiceName, JsonUnMarshError, err.Error())
}
return nil
}
func (gateway BaseServiceGateway) FastDoRequest(url, method string, param interface{}, data interface{}, options ...Option) error {
begin := time.Now()
var err error
var result = "success"
defer func() {
jsonParam, _ := json.Marshal(param)
jsonData, _ := json.Marshal(data)
if err != nil {
result = err.Error()
}
if gateway.Interceptor != nil {
gateway.Interceptor(fmt.Sprintf("【网关】%v | %v | %v : %v \nRequest:%v \nResponse:%v", time.Since(begin), url, strings.ToUpper(method),
result,
string(jsonParam),
string(jsonData),
))
}
}()
var requestOptions = &RequestOptions{}
for _, option := range options {
option(requestOptions)
}
err = gateway.DoRequest(Request{
Url: url,
Method: method,
Param: param,
}, &data, requestOptions)
if err != nil {
return err
}
return nil
}
func (gateway BaseServiceGateway) DoRequest(requestParam Request, val interface{}, options *RequestOptions) error {
r := gateway.CreateRequest(requestParam.Url, requestParam.Method, options)
var err error
if len(options.FileMap) == 0 {
r, err = r.JSONBody(requestParam.Param)
if err != nil {
return err
}
}
byteResult, err := r.Bytes()
if err != nil {
gateway.InterceptSimple(requestParam.Url, string(byteResult), err)
return err
}
var result Response
err = json.Unmarshal(byteResult, &result)
if err != nil {
gateway.InterceptSimple(requestParam.Url, string(byteResult), err)
return err
}
if result.Code != 0 && len(result.Msg) > 0 {
return NewErrCodeMsg(gateway.ServiceName, result.Code, result.Msg)
}
return gateway.GetResponseData(result, val)
}
func (gateway BaseServiceGateway) InterceptSimple(url string, data string, err error) {
if gateway.Interceptor != nil {
errMsg := ""
if err != nil {
errMsg = err.Error()
}
gateway.Interceptor(fmt.Sprintf("【网关】 %v | %v \nResponse:%v Error:%v", url,
data, errMsg,
))
}
}
func (gateway BaseServiceGateway) Host() string {
return gateway.host
}
func NewBaseServiceGateway(host string) BaseServiceGateway {
return BaseServiceGateway{
host: host,
}
}
type RequestOptions struct {
Header http.Header
// key:form key value:path
FileMap map[string]string
}
type Option func(o *RequestOptions)
func WithHeader(header http.Header) Option {
return func(o *RequestOptions) {
o.Header = header
}
}
func WithFileMap(v map[string]string) Option {
return func(o *RequestOptions) {
o.FileMap = v
}
}