instapaper.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package instapaper // import "miniflux.app/v2/internal/integration/instapaper"
  4. import (
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "time"
  9. "miniflux.app/v2/internal/version"
  10. )
  11. const defaultClientTimeout = 10 * time.Second
  12. type Client struct {
  13. username string
  14. password string
  15. }
  16. func NewClient(username, password string) *Client {
  17. return &Client{username: username, password: password}
  18. }
  19. func (c *Client) AddURL(entryURL, entryTitle string) error {
  20. if c.username == "" || c.password == "" {
  21. return fmt.Errorf("instapaper: missing username or password")
  22. }
  23. values := url.Values{}
  24. values.Add("url", entryURL)
  25. values.Add("title", entryTitle)
  26. apiEndpoint := "https://www.instapaper.com/api/add?" + values.Encode()
  27. request, err := http.NewRequest(http.MethodGet, apiEndpoint, nil)
  28. if err != nil {
  29. return fmt.Errorf("instapaper: unable to create request: %v", err)
  30. }
  31. request.SetBasicAuth(c.username, c.password)
  32. request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  33. request.Header.Set("User-Agent", "Miniflux/"+version.Version)
  34. httpClient := &http.Client{Timeout: defaultClientTimeout}
  35. response, err := httpClient.Do(request)
  36. if err != nil {
  37. return fmt.Errorf("instapaper: unable to send request: %v", err)
  38. }
  39. defer response.Body.Close()
  40. if response.StatusCode != http.StatusCreated {
  41. return fmt.Errorf("instapaper: unable to add URL: url=%s status=%d", apiEndpoint, response.StatusCode)
  42. }
  43. return nil
  44. }