shiori.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package shiori // import "miniflux.app/v2/internal/integration/shiori"
  4. import (
  5. "bytes"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "net/http"
  10. "time"
  11. "miniflux.app/v2/internal/config"
  12. "miniflux.app/v2/internal/http/client"
  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. username string
  20. password string
  21. }
  22. func NewClient(baseURL, username, password string) *Client {
  23. return &Client{baseURL: baseURL, username: username, password: password}
  24. }
  25. func (c *Client) CreateBookmark(entryURL, entryTitle string) error {
  26. if c.baseURL == "" || c.username == "" || c.password == "" {
  27. return errors.New("shiori: missing base URL, username or password")
  28. }
  29. token, err := c.authenticate()
  30. if err != nil {
  31. return fmt.Errorf("shiori: unable to authenticate: %v", err)
  32. }
  33. apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/api/bookmarks")
  34. if err != nil {
  35. return fmt.Errorf("shiori: invalid API endpoint: %v", err)
  36. }
  37. requestBody, err := json.Marshal(&addBookmarkRequest{
  38. URL: entryURL,
  39. Title: entryTitle,
  40. Excerpt: "",
  41. CreateArchive: true,
  42. CreateEbook: false,
  43. Public: 0,
  44. Tags: make([]string, 0),
  45. })
  46. if err != nil {
  47. return fmt.Errorf("shiori: unable to encode request body: %v", err)
  48. }
  49. request, err := http.NewRequest(http.MethodPost, apiEndpoint, bytes.NewReader(requestBody))
  50. if err != nil {
  51. return fmt.Errorf("shiori: unable to create request: %v", err)
  52. }
  53. request.Header.Set("Content-Type", "application/json")
  54. request.Header.Set("User-Agent", "Miniflux/"+version.Version)
  55. request.Header.Set("Authorization", "Bearer "+token)
  56. httpClient := client.NewClientWithOptions(client.Options{Timeout: defaultClientTimeout, BlockPrivateNetworks: !config.Opts.IntegrationAllowPrivateNetworks()})
  57. response, err := httpClient.Do(request)
  58. if err != nil {
  59. return fmt.Errorf("shiori: unable to send request: %v", err)
  60. }
  61. defer response.Body.Close()
  62. if response.StatusCode != http.StatusOK {
  63. return fmt.Errorf("shiori: unable to create bookmark: url=%s status=%d", apiEndpoint, response.StatusCode)
  64. }
  65. return nil
  66. }
  67. func (c *Client) authenticate() (string, error) {
  68. apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/api/v1/auth/login")
  69. if err != nil {
  70. return "", fmt.Errorf("shiori: invalid API endpoint: %v", err)
  71. }
  72. requestBody, err := json.Marshal(&authRequest{Username: c.username, Password: c.password, RememberMe: false})
  73. if err != nil {
  74. return "", fmt.Errorf("shiori: unable to encode request body: %v", err)
  75. }
  76. request, err := http.NewRequest(http.MethodPost, apiEndpoint, bytes.NewReader(requestBody))
  77. if err != nil {
  78. return "", fmt.Errorf("shiori: unable to create request: %v", err)
  79. }
  80. request.Header.Set("Content-Type", "application/json")
  81. request.Header.Set("Accept", "application/json")
  82. request.Header.Set("User-Agent", "Miniflux/"+version.Version)
  83. httpClient := client.NewClientWithOptions(client.Options{Timeout: defaultClientTimeout, BlockPrivateNetworks: !config.Opts.IntegrationAllowPrivateNetworks()})
  84. response, err := httpClient.Do(request)
  85. if err != nil {
  86. return "", fmt.Errorf("shiori: unable to send request: %v", err)
  87. }
  88. defer response.Body.Close()
  89. if response.StatusCode != http.StatusOK {
  90. return "", fmt.Errorf("shiori: unable to authenticate: url=%s status=%d", apiEndpoint, response.StatusCode)
  91. }
  92. var authResponse authResponse
  93. if err := json.NewDecoder(response.Body).Decode(&authResponse); err != nil {
  94. return "", fmt.Errorf("shiori: unable to decode response: %v", err)
  95. }
  96. return authResponse.Message.Token, nil
  97. }
  98. type authRequest struct {
  99. Username string `json:"username"`
  100. Password string `json:"password"`
  101. RememberMe bool `json:"remember_me"`
  102. }
  103. type authResponse struct {
  104. OK bool `json:"ok"`
  105. Message authResponseMessage `json:"message"`
  106. }
  107. type authResponseMessage struct {
  108. SessionID string `json:"session"`
  109. Token string `json:"token"`
  110. }
  111. type addBookmarkRequest struct {
  112. URL string `json:"url"`
  113. Title string `json:"title"`
  114. CreateArchive bool `json:"create_archive"`
  115. CreateEbook bool `json:"create_ebook"`
  116. Public int `json:"public"`
  117. Excerpt string `json:"excerpt"`
  118. Tags []string `json:"tags"`
  119. }