wallabag.go 4.2 KB

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