From 9fa63b511cec167e49c255dfeb7f54c0958c43a6 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Fri, 28 Apr 2017 14:09:10 +0900 Subject: [PATCH] Fix local streaming --- streaming.go | 63 ++++++++++++++++++++++++----------------------- streaming_test.go | 2 +- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/streaming.go b/streaming.go index 0bff3e3..897be4c 100644 --- a/streaming.go +++ b/streaming.go @@ -86,7 +86,15 @@ func (c *Client) streaming(ctx context.Context, p string, params url.Values) (ch if err != nil { return nil, err } - u.Path = path.Join(u.Path, "/api/v1/streaming/"+p) + u.Path = path.Join(u.Path, "/api/v1/streaming", p) + u.RawQuery = params.Encode() + + req, err := http.NewRequest(http.MethodGet, u.String(), nil) + if err != nil { + return nil, err + } + req = req.WithContext(ctx) + req.Header.Set("Authorization", "Bearer "+c.config.AccessToken) var resp *http.Response @@ -95,18 +103,9 @@ func (c *Client) streaming(ctx context.Context, p string, params url.Values) (ch defer ctx.Done() for { - var in io.Reader - if params != nil { - in = strings.NewReader(params.Encode()) - } - req, err := http.NewRequest(http.MethodGet, u.String(), in) - if err == nil { - req = req.WithContext(ctx) - req.Header.Set("Authorization", "Bearer "+c.config.AccessToken) - resp, err = c.Do(req) - if resp != nil && resp.StatusCode != http.StatusOK { - err = parseAPIError("bad request", resp) - } + resp, err = c.Do(req) + if resp != nil && resp.StatusCode != http.StatusOK { + err = parseAPIError("bad request", resp) } if err == nil { err = handleReader(ctx, q, resp.Body) @@ -129,28 +128,30 @@ func (c *Client) streaming(ctx context.Context, p string, params url.Values) (ch return q, nil } -// StreamingPublic return channel to read events on public. -func (c *Client) StreamingPublic(ctx context.Context) (chan Event, error) { - params := url.Values{} - return c.streaming(ctx, "public", params) -} - -// StreamingPublicLocal return channel to read events on public. -func (c *Client) StreamingPublicLocal(ctx context.Context) (chan Event, error) { - params := url.Values{} - return c.streaming(ctx, "public/local", params) -} - // StreamingUser return channel to read events on home. -func (c *Client) StreamingUser(ctx context.Context, user string) (chan Event, error) { - params := url.Values{} - params.Set("user", user) - return c.streaming(ctx, "user", params) +func (c *Client) StreamingUser(ctx context.Context) (chan Event, error) { + return c.streaming(ctx, "user", nil) +} + +// StreamingPublic return channel to read events on public. +func (c *Client) StreamingPublic(ctx context.Context, isLocal bool) (chan Event, error) { + p := "public" + if isLocal { + p = path.Join(p, "local") + } + + return c.streaming(ctx, p, nil) } // StreamingHashtag return channel to read events on tagged timeline. -func (c *Client) StreamingHashtag(ctx context.Context, tag string) (chan Event, error) { +func (c *Client) StreamingHashtag(ctx context.Context, tag string, isLocal bool) (chan Event, error) { params := url.Values{} params.Set("tag", tag) - return c.streaming(ctx, "hashtag", params) + + p := "hashtag" + if isLocal { + p = path.Join(p, "local") + } + + return c.streaming(ctx, p, params) } diff --git a/streaming_test.go b/streaming_test.go index 838d61d..6e6153d 100644 --- a/streaming_test.go +++ b/streaming_test.go @@ -38,7 +38,7 @@ data: {"content": "bar"} AccessToken: "zoo", }) ctx, cancel := context.WithCancel(context.Background()) - q, err := client.StreamingPublic(ctx) + q, err := client.StreamingPublic(ctx, false) if err != nil { t.Fatalf("should not be fail: %v", err) }