/
ext.go
84 lines (75 loc) · 2.16 KB
/
ext.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
// 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 common
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"github.com/erda-project/erda-infra/providers/httpserver"
api "github.com/erda-project/erda/pkg/common/httpapi"
)
// ExtResponse .
type ExtResponse struct {
Success bool `json:"success,omitempty"`
Data interface{} `json:"data,omitempty"`
Err *api.Error `json:"err,omitempty"`
UserIDs []string `json:"userIDs,omitempty"`
status int
}
// Error .
func (r *ExtResponse) Error(ctx httpserver.Context) error { return nil }
// Status .
func (r *ExtResponse) Status(ctx httpserver.Context) int { return r.status }
// ReadCloser .
func (r *ExtResponse) ReadCloser(ctx httpserver.Context) io.ReadCloser {
byts, err := json.Marshal(r)
if err != nil {
byts, _ = json.Marshal(&api.Response{
Success: false,
Err: &api.Error{
Code: api.InternalError.Code(),
Msg: err.Error(),
},
})
r.status = api.InternalError.Status()
}
return ioutil.NopCloser(bytes.NewBuffer(byts))
}
func (r *ExtResponse) Response(ctx httpserver.Context) httpserver.Response {
if r.Err != nil {
errMsg, ok := r.Err.Msg.(api.ErrorMessage)
if ok {
r.Err.Msg = errMsg.Message(ctx)
}
}
header := ctx.ResponseWriter().Header()
if header.Get("Content-Type") == "" {
header.Set("Content-Type", "application/json; charset=UTF-8")
}
return r
}
// SuccessExt .
func SuccessExt(data interface{}, userIDs []string, status ...int) *ExtResponse {
var statusCode int
if len(status) > 0 {
statusCode = status[0]
}
return &ExtResponse{
Success: true,
Data: data,
UserIDs: userIDs,
status: statusCode,
}
}