wallabag.go 4.7 KB

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