shiori.go 4.1 KB

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