-
Notifications
You must be signed in to change notification settings - Fork 201
/
restapi_pushresponse.go
73 lines (64 loc) · 2.41 KB
/
restapi_pushresponse.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
package main
import (
"encoding/json"
"sync"
"time"
"github.com/uniqush/log"
)
// APIPushResponseHandler records information about push notification attempts to generate a JSON response.
type APIPushResponseHandler struct {
response APIPushResponse
logger log.Logger
mutex sync.Mutex
}
var _ APIResponseHandler = &APIPushResponseHandler{}
// APIPushResponse represents the push response data that will be returned to the uniqush client.
type APIPushResponse struct {
Type string `json:"type"`
Date int64 `json:"date"`
SuccessCount int `json:"successCount"`
FailureCount int `json:"failureCount"`
DroppedCount int `json:"droppedCount"`
SuccessDetails []APIResponseDetails `json:"successDetails"`
FailureDetails []APIResponseDetails `json:"failureDetails"`
DroppedDetails []APIResponseDetails `json:"droppedDetails"`
}
func newPushResponseHandler(logger log.Logger) *APIPushResponseHandler {
return &APIPushResponseHandler{
response: newAPIPushResponse(),
logger: logger,
}
}
func newAPIPushResponse() APIPushResponse {
return APIPushResponse{
Type: "Push",
Date: time.Now().Unix(),
SuccessDetails: make([]APIResponseDetails, 0),
FailureDetails: make([]APIResponseDetails, 0),
DroppedDetails: make([]APIResponseDetails, 0),
}
}
// AddDetailsToHandler will record information about one response (of one or more responses) to an individual push attempt to a psp.
func (handler *APIPushResponseHandler) AddDetailsToHandler(v APIResponseDetails) {
handler.mutex.Lock()
if v.Code == UNIQUSH_SUCCESS {
handler.response.SuccessDetails = append(handler.response.SuccessDetails, v)
handler.response.SuccessCount++
} else if v.Code == UNIQUSH_UPDATE_UNSUBSCRIBE || v.Code == UNIQUSH_REMOVE_INVALID_REG {
handler.response.DroppedDetails = append(handler.response.DroppedDetails, v)
handler.response.DroppedCount++
} else {
handler.response.FailureDetails = append(handler.response.FailureDetails, v)
handler.response.FailureCount++
}
handler.mutex.Unlock()
}
// ToJSON serializes this push response as JSON to send to the client of uniqush-push.
func (handler *APIPushResponseHandler) ToJSON() []byte {
json, err := json.Marshal(handler.response)
if err != nil {
handler.logger.Errorf("Failed to marshal json [%v] as string: %v", handler.response, err)
return nil
}
return json
}