shiori.go 4.1 KB

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