forked from uniqush/uniqush-push
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restapi_simpleresponse.go
60 lines (51 loc) · 1.31 KB
/
restapi_simpleresponse.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
package main
import (
"encoding/json"
"github.com/uniqush/log"
"time"
)
const (
STATUS_SUCCESS = iota
STATUS_FAILURE
STATUS_UNKNOWN
)
type ApiSimpleResponseHandler struct {
response ApiSimpleResponse
logger log.Logger
}
var _ ApiResponseHandler = (*ApiSimpleResponseHandler)(nil)
type ApiSimpleResponse struct {
Type string `json:"type"`
Date int64 `json:"date"`
Status int `json:"status"`
Details ApiResponseDetails `json:"details"`
}
func newSimpleResponseHandler(logger log.Logger, apiType string) *ApiSimpleResponseHandler {
return &ApiSimpleResponseHandler{
response: newApiSimpleResponse(apiType),
logger: logger,
}
}
func newApiSimpleResponse(apiType string) ApiSimpleResponse {
return ApiSimpleResponse{
Type: apiType,
Date: time.Now().Unix(),
Status: STATUS_UNKNOWN,
}
}
func (self *ApiSimpleResponseHandler) AddDetailsToHandler(v ApiResponseDetails) {
if v.Code == UNIQUSH_SUCCESS {
self.response.Status = STATUS_SUCCESS
} else {
self.response.Status = STATUS_FAILURE
}
self.response.Details = v
}
func (self *ApiSimpleResponseHandler) ToJSON() []byte {
json, err := json.Marshal(self.response)
if err != nil {
self.logger.Errorf("Failed to marshal json [%v] as string: %v", self.response, err)
return nil
}
return json
}