instapaper.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. "errors"
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "time"
  10. "miniflux.app/v2/internal/http/client"
  11. "miniflux.app/v2/internal/version"
  12. )
  13. const defaultClientTimeout = 10 * time.Second
  14. type Client struct {
  15. username string
  16. password string
  17. }
  18. func NewClient(username, password string) *Client {
  19. return &Client{username: username, password: password}
  20. }
  21. func (c *Client) AddURL(entryURL, entryTitle string) error {
  22. if c.username == "" || c.password == "" {
  23. return errors.New("instapaper: missing username or password")
  24. }
  25. values := url.Values{}
  26. values.Add("url", entryURL)
  27. values.Add("title", entryTitle)
  28. apiEndpoint := "https://www.instapaper.com/api/add?" + values.Encode()
  29. request, err := http.NewRequest(http.MethodGet, apiEndpoint, nil)
  30. if err != nil {
  31. return fmt.Errorf("instapaper: unable to create request: %v", err)
  32. }
  33. request.SetBasicAuth(c.username, c.password)
  34. request.Header.Set("User-Agent", "Miniflux/"+version.Version)
  35. httpClient := client.NewClientWithOptions(client.Options{Timeout: defaultClientTimeout})
  36. response, err := httpClient.Do(request)
  37. if err != nil {
  38. return fmt.Errorf("instapaper: unable to send request: %v", err)
  39. }
  40. defer response.Body.Close()
  41. if response.StatusCode != http.StatusCreated {
  42. return fmt.Errorf("instapaper: unable to add URL: url=%s status=%d", apiEndpoint, response.StatusCode)
  43. }
  44. return nil
  45. }