wallabag.go 4.3 KB

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