From bbb4df76cabed842ae899ed142a127b65c6379b3 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Sun, 16 Apr 2017 15:32:48 +0900 Subject: [PATCH] Add Favourite and Unfavourite --- status.go | 20 +++++++++++++++++ status_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/status.go b/status.go index 2b180b2..115232a 100644 --- a/status.go +++ b/status.go @@ -125,6 +125,26 @@ func (c *Client) Unreblog(id int64) (*Status, error) { 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. func (c *Client) GetTimelineHome() ([]*Status, error) { var statuses []*Status diff --git a/status_test.go b/status_test.go index 3c0b8a2..c88bcaf 100644 --- a/status_test.go +++ b/status_test.go @@ -166,3 +166,63 @@ func TestUnreblog(t *testing.T) { 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) + } +}