1
0
Fork 0
miniflux/integration/instapaper/instapaper.go

49 lines
1.2 KiB
Go
Raw Normal View History

2017-12-03 00:12:03 -05: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 00:51:50 -04:00
package instapaper // import "miniflux.app/integration/instapaper"
2017-12-03 00:12:03 -05:00
import (
"fmt"
"net/url"
2018-08-25 00:51:50 -04:00
"miniflux.app/http/client"
2017-12-03 00:12:03 -05: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 00:12:03 -05: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 00:12:03 -05:00
values := url.Values{}
values.Add("url", link)
values.Add("title", title)
apiURL := "https://www.instapaper.com/api/add?" + values.Encode()
2018-04-28 13:51:07 -04: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 00:12:03 -05:00
if response.HasServerFailure() {
2017-12-18 23:52:46 -05:00
return fmt.Errorf("instapaper: unable to send url, status=%d", response.StatusCode)
2017-12-03 00:12:03 -05:00
}
return nil
2017-12-03 00:12:03 -05:00
}