-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponse.go
108 lines (93 loc) · 2.88 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
package gouncer
import (
"encoding/json"
"encoding/xml"
"fmt"
"net/http"
"strconv"
"strings"
)
type ResponseHandler struct {
Writer http.ResponseWriter
HttpRequest *http.Request
Response *Response
JsonP bool
}
type Response struct {
Status int `json:"status,omitempty" xml:"Status,attr,omitempty"`
HttpMessage string `json:"http_message,omitempty" xml:"HttpMessage,omitempty"`
Error string `json:"error,omitempty" xml:"Error,omitempty"`
Message string `json:"message,omitempty" xml:"Message,omitempty"`
Token string `json:"token,omitempty" xml:"Token,omitempty"`
AccessRights interface{} `json:"rights,omitempty" xml:"Access>Right,omitempty"`
Info *Info `json:"info,omitempty" xml:",omitempty"`
}
func NewResponseHandler(w http.ResponseWriter, r *http.Request) *ResponseHandler {
return &ResponseHandler{
Response: &Response{},
Writer: w,
HttpRequest: r,
}
}
// NewError loads error data into the Response structure
func (resp *ResponseHandler) NewError(status int, err string) {
resp.Response = &Response{
Status: status,
HttpMessage: http.StatusText(status),
Error: err,
}
}
// NewResponse loads response data into the Response structure
func (resp *ResponseHandler) NewResponse(status int, message string) {
resp.Response = &Response{
Status: status,
HttpMessage: http.StatusText(status),
Message: message,
}
}
func (h *ResponseHandler) Respond() {
switch strings.ToLower(h.HttpRequest.Header.Get("Accept")) {
case "text/plain":
h.RespondText()
case "application/xml", "text/xml":
h.RespondXml()
default:
h.RespondJson()
}
}
func (h *ResponseHandler) RespondText() {
if h.Response.Error == "" {
if h.Response.Token != "" {
fmt.Fprintf(h.Writer, "%v", h.Response.Token)
} else if h.Response.AccessRights != nil {
fmt.Fprintf(h.Writer, "%v", h.Response.AccessRights)
} else {
fmt.Fprintf(h.Writer, "%+v", h.Response.Info)
}
} else {
h.Writer.WriteHeader(h.Response.Status)
h.Writer.Write([]byte(h.Response.String()))
}
}
func (h *ResponseHandler) RespondJson() {
var body []byte
h.Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
h.Writer.WriteHeader(h.Response.Status)
body, _ = json.Marshal(h.Response)
if callback := h.HttpRequest.FormValue("callback"); callback != "" && h.JsonP {
fmt.Fprintf(h.Writer, "%s(%s)", callback, body)
} else {
h.Writer.Write(body)
}
}
func (h *ResponseHandler) RespondXml() {
var body []byte
h.Writer.Header().Set("Content-Type", "application/xml; charset=utf-8")
h.Writer.WriteHeader(h.Response.Status)
body, _ = xml.MarshalIndent(h.Response, "", " ")
h.Writer.Write(body)
}
// String converts the Response struct to a string
func (r *Response) String() string {
return strconv.Itoa(r.Status) + " - " + r.HttpMessage + ": [Error]" + r.Error + " [Message]" + r.Message
}