shiori.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. sessionID, 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. CreateArchive: true,
  38. })
  39. if err != nil {
  40. return fmt.Errorf("shiori: unable to encode request body: %v", err)
  41. }
  42. request, err := http.NewRequest(http.MethodPost, apiEndpoint, bytes.NewReader(requestBody))
  43. if err != nil {
  44. return fmt.Errorf("shiori: unable to create request: %v", err)
  45. }
  46. request.Header.Set("Content-Type", "application/json")
  47. request.Header.Set("User-Agent", "Miniflux/"+version.Version)
  48. request.Header.Set("X-Session-Id", sessionID)
  49. httpClient := &http.Client{Timeout: defaultClientTimeout}
  50. response, err := httpClient.Do(request)
  51. if err != nil {
  52. return fmt.Errorf("shiori: unable to send request: %v", err)
  53. }
  54. defer response.Body.Close()
  55. if response.StatusCode != http.StatusOK {
  56. return fmt.Errorf("shiori: unable to create bookmark: url=%s status=%d", apiEndpoint, response.StatusCode)
  57. }
  58. return nil
  59. }
  60. func (c *Client) authenticate() (sessionID string, err error) {
  61. apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/api/login")
  62. if err != nil {
  63. return "", fmt.Errorf("shiori: invalid API endpoint: %v", err)
  64. }
  65. requestBody, err := json.Marshal(&authRequest{Username: c.username, Password: c.password})
  66. if err != nil {
  67. return "", fmt.Errorf("shiori: unable to encode request body: %v", err)
  68. }
  69. request, err := http.NewRequest(http.MethodPost, apiEndpoint, bytes.NewReader(requestBody))
  70. if err != nil {
  71. return "", fmt.Errorf("shiori: unable to create request: %v", err)
  72. }
  73. request.Header.Set("Content-Type", "application/json")
  74. request.Header.Set("Accept", "application/json")
  75. request.Header.Set("User-Agent", "Miniflux/"+version.Version)
  76. httpClient := &http.Client{Timeout: defaultClientTimeout}
  77. response, err := httpClient.Do(request)
  78. if err != nil {
  79. return "", fmt.Errorf("shiori: unable to send request: %v", err)
  80. }
  81. defer response.Body.Close()
  82. if response.StatusCode != http.StatusOK {
  83. return "", fmt.Errorf("shiori: unable to authenticate: url=%s status=%d", apiEndpoint, response.StatusCode)
  84. }
  85. var authResponse authResponse
  86. if err := json.NewDecoder(response.Body).Decode(&authResponse); err != nil {
  87. return "", fmt.Errorf("shiori: unable to decode response: %v", err)
  88. }
  89. return authResponse.SessionID, nil
  90. }
  91. type authRequest struct {
  92. Username string `json:"username"`
  93. Password string `json:"password"`
  94. }
  95. type authResponse struct {
  96. SessionID string `json:"session"`
  97. }
  98. type addBookmarkRequest struct {
  99. URL string `json:"url"`
  100. Title string `json:"title"`
  101. CreateArchive bool `json:"createArchive"`
  102. }