Add Favourite and Unfavourite

pull/15/head
178inaba 2017-04-16 15:32:48 +09:00
parent 7719f511aa
commit bbb4df76ca
2 changed files with 80 additions and 0 deletions

View File

@ -125,6 +125,26 @@ func (c *Client) Unreblog(id int64) (*Status, error) {
return &status, nil return &status, nil
} }
// Favourite is favourite the toot of id and return status of the favourite toot.
func (c *Client) Favourite(id int64) (*Status, error) {
var status Status
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%d/favourite", id), nil, &status)
if err != nil {
return nil, err
}
return &status, nil
}
// Unfavourite is unfavourite the toot of id and return status of the unfavourite toot.
func (c *Client) Unfavourite(id int64) (*Status, error) {
var status Status
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%d/unfavourite", id), nil, &status)
if err != nil {
return nil, err
}
return &status, nil
}
// GetTimelineHome return statuses from home timeline. // GetTimelineHome return statuses from home timeline.
func (c *Client) GetTimelineHome() ([]*Status, error) { func (c *Client) GetTimelineHome() ([]*Status, error) {
var statuses []*Status var statuses []*Status

View File

@ -166,3 +166,63 @@ func TestUnreblog(t *testing.T) {
t.Fatalf("want %q but %q", "zzz", status.Content) t.Fatalf("want %q but %q", "zzz", status.Content)
} }
} }
func TestFavourite(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/statuses/1234567/favourite" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `{"Content": "zzz"}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.Favourite(123)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
status, err := client.Favourite(1234567)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if status.Content != "zzz" {
t.Fatalf("want %q but %q", "zzz", status.Content)
}
}
func TestUnfavourite(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/statuses/1234567/unfavourite" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `{"Content": "zzz"}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.Unfavourite(123)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
status, err := client.Unfavourite(1234567)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if status.Content != "zzz" {
t.Fatalf("want %q but %q", "zzz", status.Content)
}
}