bullet.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package bullet
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "mime"
  7. "net/http"
  8. "path/filepath"
  9. "time"
  10. )
  11. const (
  12. shortTimeFormat = "2006-01-02 15:04"
  13. )
  14. //Bullet client for the Pushbullet API
  15. type Bullet struct {
  16. token string
  17. }
  18. //NewBullet creates new Bullet using provided token
  19. func NewBullet(token string) Bullet {
  20. b := Bullet{token: token}
  21. return b
  22. }
  23. func (b Bullet) newRequest(body io.Reader, URL string) (*http.Request, error) {
  24. request, err := http.NewRequest(http.MethodPost, URL, body)
  25. if err != nil {
  26. return nil, err
  27. }
  28. request.Header.Add("Access-Token", b.token)
  29. request.Header.Add("Content-Type", "application/json")
  30. return request, nil
  31. }
  32. func (b Bullet) newRequestPush(body io.Reader) (*http.Request, error) {
  33. return b.newRequest(body, "https://api.pushbullet.com/v2/pushes")
  34. }
  35. func (b Bullet) newRequestUpload(body io.Reader) (*http.Request, error) {
  36. return b.newRequest(body, "https://api.pushbullet.com/v2/upload-request")
  37. }
  38. func (b Bullet) send(push pushInterface) error {
  39. reader, errReader := push.getReader()
  40. if errReader != nil {
  41. return errReader
  42. }
  43. request, errRequest := b.newRequestPush(reader)
  44. if errRequest != nil {
  45. return errRequest
  46. }
  47. client := http.Client{}
  48. response, errResponse := client.Do(request)
  49. if errResponse != nil {
  50. return errResponse
  51. }
  52. defer response.Body.Close()
  53. if response.StatusCode != http.StatusOK {
  54. var errBullet bulletError
  55. decoder := json.NewDecoder(response.Body)
  56. errJSON := decoder.Decode(&errBullet)
  57. if errJSON != nil {
  58. return errJSON
  59. }
  60. return errBullet.getError()
  61. }
  62. return nil
  63. }
  64. //SendNote sends push note with given title and text
  65. func (b Bullet) SendNote(title, text string) error {
  66. note := newNotePush(title, text)
  67. err := b.send(note)
  68. return err
  69. }
  70. //SendLink sends push link with given title, text and link
  71. func (b Bullet) SendLink(title, text, link string) error {
  72. linkPush := newLinkPush(title, text, link)
  73. err := b.send(linkPush)
  74. return err
  75. }
  76. func (b Bullet) requestUpload(file fileUpload) (*fileUpload, error) {
  77. reader, errReader := file.getReader()
  78. if errReader != nil {
  79. return nil, errReader
  80. }
  81. request, errRequest := b.newRequestUpload(reader)
  82. if errRequest != nil {
  83. return nil, errRequest
  84. }
  85. client := http.Client{}
  86. response, errResponse := client.Do(request)
  87. if errResponse != nil {
  88. return nil, errResponse
  89. }
  90. defer response.Body.Close()
  91. if response.StatusCode != http.StatusOK {
  92. var errBullet bulletError
  93. decoder := json.NewDecoder(response.Body)
  94. errJSON := decoder.Decode(&errBullet)
  95. if errJSON != nil {
  96. return nil, errJSON
  97. }
  98. return nil, errBullet.getError()
  99. }
  100. var result fileUpload
  101. decoder := json.NewDecoder(response.Body)
  102. errJSON := decoder.Decode(&result)
  103. if errJSON != nil {
  104. return nil, errJSON
  105. }
  106. return &result, nil
  107. }
  108. func (b Bullet) uploadFile(path string) (*fileUpload, error) {
  109. extension := filepath.Ext(path)
  110. mimeType := mime.TypeByExtension(extension)
  111. if mimeType == "" {
  112. mimeType = "application/octet-stream"
  113. }
  114. filename := filepath.Base(path)
  115. fileUpload := newFileUpload(filename, mimeType)
  116. uploadResponse, errUpload := b.requestUpload(fileUpload)
  117. if errUpload != nil {
  118. return nil, errUpload
  119. }
  120. request, errRequest := getFileRequest(path, *uploadResponse)
  121. if errRequest != nil {
  122. return nil, errRequest
  123. }
  124. client := http.Client{}
  125. response, errResponse := client.Do(request)
  126. if errResponse != nil {
  127. return nil, errResponse
  128. }
  129. defer response.Body.Close()
  130. if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {
  131. return nil, fmt.Errorf("Error uploading file: %s", response.Status)
  132. }
  133. return uploadResponse, nil
  134. }
  135. //SendFile sends file push with given title, text and file
  136. func (b Bullet) SendFile(title, text, file string) error {
  137. uploadResult, errUpload := b.uploadFile(file)
  138. if errUpload != nil {
  139. return errUpload
  140. }
  141. if title != "" {
  142. uploadResult.Title = title
  143. } else {
  144. uploadResult.Title = time.Now().Format(shortTimeFormat)
  145. }
  146. uploadResult.Body = text
  147. uploadResult.Type = "file"
  148. err := b.send(uploadResult)
  149. return err
  150. }