Fix local streaming

This commit is contained in:
178inaba 2017-04-28 14:09:10 +09:00
parent a4904109e4
commit 9fa63b511c
2 changed files with 33 additions and 32 deletions

View File

@ -86,7 +86,15 @@ func (c *Client) streaming(ctx context.Context, p string, params url.Values) (ch
if err != nil { if err != nil {
return nil, err 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 var resp *http.Response
@ -95,19 +103,10 @@ func (c *Client) streaming(ctx context.Context, p string, params url.Values) (ch
defer ctx.Done() defer ctx.Done()
for { 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) resp, err = c.Do(req)
if resp != nil && resp.StatusCode != http.StatusOK { if resp != nil && resp.StatusCode != http.StatusOK {
err = parseAPIError("bad request", resp) err = parseAPIError("bad request", resp)
} }
}
if err == nil { if err == nil {
err = handleReader(ctx, q, resp.Body) err = handleReader(ctx, q, resp.Body)
if err == nil { if err == nil {
@ -129,28 +128,30 @@ func (c *Client) streaming(ctx context.Context, p string, params url.Values) (ch
return q, nil 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. // StreamingUser return channel to read events on home.
func (c *Client) StreamingUser(ctx context.Context, user string) (chan Event, error) { func (c *Client) StreamingUser(ctx context.Context) (chan Event, error) {
params := url.Values{} return c.streaming(ctx, "user", nil)
params.Set("user", user) }
return c.streaming(ctx, "user", params)
// 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. // 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 := url.Values{}
params.Set("tag", tag) 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)
} }

View File

@ -38,7 +38,7 @@ data: {"content": "bar"}
AccessToken: "zoo", AccessToken: "zoo",
}) })
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
q, err := client.StreamingPublic(ctx) q, err := client.StreamingPublic(ctx, false)
if err != nil { if err != nil {
t.Fatalf("should not be fail: %v", err) t.Fatalf("should not be fail: %v", err)
} }