golint & go vet
parent
bd23c855dd
commit
d8dfefc0ed
38
mastodon.go
38
mastodon.go
|
@ -14,6 +14,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Config is a setting for access mastodon APIs.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Server string
|
Server string
|
||||||
ClientID string
|
ClientID string
|
||||||
|
@ -21,12 +22,13 @@ type Config struct {
|
||||||
AccessToken string
|
AccessToken string
|
||||||
}
|
}
|
||||||
|
|
||||||
type client struct {
|
// Client is a API client for mastodon.
|
||||||
|
type Client struct {
|
||||||
http.Client
|
http.Client
|
||||||
config *Config
|
config *Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) doAPI(method string, uri string, params url.Values, res interface{}) error {
|
func (c *Client) doAPI(method string, uri string, params url.Values, res interface{}) error {
|
||||||
url, err := url.Parse(c.config.Server)
|
url, err := url.Parse(c.config.Server)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -50,14 +52,16 @@ func (c *client) doAPI(method string, uri string, params url.Values, res interfa
|
||||||
return json.NewDecoder(resp.Body).Decode(&res)
|
return json.NewDecoder(resp.Body).Decode(&res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(config *Config) *client {
|
// NewClient return new mastodon API client.
|
||||||
return &client{
|
func NewClient(config *Config) *Client {
|
||||||
|
return &Client{
|
||||||
Client: *http.DefaultClient,
|
Client: *http.DefaultClient,
|
||||||
config: config,
|
config: config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) Authenticate(username, password string) error {
|
// Authenticate get access-token to the API.
|
||||||
|
func (c *Client) Authenticate(username, password string) error {
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Set("client_id", c.config.ClientID)
|
params.Set("client_id", c.config.ClientID)
|
||||||
params.Set("client_secret", c.config.ClientSecret)
|
params.Set("client_secret", c.config.ClientSecret)
|
||||||
|
@ -150,6 +154,7 @@ func RegisterApp(appConfig *AppConfig) (*Application, error) {
|
||||||
return app, nil
|
return app, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Account hold information for mastodon account.
|
||||||
type Account struct {
|
type Account struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
|
@ -168,17 +173,17 @@ type Account struct {
|
||||||
HeaderStatic string `json:"header_static"`
|
HeaderStatic string `json:"header_static"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Visibility int64
|
// Toot is struct to post status.
|
||||||
|
|
||||||
type Toot struct {
|
type Toot struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
InReplyToID int64 `json:"in_reply_to_id"`
|
InReplyToID int64 `json:"in_reply_to_id"`
|
||||||
MediaIDs []int64 `json:"in_reply_to_id"`
|
MediaIDs []int64 `json:"media_ids"`
|
||||||
Sensitive bool `json:"sensitive"`
|
Sensitive bool `json:"sensitive"`
|
||||||
SpoilerText string `json:"spoiler_text"`
|
SpoilerText string `json:"spoiler_text"`
|
||||||
Visibility string `json:"visibility"`
|
Visibility string `json:"visibility"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Status is struct to hold status.
|
||||||
type Status struct {
|
type Status struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
@ -202,7 +207,8 @@ type Status struct {
|
||||||
Reblogged interface{} `json:"reblogged"`
|
Reblogged interface{} `json:"reblogged"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) GetAccount(id int) (*Account, error) {
|
// GetAccount return Account.
|
||||||
|
func (c *Client) GetAccount(id int) (*Account, error) {
|
||||||
var account Account
|
var account Account
|
||||||
err := c.doAPI("GET", fmt.Sprintf("/api/v1/accounts/%d", id), nil, &account)
|
err := c.doAPI("GET", fmt.Sprintf("/api/v1/accounts/%d", id), nil, &account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -211,7 +217,8 @@ func (c *client) GetAccount(id int) (*Account, error) {
|
||||||
return &account, nil
|
return &account, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) GetTimelineHome() ([]*Status, error) {
|
// GetTimelineHome return statuses from home timeline.
|
||||||
|
func (c *Client) GetTimelineHome() ([]*Status, error) {
|
||||||
var statuses []*Status
|
var statuses []*Status
|
||||||
err := c.doAPI("GET", "/api/v1/timelines/home", nil, &statuses)
|
err := c.doAPI("GET", "/api/v1/timelines/home", nil, &statuses)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -220,7 +227,8 @@ func (c *client) GetTimelineHome() ([]*Status, error) {
|
||||||
return statuses, nil
|
return statuses, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) PostStatus(toot *Toot) (*Status, error) {
|
// PostStatus post the toot.
|
||||||
|
func (c *Client) PostStatus(toot *Toot) (*Status, error) {
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Set("status", toot.Status)
|
params.Set("status", toot.Status)
|
||||||
if toot.InReplyToID > 0 {
|
if toot.InReplyToID > 0 {
|
||||||
|
@ -237,23 +245,27 @@ func (c *client) PostStatus(toot *Toot) (*Status, error) {
|
||||||
return &status, nil
|
return &status, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateEvent is struct for passing status event to app.
|
||||||
type UpdateEvent struct {
|
type UpdateEvent struct {
|
||||||
Status *Status
|
Status *Status
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *UpdateEvent) event() {}
|
func (e *UpdateEvent) event() {}
|
||||||
|
|
||||||
|
// NotificationEvent is struct for passing notification event to app.
|
||||||
type NotificationEvent struct {
|
type NotificationEvent struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *NotificationEvent) event() {}
|
func (e *NotificationEvent) event() {}
|
||||||
|
|
||||||
|
// DeleteEvent is struct for passing deletion event to app.
|
||||||
type DeleteEvent struct {
|
type DeleteEvent struct {
|
||||||
ID int64
|
ID int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *DeleteEvent) event() {}
|
func (e *DeleteEvent) event() {}
|
||||||
|
|
||||||
|
// ErrorEvent is struct for passing errors to app.
|
||||||
type ErrorEvent struct {
|
type ErrorEvent struct {
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
@ -261,11 +273,13 @@ type ErrorEvent struct {
|
||||||
func (e *ErrorEvent) Error() string { return e.err.Error() }
|
func (e *ErrorEvent) Error() string { return e.err.Error() }
|
||||||
func (e *ErrorEvent) event() {}
|
func (e *ErrorEvent) event() {}
|
||||||
|
|
||||||
|
// Event is interface passing events to app.
|
||||||
type Event interface {
|
type Event interface {
|
||||||
event()
|
event()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) StreamingPublic(ctx context.Context) (chan Event, error) {
|
// StreamingPublic return channel to read events.
|
||||||
|
func (c *Client) StreamingPublic(ctx context.Context) (chan Event, error) {
|
||||||
url, err := url.Parse(c.config.Server)
|
url, err := url.Parse(c.config.Server)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in New Issue