miniflux/integration/instapaper/instapaper.go

49 lines
1.2 KiB
Go
Raw Normal View History

2017-12-03 06:12:03 +01:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
2018-08-25 06:51:50 +02:00
package instapaper // import "miniflux.app/integration/instapaper"
2017-12-03 06:12:03 +01:00
import (
"fmt"
"net/url"
2018-08-25 06:51:50 +02:00
"miniflux.app/http/client"
2017-12-03 06:12:03 +01:00
)
// Client represents an Instapaper client.
type Client struct {
username string
password string
}
// NewClient returns a new Instapaper client.
func NewClient(username, password string) *Client {
return &Client{username: username, password: password}
}
2017-12-03 06:12:03 +01:00
// AddURL sends a link to Instapaper.
func (c *Client) AddURL(link, title string) error {
if c.username == "" || c.password == "" {
return fmt.Errorf("instapaper: missing credentials")
}
2017-12-03 06:12:03 +01:00
values := url.Values{}
values.Add("url", link)
values.Add("title", title)
apiURL := "https://www.instapaper.com/api/add?" + values.Encode()
2018-04-28 19:51:07 +02:00
clt := client.New(apiURL)
clt.WithCredentials(c.username, c.password)
response, err := clt.Get()
if err != nil {
return fmt.Errorf("instapaper: unable to send url: %v", err)
}
2017-12-03 06:12:03 +01:00
if response.HasServerFailure() {
2017-12-19 05:52:46 +01:00
return fmt.Errorf("instapaper: unable to send url, status=%d", response.StatusCode)
2017-12-03 06:12:03 +01:00
}
return nil
2017-12-03 06:12:03 +01:00
}