-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_formatter.go
70 lines (60 loc) · 1.74 KB
/
response_formatter.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
package api
import (
"fmt"
"strings"
"github.com/jgolang/api/core"
)
// ResponseFormatter implementation of core.APIResponseFormatter interface.
type ResponseFormatter struct{}
// TraceIDLength specifies the number of characters to return from the beginning of the input trace ID.
var TraceIDLength = 6
// shortID returns the first n characters of the input traceID
func shortID(traceID string) string {
if len(traceID) < TraceIDLength {
return traceID
}
return traceID[:TraceIDLength]
}
// TraceVisibility controls how trace information is included in the response message.
// - 1: Includes both event ID and response code.
// - 2: Includes only the Code.
// - 3: Includes only the event ID.
var TraceVisibility = 1
// BlankSuccess contols if success include user feeback information
var BlankSuccess = true
// Format the response body information.
func (formatter ResponseFormatter) Format(data core.ResponseData) *core.ResponseFormatted {
msg := data.Message
title := data.Title
code := strings.ToLower(string(data.ResponseCode))
if TraceVisibility == 1 {
msg = fmt.Sprintf("%s (%s-%s)", data.Message, shortID(data.EventID), code)
}
if TraceVisibility == 2 {
msg = fmt.Sprintf("%s (%s)", data.Message, code)
}
if TraceVisibility == 3 {
msg = fmt.Sprintf("%s (%s)", data.Message, shortID(data.EventID))
}
if BlankSuccess {
msg = ""
title = ""
}
return &core.ResponseFormatted{
HTTPStatusCode: data.HTTPStatusCode,
Headers: data.Headers,
Body: JSONResponse{
Content: data.Content,
Header: JSONResponseInfo{
Title: title,
Message: msg,
Type: data.ResponseType,
Action: data.Actions,
Token: data.SecurityToken,
Code: code,
EventID: data.EventID,
Info: data.Info,
},
},
}
}