-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbase_model.go
88 lines (69 loc) · 1.55 KB
/
base_model.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
package coze
import "net/http"
type Responser interface {
Response() HTTPResponse
}
type HTTPResponse interface {
LogID() string
}
type httpResponse struct {
Status int
Header http.Header
ContentLength int64
logid string
}
func (r *httpResponse) LogID() string {
if r.logid == "" {
r.logid = r.Header.Get(logIDHeader)
}
return r.logid
}
type baseResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
HTTPResponse *httpResponse `json:"http_response"`
}
func (r *baseResponse) SetHTTPResponse(httpResponse *httpResponse) {
r.HTTPResponse = httpResponse
}
func (r *baseResponse) SetCode(code int) {
r.Code = code
}
func (r *baseResponse) SetMsg(msg string) {
r.Msg = msg
}
func (r *baseResponse) GetCode() int {
return r.Code
}
func (r *baseResponse) GetMsg() string {
return r.Msg
}
func (r *baseResponse) LogID() string {
return r.HTTPResponse.LogID()
}
type baseRespInterface interface {
SetHTTPResponse(httpResponse *httpResponse)
SetCode(code int)
SetMsg(msg string)
GetMsg() string
GetCode() int
}
type baseModel struct {
httpResponse *httpResponse
}
func (r *baseModel) setHTTPResponse(httpResponse *httpResponse) {
r.httpResponse = httpResponse
}
func (r *baseModel) Response() HTTPResponse {
return r.httpResponse
}
func (r *baseModel) LogID() string {
return r.httpResponse.LogID()
}
func newHTTPResponse(resp *http.Response) *httpResponse {
return &httpResponse{
Status: resp.StatusCode,
Header: resp.Header,
ContentLength: resp.ContentLength,
}
}