client.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package oppo
  2. import (
  3. "encoding/json"
  4. "io"
  5. "io/ioutil"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "go-common/library/log"
  12. "go-common/library/stat"
  13. "go-common/library/stat/prom"
  14. )
  15. // Client huawei push http client.
  16. type Client struct {
  17. Auth *Auth
  18. HTTPClient *http.Client
  19. Stats stat.Stat
  20. Activity string
  21. }
  22. // NewClient new huawei push HTTP client.
  23. func NewClient(a *Auth, activity string, timeout time.Duration) *Client {
  24. return &Client{
  25. Auth: a,
  26. HTTPClient: &http.Client{Timeout: timeout},
  27. Stats: prom.HTTPClient,
  28. Activity: activity,
  29. }
  30. }
  31. // Message saves push message content.
  32. func (c *Client) Message(m *Message) (response *Response, err error) {
  33. now := time.Now()
  34. if c.Stats != nil {
  35. defer func() {
  36. c.Stats.Timing(_apiMessage, int64(time.Since(now)/time.Millisecond))
  37. log.Info("oppo message stats timing: %v", int64(time.Since(now)/time.Millisecond))
  38. if err != nil {
  39. c.Stats.Incr(_apiMessage, "failed")
  40. }
  41. }()
  42. }
  43. params := url.Values{}
  44. params.Add("auth_token", c.Auth.Token)
  45. params.Add("title", m.Title)
  46. params.Add("content", m.Content)
  47. params.Add("click_action_type", strconv.Itoa(m.ActionType))
  48. params.Add("click_action_activity", c.Activity)
  49. params.Add("action_parameters", m.ActionParams)
  50. params.Add("off_line_ttl", strconv.Itoa(m.OfflineTTL))
  51. params.Add("call_back_url", m.CallbackURL)
  52. req, err := http.NewRequest(http.MethodPost, _apiMessage, strings.NewReader(params.Encode()))
  53. if err != nil {
  54. log.Error("http.NewRequest() error(%v)", err)
  55. return
  56. }
  57. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  58. req.Header.Set("Connection", "Keep-Alive")
  59. res, err := c.HTTPClient.Do(req)
  60. if err != nil {
  61. log.Error("HTTPClient.Do() error(%v)", err)
  62. return
  63. }
  64. defer res.Body.Close()
  65. response = &Response{}
  66. bs, err := ioutil.ReadAll(res.Body)
  67. if err != nil {
  68. log.Error("ioutil.ReadAll() error(%v)", err)
  69. return
  70. } else if len(bs) == 0 {
  71. return
  72. }
  73. if e := json.Unmarshal(bs, &response); e != nil {
  74. if e != io.EOF {
  75. log.Error("json decode body(%s) error(%v)", string(bs), e)
  76. }
  77. }
  78. return
  79. }
  80. // Push push notification.
  81. func (c *Client) Push(msgID string, tokens []string) (response *Response, err error) {
  82. now := time.Now()
  83. if c.Stats != nil {
  84. defer func() {
  85. c.Stats.Timing(_apiPushBroadcast, int64(time.Since(now)/time.Millisecond))
  86. log.Info("oppo push stats timing: %v", int64(time.Since(now)/time.Millisecond))
  87. if err != nil {
  88. c.Stats.Incr(_apiPushBroadcast, "failed")
  89. }
  90. }()
  91. }
  92. params := url.Values{}
  93. params.Add("auth_token", c.Auth.Token)
  94. params.Add("message_id", msgID)
  95. params.Add("target_type", _pushTypeToken)
  96. params.Add("registration_ids", strings.Join(tokens, ";"))
  97. req, err := http.NewRequest(http.MethodPost, _apiPushBroadcast, strings.NewReader(params.Encode()))
  98. if err != nil {
  99. log.Error("http.NewRequest() error(%v)", err)
  100. return
  101. }
  102. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  103. req.Header.Set("Connection", "Keep-Alive")
  104. res, err := c.HTTPClient.Do(req)
  105. if err != nil {
  106. log.Error("HTTPClient.Do() error(%v)", err)
  107. return
  108. }
  109. defer res.Body.Close()
  110. response = &Response{}
  111. bs, err := ioutil.ReadAll(res.Body)
  112. if err != nil {
  113. log.Error("ioutil.ReadAll() error(%v)", err)
  114. return
  115. } else if len(bs) == 0 {
  116. return
  117. }
  118. if e := json.Unmarshal(bs, &response); e != nil {
  119. if e != io.EOF {
  120. log.Error("json decode body(%s) error(%v)", string(bs), e)
  121. }
  122. }
  123. return
  124. }
  125. // PushOne push single notification.
  126. func (c *Client) PushOne(m *Message, token string) (response *Response, err error) {
  127. now := time.Now()
  128. if c.Stats != nil {
  129. defer func() {
  130. c.Stats.Timing(_apiPushUnicast, int64(time.Since(now)/time.Millisecond))
  131. log.Info("oppo pushOne stats timing: %v", int64(time.Since(now)/time.Millisecond))
  132. if err != nil {
  133. c.Stats.Incr(_apiPushUnicast, "failed")
  134. }
  135. }()
  136. }
  137. m.ActionActivity = c.Activity
  138. params := url.Values{}
  139. msg, _ := json.Marshal(&struct {
  140. TargetType string `json:"target_type"`
  141. Token string `json:"registration_id"`
  142. Notification *Message `json:"notification"`
  143. }{
  144. TargetType: _pushTypeToken,
  145. Token: token,
  146. Notification: m,
  147. })
  148. params.Add("auth_token", c.Auth.Token)
  149. params.Add("message", string(msg))
  150. req, err := http.NewRequest(http.MethodPost, _apiPushUnicast, strings.NewReader(params.Encode()))
  151. if err != nil {
  152. log.Error("http.NewRequest() error(%v)", err)
  153. return
  154. }
  155. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  156. req.Header.Set("Connection", "Keep-Alive")
  157. res, err := c.HTTPClient.Do(req)
  158. if err != nil {
  159. log.Error("HTTPClient.Do() error(%v)", err)
  160. return
  161. }
  162. defer res.Body.Close()
  163. response = &Response{}
  164. bs, err := ioutil.ReadAll(res.Body)
  165. if err != nil {
  166. log.Error("ioutil.ReadAll() error(%v)", err)
  167. return
  168. } else if len(bs) == 0 {
  169. return
  170. }
  171. if e := json.Unmarshal(bs, &response); e != nil {
  172. if e != io.EOF {
  173. log.Error("json decode body(%s) error(%v)", string(bs), e)
  174. }
  175. }
  176. return
  177. }