bullet.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 doRequest(request *http.Request) (*http.Response, error) {
  39. client := http.Client{}
  40. response, errResponse := client.Do(request)
  41. if errResponse != nil {
  42. return nil, errResponse
  43. }
  44. if response.StatusCode != http.StatusOK {
  45. var errBullet bulletError
  46. decoder := json.NewDecoder(response.Body)
  47. errJSON := decoder.Decode(&errBullet)
  48. if errJSON != nil {
  49. return nil, errJSON
  50. }
  51. return nil, errBullet.getError()
  52. }
  53. return response, nil
  54. }
  55. func (b Bullet) send(push pushInterface) error {
  56. reader, errReader := push.getReader()
  57. if errReader != nil {
  58. return errReader
  59. }
  60. request, errRequest := b.newRequestPush(reader)
  61. if errRequest != nil {
  62. return errRequest
  63. }
  64. response, errResponse := doRequest(request)
  65. if errResponse != nil {
  66. return errResponse
  67. }
  68. defer response.Body.Close()
  69. return nil
  70. }
  71. //SendNote sends push note with given title and text
  72. func (b Bullet) SendNote(title, text string) error {
  73. note := newNotePush(title, text)
  74. err := b.send(note)
  75. return err
  76. }
  77. //SendLink sends push link with given title, text and link
  78. func (b Bullet) SendLink(title, text, link string) error {
  79. linkPush := newLinkPush(title, text, link)
  80. err := b.send(linkPush)
  81. return err
  82. }
  83. func (b Bullet) requestUpload(file fileUpload) (*fileUpload, error) {
  84. reader, errReader := file.getReader()
  85. if errReader != nil {
  86. return nil, errReader
  87. }
  88. request, errRequest := b.newRequestUpload(reader)
  89. if errRequest != nil {
  90. return nil, errRequest
  91. }
  92. response, errResponse := doRequest(request)
  93. if errResponse != nil {
  94. return nil, errResponse
  95. }
  96. defer response.Body.Close()
  97. var result fileUpload
  98. decoder := json.NewDecoder(response.Body)
  99. errJSON := decoder.Decode(&result)
  100. if errJSON != nil {
  101. return nil, errJSON
  102. }
  103. return &result, nil
  104. }
  105. func (b Bullet) uploadFile(path string) (*fileUpload, error) {
  106. extension := filepath.Ext(path)
  107. mimeType := mime.TypeByExtension(extension)
  108. if mimeType == "" {
  109. mimeType = "application/octet-stream"
  110. }
  111. filename := filepath.Base(path)
  112. fileUpload := newFileUpload(filename, mimeType)
  113. uploadResponse, errUpload := b.requestUpload(fileUpload)
  114. if errUpload != nil {
  115. return nil, errUpload
  116. }
  117. request, errRequest := getFileRequest(path, *uploadResponse)
  118. if errRequest != nil {
  119. return nil, errRequest
  120. }
  121. client := http.Client{}
  122. response, errResponse := client.Do(request)
  123. if errResponse != nil {
  124. return nil, errResponse
  125. }
  126. defer response.Body.Close()
  127. if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {
  128. return nil, fmt.Errorf("Error uploading file: %s", response.Status)
  129. }
  130. return uploadResponse, nil
  131. }
  132. //SendFile sends file push with given title, text and file
  133. func (b Bullet) SendFile(title, text, file string) error {
  134. uploadResult, errUpload := b.uploadFile(file)
  135. if errUpload != nil {
  136. return errUpload
  137. }
  138. if title != "" {
  139. uploadResult.Title = title
  140. } else {
  141. uploadResult.Title = time.Now().Format(shortTimeFormat)
  142. }
  143. uploadResult.Body = text
  144. uploadResult.Type = "file"
  145. err := b.send(uploadResult)
  146. return err
  147. }