go-mastodon/mastodon.go

378 lines
8.8 KiB
Go
Raw Permalink Normal View History

2017-05-13 21:59:56 +02:00
// Package mastodon provides functions and structs for accessing the mastodon API.
2017-04-13 09:47:00 +02:00
package mastodon
import (
"context"
2017-04-13 09:47:00 +02:00
"encoding/json"
2017-05-04 14:56:54 +02:00
"errors"
"fmt"
2017-04-17 06:54:36 +02:00
"io"
2017-04-13 09:47:00 +02:00
"net/http"
"net/url"
2017-04-13 19:16:52 +02:00
"path"
2017-04-13 09:47:00 +02:00
"strings"
"time"
2017-05-04 14:56:54 +02:00
"github.com/tomnomnom/linkheader"
2017-04-13 09:47:00 +02:00
)
2017-04-14 05:21:27 +02:00
// Config is a setting for access mastodon APIs.
2017-04-13 09:47:00 +02:00
type Config struct {
Server string
ClientID string
ClientSecret string
AccessToken string
}
2017-04-14 05:21:27 +02:00
// Client is a API client for mastodon.
type Client struct {
2017-04-13 09:47:00 +02:00
http.Client
2019-08-20 13:12:09 +02:00
Config *Config
2019-06-21 18:14:52 +02:00
UserAgent string
2017-04-18 10:08:48 +02:00
}
2017-05-06 16:03:19 +02:00
func (c *Client) doAPI(ctx context.Context, method string, uri string, params interface{}, res interface{}, pg *Pagination) error {
2019-08-20 13:12:09 +02:00
u, err := url.Parse(c.Config.Server)
2017-04-14 05:12:09 +02:00
if err != nil {
2017-05-06 16:03:19 +02:00
return err
2017-04-14 05:12:09 +02:00
}
2017-04-14 21:36:27 +02:00
u.Path = path.Join(u.Path, uri)
2017-04-14 05:12:09 +02:00
2017-04-17 06:54:36 +02:00
var req *http.Request
ct := "application/x-www-form-urlencoded"
if values, ok := params.(url.Values); ok {
2017-04-17 18:59:52 +02:00
var body io.Reader
if method == http.MethodGet {
2017-05-04 16:35:00 +02:00
if pg != nil {
values = pg.setValues(values)
}
u.RawQuery = values.Encode()
2017-04-17 18:59:52 +02:00
} else {
body = strings.NewReader(values.Encode())
}
req, err = http.NewRequest(method, u.String(), body)
2017-04-17 06:54:36 +02:00
if err != nil {
2017-05-06 16:03:19 +02:00
return err
2017-04-17 06:54:36 +02:00
}
} else if media, ok := params.(*Media); ok {
r, contentType, err := media.bodyAndContentType()
2017-04-17 06:54:36 +02:00
if err != nil {
2017-05-06 16:03:19 +02:00
return err
2017-04-17 06:54:36 +02:00
}
req, err = http.NewRequest(method, u.String(), r)
2017-04-17 06:54:36 +02:00
if err != nil {
2017-05-06 16:03:19 +02:00
return err
2017-04-17 06:54:36 +02:00
}
ct = contentType
2017-04-17 06:54:36 +02:00
} else {
2017-05-04 16:35:00 +02:00
if method == http.MethodGet && pg != nil {
u.RawQuery = pg.toValues().Encode()
}
2017-04-17 06:54:36 +02:00
req, err = http.NewRequest(method, u.String(), nil)
2017-04-20 14:29:10 +02:00
if err != nil {
2017-05-06 16:03:19 +02:00
return err
2017-04-20 14:29:10 +02:00
}
2017-04-14 05:12:09 +02:00
}
req = req.WithContext(ctx)
2019-08-20 13:12:09 +02:00
req.Header.Set("Authorization", "Bearer "+c.Config.AccessToken)
2017-04-15 17:47:23 +02:00
if params != nil {
2017-04-17 06:54:36 +02:00
req.Header.Set("Content-Type", ct)
2017-04-15 17:47:23 +02:00
}
2019-06-21 18:14:52 +02:00
if c.UserAgent != "" {
req.Header.Set("User-Agent", c.UserAgent)
}
var resp *http.Response
backoff := time.Second
for {
resp, err = c.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// handle status code 429, which indicates the server is throttling
// our requests. Do an exponential backoff and retry the request.
if resp.StatusCode == 429 {
if backoff > time.Hour {
break
}
select {
case <-time.After(backoff):
case <-ctx.Done():
return ctx.Err()
}
backoff = time.Duration(1.5 * float64(backoff))
continue
}
break
2017-04-17 05:25:20 +02:00
}
2017-04-17 05:25:20 +02:00
if resp.StatusCode != http.StatusOK {
2017-05-06 16:03:19 +02:00
return parseAPIError("bad request", resp)
2017-04-17 05:25:20 +02:00
} else if res == nil {
2017-05-06 16:03:19 +02:00
return nil
2017-05-06 16:34:42 +02:00
} else if pg != nil {
if lh := resp.Header.Get("Link"); lh != "" {
pg2, err := newPagination(lh)
if err != nil {
return err
}
*pg = *pg2
}
2017-04-17 05:25:20 +02:00
}
2017-05-06 16:03:19 +02:00
return json.NewDecoder(resp.Body).Decode(&res)
2017-04-14 05:12:09 +02:00
}
2022-11-13 22:14:40 +01:00
// NewClient returns a new mastodon API client.
2017-04-14 05:21:27 +02:00
func NewClient(config *Config) *Client {
return &Client{
2017-05-04 14:56:54 +02:00
Client: *http.DefaultClient,
2019-08-20 13:12:09 +02:00
Config: config,
2017-04-13 09:47:00 +02:00
}
}
2022-11-13 22:14:40 +01:00
// Authenticate gets access-token to the API.
func (c *Client) Authenticate(ctx context.Context, username, password string) error {
params := url.Values{
2019-08-20 13:12:09 +02:00
"client_id": {c.Config.ClientID},
"client_secret": {c.Config.ClientSecret},
"grant_type": {"password"},
"username": {username},
"password": {password},
"scope": {"read write follow"},
}
return c.authenticate(ctx, params)
}
2022-08-27 01:33:24 +02:00
// AuthenticateApp logs in using client credentials.
func (c *Client) AuthenticateApp(ctx context.Context) error {
params := url.Values{
"client_id": {c.Config.ClientID},
"client_secret": {c.Config.ClientSecret},
"grant_type": {"client_credentials"},
"redirect_uri": {"urn:ietf:wg:oauth:2.0:oob"},
}
return c.authenticate(ctx, params)
}
// AuthenticateToken logs in using a grant token returned by Application.AuthURI.
//
// redirectURI should be the same as Application.RedirectURI.
func (c *Client) AuthenticateToken(ctx context.Context, authCode, redirectURI string) error {
params := url.Values{
2019-08-20 13:12:09 +02:00
"client_id": {c.Config.ClientID},
"client_secret": {c.Config.ClientSecret},
"grant_type": {"authorization_code"},
"code": {authCode},
"redirect_uri": {redirectURI},
}
return c.authenticate(ctx, params)
}
2017-04-13 19:16:52 +02:00
func (c *Client) authenticate(ctx context.Context, params url.Values) error {
2019-08-20 13:12:09 +02:00
u, err := url.Parse(c.Config.Server)
2017-04-13 19:16:52 +02:00
if err != nil {
return err
}
2017-04-14 21:36:27 +02:00
u.Path = path.Join(u.Path, "/oauth/token")
2017-04-13 19:16:52 +02:00
2017-04-14 21:36:27 +02:00
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(params.Encode()))
2017-04-13 09:47:00 +02:00
if err != nil {
return err
}
req = req.WithContext(ctx)
2017-04-14 10:10:13 +02:00
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
2019-06-21 18:14:52 +02:00
if c.UserAgent != "" {
req.Header.Set("User-Agent", c.UserAgent)
}
2017-04-13 09:47:00 +02:00
resp, err := c.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
2017-04-19 07:32:53 +02:00
return parseAPIError("bad authorization", resp)
2017-04-14 10:10:13 +02:00
}
var res struct {
2017-04-13 09:47:00 +02:00
AccessToken string `json:"access_token"`
}
2017-04-13 19:16:52 +02:00
err = json.NewDecoder(resp.Body).Decode(&res)
2017-04-13 09:47:00 +02:00
if err != nil {
return err
}
2019-08-20 13:12:09 +02:00
c.Config.AccessToken = res.AccessToken
2017-04-13 09:47:00 +02:00
return nil
}
// Convenience constants for Toot.Visibility
const (
VisibilityPublic = "public"
VisibilityUnlisted = "unlisted"
VisibilityFollowersOnly = "private"
VisibilityDirectMessage = "direct"
)
2022-11-13 22:14:40 +01:00
// Toot is a struct to post status.
2017-04-13 19:16:52 +02:00
type Toot struct {
2019-06-14 08:39:23 +02:00
Status string `json:"status"`
InReplyToID ID `json:"in_reply_to_id"`
MediaIDs []ID `json:"media_ids"`
Sensitive bool `json:"sensitive"`
SpoilerText string `json:"spoiler_text"`
Visibility string `json:"visibility"`
2022-11-16 20:20:18 +01:00
Language string `json:"language"`
2019-06-14 08:39:23 +02:00
ScheduledAt *time.Time `json:"scheduled_at,omitempty"`
2021-11-05 09:21:53 +01:00
Poll *TootPoll `json:"poll"`
}
// TootPoll holds information for creating a poll in Toot.
type TootPoll struct {
Options []string `json:"options"`
ExpiresInSeconds int64 `json:"expires_in"`
Multiple bool `json:"multiple"`
HideTotals bool `json:"hide_totals"`
2017-04-13 19:16:52 +02:00
}
2017-04-14 17:10:04 +02:00
// Mention hold information for mention.
type Mention struct {
URL string `json:"url"`
Username string `json:"username"`
Acct string `json:"acct"`
ID ID `json:"id"`
2017-04-14 17:10:04 +02:00
}
// Tag hold information for tag.
type Tag struct {
Name string `json:"name"`
URL string `json:"url"`
History []History `json:"history"`
}
// History hold information for history.
type History struct {
Day string `json:"day"`
2020-03-01 12:25:12 +01:00
Uses string `json:"uses"`
Accounts string `json:"accounts"`
2017-04-14 17:10:04 +02:00
}
// Attachment hold information for attachment.
type Attachment struct {
2019-01-29 10:24:07 +01:00
ID ID `json:"id"`
Type string `json:"type"`
URL string `json:"url"`
RemoteURL string `json:"remote_url"`
PreviewURL string `json:"preview_url"`
TextURL string `json:"text_url"`
Description string `json:"description"`
Meta AttachmentMeta `json:"meta"`
}
// AttachmentMeta holds information for attachment metadata.
type AttachmentMeta struct {
2019-01-29 11:24:00 +01:00
Original AttachmentSize `json:"original"`
Small AttachmentSize `json:"small"`
2019-01-29 10:24:07 +01:00
}
2019-01-29 11:24:00 +01:00
// AttachmentSize holds information for attatchment size.
type AttachmentSize struct {
2019-01-29 10:24:07 +01:00
Width int64 `json:"width"`
Height int64 `json:"height"`
Size string `json:"size"`
Aspect float64 `json:"aspect"`
2017-04-14 17:10:04 +02:00
}
2017-04-15 16:21:37 +02:00
2017-10-25 14:19:38 +02:00
// Emoji hold information for CustomEmoji.
type Emoji struct {
ShortCode string `json:"shortcode"`
StaticURL string `json:"static_url"`
URL string `json:"url"`
VisibleInPicker bool `json:"visible_in_picker"`
2017-10-25 14:19:38 +02:00
}
2017-04-16 16:38:53 +02:00
// Results hold information for search result.
2017-04-15 16:21:37 +02:00
type Results struct {
Accounts []*Account `json:"accounts"`
Statuses []*Status `json:"statuses"`
Hashtags []*Tag `json:"hashtags"`
2017-04-15 16:21:37 +02:00
}
2017-05-04 14:56:54 +02:00
// Pagination is a struct for specifying the get range.
type Pagination struct {
MaxID ID
SinceID ID
2019-02-14 14:23:25 +01:00
MinID ID
Limit int64
2017-05-04 14:56:54 +02:00
}
func newPagination(rawlink string) (*Pagination, error) {
if rawlink == "" {
return nil, errors.New("empty link header")
}
p := &Pagination{}
for _, link := range linkheader.Parse(rawlink) {
switch link.Rel {
case "next":
maxID, err := getPaginationID(link.URL, "max_id")
if err != nil {
return nil, err
}
p.MaxID = maxID
2017-05-04 14:56:54 +02:00
case "prev":
sinceID, err := getPaginationID(link.URL, "since_id")
if err != nil {
return nil, err
}
p.SinceID = sinceID
2019-02-14 14:23:25 +01:00
minID, err := getPaginationID(link.URL, "min_id")
if err != nil {
return nil, err
}
p.MinID = minID
2017-05-04 14:56:54 +02:00
}
}
return p, nil
}
func getPaginationID(rawurl, key string) (ID, error) {
2017-05-04 14:56:54 +02:00
u, err := url.Parse(rawurl)
if err != nil {
return "", err
2017-05-04 14:56:54 +02:00
}
return ID(u.Query().Get(key)), nil
2017-05-04 14:56:54 +02:00
}
2017-05-04 16:35:00 +02:00
func (p *Pagination) toValues() url.Values {
return p.setValues(url.Values{})
}
2017-05-04 14:56:54 +02:00
func (p *Pagination) setValues(params url.Values) url.Values {
if p.MaxID != "" {
params.Set("max_id", string(p.MaxID))
2019-02-14 14:23:25 +01:00
}
if p.SinceID != "" {
params.Set("since_id", string(p.SinceID))
2017-05-04 16:35:00 +02:00
}
2019-02-14 14:23:25 +01:00
if p.MinID != "" {
params.Set("min_id", string(p.MinID))
}
if p.Limit > 0 {
params.Set("limit", fmt.Sprint(p.Limit))
2017-05-04 14:56:54 +02:00
}
return params
}