wallabag.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package wallabag // import "miniflux.app/v2/internal/integration/wallabag"
  4. import (
  5. "bytes"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. "time"
  13. "miniflux.app/v2/internal/urllib"
  14. "miniflux.app/v2/internal/version"
  15. )
  16. const defaultClientTimeout = 10 * time.Second
  17. type Client struct {
  18. baseURL string
  19. clientID string
  20. clientSecret string
  21. username string
  22. password string
  23. tags string
  24. onlyURL bool
  25. }
  26. func NewClient(baseURL, clientID, clientSecret, username, password, tags string, onlyURL bool) *Client {
  27. return &Client{baseURL, clientID, clientSecret, username, password, tags, onlyURL}
  28. }
  29. func (c *Client) CreateEntry(entryURL, entryTitle, entryContent string) error {
  30. if c.baseURL == "" || c.clientID == "" || c.clientSecret == "" || c.username == "" || c.password == "" {
  31. return errors.New("wallabag: missing base URL, client ID, client secret, username or password")
  32. }
  33. accessToken, err := c.getAccessToken()
  34. if err != nil {
  35. return err
  36. }
  37. return c.createEntry(accessToken, entryURL, entryTitle, entryContent, c.tags)
  38. }
  39. func (c *Client) createEntry(accessToken, entryURL, entryTitle, entryContent, tags string) error {
  40. apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/api/entries.json")
  41. if err != nil {
  42. return fmt.Errorf("wallbag: unable to generate entries endpoint: %v", err)
  43. }
  44. if c.onlyURL {
  45. entryContent = ""
  46. }
  47. requestBody, err := json.Marshal(&createEntryRequest{
  48. URL: entryURL,
  49. Title: entryTitle,
  50. Content: entryContent,
  51. Tags: tags,
  52. })
  53. if err != nil {
  54. return fmt.Errorf("wallbag: unable to encode request body: %v", err)
  55. }
  56. request, err := http.NewRequest(http.MethodPost, apiEndpoint, bytes.NewReader(requestBody))
  57. if err != nil {
  58. return fmt.Errorf("wallbag: unable to create request: %v", err)
  59. }
  60. request.Header.Set("Content-Type", "application/json")
  61. request.Header.Set("Accept", "application/json")
  62. request.Header.Set("User-Agent", "Miniflux/"+version.Version)
  63. request.Header.Set("Authorization", "Bearer "+accessToken)
  64. httpClient := &http.Client{Timeout: defaultClientTimeout}
  65. response, err := httpClient.Do(request)
  66. if err != nil {
  67. return fmt.Errorf("wallabag: unable to send request: %v", err)
  68. }
  69. defer response.Body.Close()
  70. if response.StatusCode >= 400 {
  71. return fmt.Errorf("wallabag: unable to get save entry: url=%s status=%d", apiEndpoint, response.StatusCode)
  72. }
  73. return nil
  74. }
  75. func (c *Client) getAccessToken() (string, error) {
  76. values := url.Values{}
  77. values.Add("grant_type", "password")
  78. values.Add("client_id", c.clientID)
  79. values.Add("client_secret", c.clientSecret)
  80. values.Add("username", c.username)
  81. values.Add("password", c.password)
  82. apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/oauth/v2/token")
  83. if err != nil {
  84. return "", fmt.Errorf("wallbag: unable to generate token endpoint: %v", err)
  85. }
  86. request, err := http.NewRequest(http.MethodPost, apiEndpoint, strings.NewReader(values.Encode()))
  87. if err != nil {
  88. return "", fmt.Errorf("wallbag: unable to create request: %v", err)
  89. }
  90. request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  91. request.Header.Set("Accept", "application/json")
  92. request.Header.Set("User-Agent", "Miniflux/"+version.Version)
  93. httpClient := &http.Client{Timeout: defaultClientTimeout}
  94. response, err := httpClient.Do(request)
  95. if err != nil {
  96. return "", fmt.Errorf("wallabag: unable to send request: %v", err)
  97. }
  98. defer response.Body.Close()
  99. if response.StatusCode >= 400 {
  100. return "", fmt.Errorf("wallabag: unable to get access token: url=%s status=%d", apiEndpoint, response.StatusCode)
  101. }
  102. var responseBody tokenResponse
  103. if err := json.NewDecoder(response.Body).Decode(&responseBody); err != nil {
  104. return "", fmt.Errorf("wallabag: unable to decode token response: %v", err)
  105. }
  106. return responseBody.AccessToken, nil
  107. }
  108. type tokenResponse struct {
  109. AccessToken string `json:"access_token"`
  110. Expires int `json:"expires_in"`
  111. RefreshToken string `json:"refresh_token"`
  112. Scope string `json:"scope"`
  113. TokenType string `json:"token_type"`
  114. }
  115. type createEntryRequest struct {
  116. URL string `json:"url"`
  117. Title string `json:"title"`
  118. Content string `json:"content,omitempty"`
  119. Tags string `json:"tags,omitempty"`
  120. }