pushclient.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package jpushclient
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "strings"
  8. )
  9. const (
  10. SUCCESS_FLAG = "msg_id"
  11. HOST_NAME_SSL = "https://api.jpush.cn/v3/push"
  12. HOST_SCHEDULE = "https://api.jpush.cn/v3/schedules"
  13. HOST_REPORT = "https://report.jpush.cn/v3/received"
  14. BASE64_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  15. )
  16. var base64Coder = base64.NewEncoding(BASE64_TABLE)
  17. type PushClient struct {
  18. MasterSecret string
  19. AppKey string
  20. AuthCode string
  21. BaseUrl string
  22. }
  23. func NewPushClient(secret, appKey string) *PushClient {
  24. //base64
  25. auth := "Basic " + base64Coder.EncodeToString([]byte(appKey+":"+secret))
  26. pusher := &PushClient{secret, appKey, auth, HOST_NAME_SSL}
  27. return pusher
  28. }
  29. func (this *PushClient) Send(data []byte) (string, error) {
  30. return this.SendPushBytes(data)
  31. }
  32. func (this *PushClient) CreateSchedule(data []byte) (string, error) {
  33. // this.BaseUrl = HOST_SCHEDULE
  34. return this.SendScheduleBytes(data, HOST_SCHEDULE)
  35. }
  36. func (this *PushClient) DeleteSchedule(id string) (string, error) {
  37. // this.BaseUrl = HOST_SCHEDULE
  38. return this.SendDeleteScheduleRequest(id, HOST_SCHEDULE)
  39. }
  40. func (this *PushClient) GetSchedule(id string) (string, error) {
  41. // GET https://api.jpush.cn/v3/schedules/{schedule_id}
  42. // this.BaseUrl = HOST_SCHEDULE
  43. return this.SendGetScheduleRequest(id, HOST_SCHEDULE)
  44. }
  45. func (this *PushClient) GetReport(msg_ids string) (string, error) {
  46. // this.BaseUrl = HOST_REPORT
  47. return this.SendGetReportRequest(msg_ids, HOST_REPORT)
  48. }
  49. func (this *PushClient) SendPushString(content string) (string, error) {
  50. ret, err := SendPostString(this.BaseUrl, content, this.AuthCode)
  51. if err != nil {
  52. return ret, err
  53. }
  54. if strings.Contains(ret, "msg_id") {
  55. return ret, nil
  56. } else {
  57. return "", errors.New(ret)
  58. }
  59. }
  60. func (this *PushClient) SendPushBytes(content []byte) (string, error) {
  61. //ret, err := SendPostBytes(this.BaseUrl, content, this.AuthCode)
  62. ret, err := SendPostBytes2(this.BaseUrl, content, this.AuthCode)
  63. if err != nil {
  64. return ret, err
  65. }
  66. if strings.Contains(ret, "msg_id") {
  67. return ret, nil
  68. } else {
  69. return "", errors.New(ret)
  70. }
  71. }
  72. func (this *PushClient) SendScheduleBytes(content []byte, url string) (string, error) {
  73. ret, err := SendPostBytes2(url, content, this.AuthCode)
  74. if err != nil {
  75. return ret, err
  76. }
  77. if strings.Contains(ret, "schedule_id") {
  78. return ret, nil
  79. } else {
  80. return "", errors.New(ret)
  81. }
  82. }
  83. func (this *PushClient) SendGetReportRequest(msg_ids string, url string) (string, error) {
  84. return Get(url).SetBasicAuth(this.AppKey, this.MasterSecret).Param("msg_ids", msg_ids).String()
  85. }
  86. func UnmarshalResponse(rsp string) (map[string]interface{}, error) {
  87. mapRs := map[string]interface{}{}
  88. if len(strings.TrimSpace(rsp)) == 0 {
  89. return mapRs, nil
  90. }
  91. err := json.Unmarshal([]byte(rsp), &mapRs)
  92. if err != nil {
  93. return nil, err
  94. }
  95. if _, ok := mapRs["error"]; ok {
  96. return nil, fmt.Errorf(rsp)
  97. }
  98. return mapRs, nil
  99. }
  100. func (this *PushClient) SendDeleteScheduleRequest(schedule_id string, url string) (string, error) {
  101. rsp, err := Delete(strings.Join([]string{url, schedule_id}, "/")).Header("Authorization", this.AuthCode).String()
  102. if err != nil {
  103. return "", err
  104. }
  105. _, err = UnmarshalResponse(rsp)
  106. if err != nil {
  107. return "", err
  108. }
  109. return rsp, nil
  110. }
  111. func (this *PushClient) SendGetScheduleRequest(schedule_id string, url string) (string, error) {
  112. rsp, err := Get(strings.Join([]string{url, schedule_id}, "/")).Header("Authorization", this.AuthCode).String()
  113. if err != nil {
  114. return "", err
  115. }
  116. _, err = UnmarshalResponse(rsp)
  117. if err != nil {
  118. return "", err
  119. }
  120. return rsp, nil
  121. }