/
response.go
149 lines (133 loc) · 3.59 KB
/
response.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
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package httpserver httpserver统一封装,使API注册更容易
package httpserver
import (
"encoding/json"
"net/http"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/pkg/http/httpserver/ierror"
"github.com/erda-project/erda/pkg/i18n"
"github.com/erda-project/erda/pkg/strutil"
)
// Responser is an interface for http response
type Responser interface {
GetLocaledResp(locale *i18n.LocaleResource) HTTPResponse
GetStatus() int
GetContent() interface{}
}
// HTTPResponse is a struct contains status code and content body
type HTTPResponse struct {
Error ierror.IAPIError
Status int
Content interface{}
}
// GetStatus returns http status code.
func (r HTTPResponse) GetStatus() int {
return r.Status
}
// GetContent returns http content body
func (r HTTPResponse) GetContent() interface{} {
return r.Content
}
// GetLocaledResp
func (r HTTPResponse) GetLocaledResp(locale *i18n.LocaleResource) HTTPResponse {
if r.Error != nil {
return HTTPResponse{
Status: r.Status,
Content: Resp{
Success: false,
Err: apistructs.ErrorResponse{
Code: r.Error.Code(),
Msg: r.Error.Render(locale),
Ctx: r.Error.Ctx(),
},
},
}
}
return r
}
// Resp dice平台http body返回结构
type Resp struct {
Success bool `json:"success"`
Data interface{} `json:"data,omitempty"`
Err apistructs.ErrorResponse `json:"err,omitempty"`
UserIDs []string `json:"userIDs,omitempty"`
}
// ErrResp 采用httpserver框架时异常返回结果封装
func ErrResp(statusCode int, code, errMsg string) (Responser, error) {
return HTTPResponse{
Status: statusCode,
Content: Resp{
Success: false,
Err: apistructs.ErrorResponse{
Code: code,
Msg: errMsg,
},
},
}, nil
}
// OkResp 采用httpserver框架时正常返回结果封装
//
// 在 `userIDs` 中设置需要由 openapi 注入的用户信息的 ID 列表
func OkResp(data interface{}, userIDs ...[]string) (Responser, error) {
content := Resp{
Success: true,
Data: data,
}
if len(userIDs) > 0 {
content.UserIDs = strutil.DedupSlice(userIDs[0], true)
}
return HTTPResponse{
Status: http.StatusOK,
Content: content,
}, nil
}
// WriteYAML 响应yaml结构
func WriteYAML(w http.ResponseWriter, v string) {
w.Header().Set("Content-Type", "application/x-yaml; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(v))
if err != nil {
logrus.Debugln(err)
}
}
func WriteJSON(w http.ResponseWriter, v interface{}) {
w.Header().Set("Content-Type", ContentTypeJSON)
w.WriteHeader(http.StatusOK)
b, err := json.Marshal(v)
if err != nil {
logrus.Debugln(err)
}
_, err = w.Write(b)
if err != nil {
logrus.Debugln(err)
}
}
func WriteData(w http.ResponseWriter, v interface{}) {
WriteJSON(w, Resp{
Success: true,
Data: v,
})
}
func WriteErr(w http.ResponseWriter, code, errMsg string) {
WriteJSON(w, Resp{
Success: false,
Err: apistructs.ErrorResponse{
Code: code,
Msg: errMsg,
},
})
}