breaking compatibility changes. take context for first arguments.

This commit is contained in:
Yasuhiro Matsumoto 2017-04-17 11:10:29 +09:00
parent 32e66a3d72
commit 5e84b57bf3
18 changed files with 176 additions and 139 deletions

View file

@ -1,6 +1,7 @@
package mastodon
import (
"context"
"fmt"
"net/http"
"net/url"
@ -27,9 +28,9 @@ type Account struct {
}
// GetAccount return Account.
func (c *Client) GetAccount(id int) (*Account, error) {
func (c *Client) GetAccount(ctx context.Context, id int) (*Account, error) {
var account Account
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d", id), nil, &account)
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d", id), nil, &account)
if err != nil {
return nil, err
}
@ -37,9 +38,9 @@ func (c *Client) GetAccount(id int) (*Account, error) {
}
// GetAccountCurrentUser return Account of current user.
func (c *Client) GetAccountCurrentUser() (*Account, error) {
func (c *Client) GetAccountCurrentUser(ctx context.Context) (*Account, error) {
var account Account
err := c.doAPI(http.MethodGet, "/api/v1/accounts/verify_credentials", nil, &account)
err := c.doAPI(ctx, http.MethodGet, "/api/v1/accounts/verify_credentials", nil, &account)
if err != nil {
return nil, err
}
@ -59,7 +60,7 @@ type Profile struct {
}
// AccountUpdate updates the information of the current user.
func (c *Client) AccountUpdate(profile *Profile) (*Account, error) {
func (c *Client) AccountUpdate(ctx context.Context, profile *Profile) (*Account, error) {
params := url.Values{}
if profile.DisplayName != nil {
params.Set("display_name", *profile.DisplayName)
@ -75,7 +76,7 @@ func (c *Client) AccountUpdate(profile *Profile) (*Account, error) {
}
var account Account
err := c.doAPI(http.MethodPatch, "/api/v1/accounts/update_credentials", params, &account)
err := c.doAPI(ctx, http.MethodPatch, "/api/v1/accounts/update_credentials", params, &account)
if err != nil {
return nil, err
}
@ -83,9 +84,9 @@ func (c *Client) AccountUpdate(profile *Profile) (*Account, error) {
}
// GetAccountStatuses return statuses by specified accuont.
func (c *Client) GetAccountStatuses(id int64) ([]*Status, error) {
func (c *Client) GetAccountStatuses(ctx context.Context, id int64) ([]*Status, error) {
var statuses []*Status
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/statuses", id), nil, &statuses)
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/statuses", id), nil, &statuses)
if err != nil {
return nil, err
}
@ -93,9 +94,9 @@ func (c *Client) GetAccountStatuses(id int64) ([]*Status, error) {
}
// GetAccountFollowers return followers list.
func (c *Client) GetAccountFollowers(id int64) ([]*Account, error) {
func (c *Client) GetAccountFollowers(ctx context.Context, id int64) ([]*Account, error) {
var accounts []*Account
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/followers", id), nil, &accounts)
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/followers", id), nil, &accounts)
if err != nil {
return nil, err
}
@ -103,9 +104,9 @@ func (c *Client) GetAccountFollowers(id int64) ([]*Account, error) {
}
// GetAccountFollowing return following list.
func (c *Client) GetAccountFollowing(id int64) ([]*Account, error) {
func (c *Client) GetAccountFollowing(ctx context.Context, id int64) ([]*Account, error) {
var accounts []*Account
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/following", id), nil, &accounts)
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/following", id), nil, &accounts)
if err != nil {
return nil, err
}
@ -113,9 +114,9 @@ func (c *Client) GetAccountFollowing(id int64) ([]*Account, error) {
}
// GetBlocks return block list.
func (c *Client) GetBlocks() ([]*Account, error) {
func (c *Client) GetBlocks(ctx context.Context) ([]*Account, error) {
var accounts []*Account
err := c.doAPI(http.MethodGet, "/api/v1/blocks", nil, &accounts)
err := c.doAPI(ctx, http.MethodGet, "/api/v1/blocks", nil, &accounts)
if err != nil {
return nil, err
}
@ -133,9 +134,9 @@ type Relationship struct {
}
// AccountFollow follow the account.
func (c *Client) AccountFollow(id int64) (*Relationship, error) {
func (c *Client) AccountFollow(ctx context.Context, id int64) (*Relationship, error) {
var relationship Relationship
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/follow", id), nil, &relationship)
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/follow", id), nil, &relationship)
if err != nil {
return nil, err
}
@ -143,9 +144,9 @@ func (c *Client) AccountFollow(id int64) (*Relationship, error) {
}
// AccountUnfollow unfollow the account.
func (c *Client) AccountUnfollow(id int64) (*Relationship, error) {
func (c *Client) AccountUnfollow(ctx context.Context, id int64) (*Relationship, error) {
var relationship Relationship
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/unfollow", id), nil, &relationship)
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/unfollow", id), nil, &relationship)
if err != nil {
return nil, err
}
@ -153,9 +154,9 @@ func (c *Client) AccountUnfollow(id int64) (*Relationship, error) {
}
// AccountBlock block the account.
func (c *Client) AccountBlock(id int64) (*Relationship, error) {
func (c *Client) AccountBlock(ctx context.Context, id int64) (*Relationship, error) {
var relationship Relationship
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/block", id), nil, &relationship)
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/block", id), nil, &relationship)
if err != nil {
return nil, err
}
@ -163,9 +164,9 @@ func (c *Client) AccountBlock(id int64) (*Relationship, error) {
}
// AccountUnblock unblock the account.
func (c *Client) AccountUnblock(id int64) (*Relationship, error) {
func (c *Client) AccountUnblock(ctx context.Context, id int64) (*Relationship, error) {
var relationship Relationship
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/unblock", id), nil, &relationship)
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/unblock", id), nil, &relationship)
if err != nil {
return nil, err
}
@ -173,9 +174,9 @@ func (c *Client) AccountUnblock(id int64) (*Relationship, error) {
}
// AccountMute mute the account.
func (c *Client) AccountMute(id int64) (*Relationship, error) {
func (c *Client) AccountMute(ctx context.Context, id int64) (*Relationship, error) {
var relationship Relationship
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/mute", id), nil, &relationship)
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/mute", id), nil, &relationship)
if err != nil {
return nil, err
}
@ -183,9 +184,9 @@ func (c *Client) AccountMute(id int64) (*Relationship, error) {
}
// AccountUnmute unmute the account.
func (c *Client) AccountUnmute(id int64) (*Relationship, error) {
func (c *Client) AccountUnmute(ctx context.Context, id int64) (*Relationship, error) {
var relationship Relationship
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/unmute", id), nil, &relationship)
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/unmute", id), nil, &relationship)
if err != nil {
return nil, err
}
@ -193,12 +194,12 @@ func (c *Client) AccountUnmute(id int64) (*Relationship, error) {
}
// GetAccountRelationship return relationship for the account.
func (c *Client) GetAccountRelationship(id int64) ([]*Relationship, error) {
func (c *Client) GetAccountRelationship(ctx context.Context, id int64) ([]*Relationship, error) {
params := url.Values{}
params.Set("id", fmt.Sprint(id))
var relationships []*Relationship
err := c.doAPI(http.MethodGet, "/api/v1/accounts/relationship", params, &relationships)
err := c.doAPI(ctx, http.MethodGet, "/api/v1/accounts/relationship", params, &relationships)
if err != nil {
return nil, err
}
@ -206,13 +207,13 @@ func (c *Client) GetAccountRelationship(id int64) ([]*Relationship, error) {
}
// AccountsSearch search accounts by query.
func (c *Client) AccountsSearch(q string, limit int64) ([]*Account, error) {
func (c *Client) AccountsSearch(ctx context.Context, q string, limit int64) ([]*Account, error) {
params := url.Values{}
params.Set("q", q)
params.Set("limit", fmt.Sprint(limit))
var accounts []*Account
err := c.doAPI(http.MethodGet, "/api/v1/accounts/search", params, &accounts)
err := c.doAPI(ctx, http.MethodGet, "/api/v1/accounts/search", params, &accounts)
if err != nil {
return nil, err
}
@ -220,12 +221,12 @@ func (c *Client) AccountsSearch(q string, limit int64) ([]*Account, error) {
}
// FollowRemoteUser send follow-request.
func (c *Client) FollowRemoteUser(uri string) (*Account, error) {
func (c *Client) FollowRemoteUser(ctx context.Context, uri string) (*Account, error) {
params := url.Values{}
params.Set("uri", uri)
var account Account
err := c.doAPI(http.MethodPost, "/api/v1/follows", params, &account)
err := c.doAPI(ctx, http.MethodPost, "/api/v1/follows", params, &account)
if err != nil {
return nil, err
}
@ -233,9 +234,9 @@ func (c *Client) FollowRemoteUser(uri string) (*Account, error) {
}
// GetFollowRequests return follow-requests.
func (c *Client) GetFollowRequests() ([]*Account, error) {
func (c *Client) GetFollowRequests(ctx context.Context) ([]*Account, error) {
var accounts []*Account
err := c.doAPI(http.MethodGet, "/api/v1/follow_requests", nil, &accounts)
err := c.doAPI(ctx, http.MethodGet, "/api/v1/follow_requests", nil, &accounts)
if err != nil {
return nil, err
}
@ -243,11 +244,11 @@ func (c *Client) GetFollowRequests() ([]*Account, error) {
}
// FollowRequestAuthorize is authorize the follow request of user with id.
func (c *Client) FollowRequestAuthorize(id int64) error {
return c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/follow_requests/%d/authorize", id), nil, nil)
func (c *Client) FollowRequestAuthorize(ctx context.Context, id int64) error {
return c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/follow_requests/%d/authorize", id), nil, nil)
}
// FollowRequestReject is rejects the follow request of user with id.
func (c *Client) FollowRequestReject(id int64) error {
return c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/follow_requests/%d/reject", id), nil, nil)
func (c *Client) FollowRequestReject(ctx context.Context, id int64) error {
return c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/follow_requests/%d/reject", id), nil, nil)
}