go-mastodon/mastodon.go

167 lines
4.4 KiB
Go
Raw Normal View History

2017-04-13 09:47:00 +02:00
package mastodon
import (
"encoding/json"
2017-04-13 19:39:34 +02:00
"fmt"
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-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
config *Config
}
2017-04-14 05:21:27 +02:00
func (c *Client) doAPI(method string, uri string, params url.Values, res interface{}) error {
2017-04-14 05:12:09 +02:00
url, err := url.Parse(c.config.Server)
if err != nil {
return err
}
url.Path = path.Join(url.Path, uri)
var resp *http.Response
req, err := http.NewRequest(method, url.String(), strings.NewReader(params.Encode()))
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
resp, err = c.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if res == nil {
return nil
}
if method == "GET" && resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad request: %v", resp.Status)
}
2017-04-14 05:12:09 +02:00
return json.NewDecoder(resp.Body).Decode(&res)
}
2017-04-14 05:21:27 +02:00
// NewClient return new mastodon API client.
func NewClient(config *Config) *Client {
return &Client{
2017-04-13 09:47:00 +02:00
Client: *http.DefaultClient,
config: config,
}
}
2017-04-14 05:21:27 +02:00
// Authenticate get access-token to the API.
func (c *Client) Authenticate(username, password string) error {
2017-04-13 09:47:00 +02:00
params := url.Values{}
params.Set("client_id", c.config.ClientID)
params.Set("client_secret", c.config.ClientSecret)
params.Set("grant_type", "password")
params.Set("username", username)
params.Set("password", password)
2017-04-13 19:16:52 +02:00
params.Set("scope", "read write follow")
url, err := url.Parse(c.config.Server)
if err != nil {
return err
}
url.Path = path.Join(url.Path, "/oauth/token")
req, err := http.NewRequest("POST", url.String(), strings.NewReader(params.Encode()))
2017-04-13 09:47:00 +02:00
if err != nil {
return err
}
2017-04-14 10:10:13 +02:00
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
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-14 10:10:13 +02:00
return fmt.Errorf("bad authorization: %v", resp.Status)
}
2017-04-13 09:47:00 +02:00
res := struct {
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
}
c.config.AccessToken = res.AccessToken
return nil
}
2017-04-14 05:21:27 +02:00
// Toot is struct to post status.
2017-04-13 19:16:52 +02:00
type Toot struct {
Status string `json:"status"`
InReplyToID int64 `json:"in_reply_to_id"`
2017-04-14 05:21:27 +02:00
MediaIDs []int64 `json:"media_ids"`
2017-04-13 19:16:52 +02:00
Sensitive bool `json:"sensitive"`
SpoilerText string `json:"spoiler_text"`
Visibility string `json:"visibility"`
}
2017-04-14 05:21:27 +02:00
// Status is struct to hold status.
2017-04-13 19:16:52 +02:00
type Status struct {
2017-04-13 19:39:34 +02:00
ID int64 `json:"id"`
CreatedAt time.Time `json:"created_at"`
InReplyToID interface{} `json:"in_reply_to_id"`
InReplyToAccountID interface{} `json:"in_reply_to_account_id"`
Sensitive bool `json:"sensitive"`
SpoilerText string `json:"spoiler_text"`
Visibility string `json:"visibility"`
Application interface{} `json:"application"`
Account Account `json:"account"`
MediaAttachments []interface{} `json:"media_attachments"`
Mentions []interface{} `json:"mentions"`
Tags []interface{} `json:"tags"`
URI string `json:"uri"`
Content string `json:"content"`
URL string `json:"url"`
ReblogsCount int64 `json:"reblogs_count"`
FavouritesCount int64 `json:"favourites_count"`
Reblog interface{} `json:"reblog"`
Favourited interface{} `json:"favourited"`
Reblogged interface{} `json:"reblogged"`
2017-04-13 09:47:00 +02:00
}
2017-04-14 05:21:27 +02:00
// GetTimelineHome return statuses from home timeline.
func (c *Client) GetTimelineHome() ([]*Status, error) {
2017-04-13 19:16:52 +02:00
var statuses []*Status
2017-04-14 05:12:09 +02:00
err := c.doAPI("GET", "/api/v1/timelines/home", nil, &statuses)
2017-04-13 19:16:52 +02:00
if err != nil {
return nil, err
}
return statuses, nil
}
2017-04-14 05:21:27 +02:00
// PostStatus post the toot.
func (c *Client) PostStatus(toot *Toot) (*Status, error) {
2017-04-13 19:16:52 +02:00
params := url.Values{}
params.Set("status", toot.Status)
2017-04-14 04:49:52 +02:00
if toot.InReplyToID > 0 {
params.Set("in_reply_to_id", fmt.Sprint(toot.InReplyToID))
}
2017-04-13 19:16:52 +02:00
// TODO: media_ids, senstitive, spoiler_text, visibility
//params.Set("visibility", "public")
var status Status
2017-04-14 05:12:09 +02:00
err := c.doAPI("POST", "/api/v1/statuses", params, &status)
2017-04-13 09:47:00 +02:00
if err != nil {
return nil, err
}
2017-04-13 19:16:52 +02:00
return &status, nil
2017-04-13 09:47:00 +02:00
}