From 315f7bbd29e7ea2f666e9a3c7acf4e48f43982f7 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Wed, 26 Apr 2017 03:16:32 +0900 Subject: [PATCH] Add GetTimelinePublic --- status.go | 17 ++++++++++++++++- status_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/status.go b/status.go index 11c750b..cf2f036 100644 --- a/status.go +++ b/status.go @@ -5,8 +5,8 @@ import ( "fmt" "net/http" "net/url" - "time" "strconv" + "time" ) // Status is struct to hold status. @@ -157,6 +157,21 @@ func (c *Client) GetTimelineHome(ctx context.Context) ([]*Status, error) { return statuses, nil } +// GetTimelinePublic return statuses from public timeline. +func (c *Client) GetTimelinePublic(ctx context.Context, isLocal bool) ([]*Status, error) { + params := url.Values{} + if isLocal { + params.Set("local", "t") + } + + var statuses []*Status + err := c.doAPI(ctx, http.MethodGet, "/api/v1/timelines/public", params, &statuses, nil) + if err != nil { + return nil, err + } + return statuses, nil +} + // GetTimelineHashtag return statuses from tagged timeline. func (c *Client) GetTimelineHashtag(ctx context.Context, tag string) ([]*Status, error) { var statuses []*Status diff --git a/status_test.go b/status_test.go index d563a57..a511afe 100644 --- a/status_test.go +++ b/status_test.go @@ -327,6 +327,36 @@ func TestUnfavourite(t *testing.T) { } } +func TestGetTimelinePublic(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Query().Get("local") == "" { + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + fmt.Fprintln(w, `[{"content": "foo"}, {"content": "bar"}]`) + })) + defer ts.Close() + + client := NewClient(&Config{Server: ts.URL}) + _, err := client.GetTimelinePublic(context.Background(), false) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + tl, err := client.GetTimelinePublic(context.Background(), true) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if len(tl) != 2 { + t.Fatalf("result should be two: %d", len(tl)) + } + if tl[0].Content != "foo" { + t.Fatalf("want %q but %q", "foo", tl[0].Content) + } + if tl[1].Content != "bar" { + t.Fatalf("want %q but %q", "bar", tl[1].Content) + } +} + func TestGetTimelineHashtag(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/api/v1/timelines/tag/zzz" {