From bf42b86b9ff81e68b57f0778512599089182b70a Mon Sep 17 00:00:00 2001 From: WaybackBot <66856220+warcbot@users.noreply.github.com> Date: Thu, 25 Feb 2021 14:26:45 +0000 Subject: [PATCH] Add streaming direct support --- README.md | 1 + streaming.go | 5 +++++ streaming_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/README.md b/README.md index 44f5066..75e8fb1 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,7 @@ func main() { * [x] GET /api/v1/streaming/hashtag?tag=:hashtag * [x] GET /api/v1/streaming/hashtag/local?tag=:hashtag * [x] GET /api/v1/streaming/list?list=:list_id +* [x] GET /api/v1/streaming/direct ## Installation diff --git a/streaming.go b/streaming.go index b57f454..f560fd8 100644 --- a/streaming.go +++ b/streaming.go @@ -164,3 +164,8 @@ func (c *Client) StreamingList(ctx context.Context, id ID) (chan Event, error) { return c.streaming(ctx, "list", params) } + +// StreamingDirect return channel to read events on a direct messages. +func (c *Client) StreamingDirect(ctx context.Context) (chan Event, error) { + return c.streaming(ctx, "direct", nil) +} diff --git a/streaming_test.go b/streaming_test.go index bc0e867..0bd5ccc 100644 --- a/streaming_test.go +++ b/streaming_test.go @@ -333,3 +333,43 @@ data: {"content": "foo"} t.Fatalf("want %q but %q", "foo", events[0].(*UpdateEvent).Status.Content) } } + +func TestStreamingDirect(t *testing.T) { + var isEnd bool + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if isEnd { + return + } else if r.URL.Path != "/api/v1/streaming/direct" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + f, _ := w.(http.Flusher) + fmt.Fprintln(w, ` +event: update +data: {"content": "foo"} + `) + f.Flush() + isEnd = true + })) + defer ts.Close() + + client := NewClient(&Config{Server: ts.URL}) + ctx, cancel := context.WithCancel(context.Background()) + time.AfterFunc(time.Second, cancel) + q, err := client.StreamingDirect(ctx) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + events := []Event{} + for e := range q { + if _, ok := e.(*ErrorEvent); !ok { + events = append(events, e) + } + } + if len(events) != 1 { + t.Fatalf("result should be one: %d", len(events)) + } + if events[0].(*UpdateEvent).Status.Content != "foo" { + t.Fatalf("want %q but %q", "foo", events[0].(*UpdateEvent).Status.Content) + } +}