Create LiveThreadService

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian
2020-09-15 21:41:12 -04:00
parent 77d0d257d3
commit 359f8126a1
6 changed files with 136 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
package reddit
import (
"context"
"fmt"
"net/http"
)
// LiveThreadService handles communication with the live thread
// related methods of the Reddit API.
//
// Reddit API docs: https://www.reddit.com/dev/api/#section_live
type LiveThreadService struct {
client *Client
}
// LiveThread is a thread on Reddit that provides real-time updates.
type LiveThread struct {
ID string `json:"id,omitempty"`
FullID string `json:"name,omitempty"`
Created *Timestamp `json:"created_utc,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Resources string `json:"resources,omitempty"`
State string `json:"state,omitempty"`
ViewerCount int `json:"viewer_count"`
ViewerCountFuzzed bool `json:"viewer_count_fuzzed"`
WebSocketURL string `json:"websocket_url,omitempty"`
Announcement bool `json:"is_announcement"`
NSFW bool `json:"nsfw"`
}
// Get information about a live thread.
func (s *LiveThreadService) Get(ctx context.Context, id string) (*LiveThread, *Response, error) {
path := fmt.Sprintf("live/%s/about", id)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(thing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
t, _ := root.LiveThread()
return t, resp, nil
}
+46
View File
@@ -0,0 +1,46 @@
package reddit
import (
"fmt"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
)
var expectedLiveThread = &LiveThread{
ID: "15nevtv8e54dh",
FullID: "LiveUpdateEvent_15nevtv8e54dh",
Created: &Timestamp{time.Date(2020, 9, 16, 1, 20, 27, 0, time.UTC)},
Title: "test",
Description: "test",
Resources: "",
State: "live",
ViewerCount: 6,
ViewerCountFuzzed: true,
WebSocketURL: "wss://ws-078adc7cb2099a9df.wss.redditmedia.com/live/15nevtv8e54dh?m=AQAA7rxiX6EpLYFCFZ0KJD4lVAPaMt0A1z2-xJ1b2dWCmxNIfMwL",
Announcement: false,
NSFW: false,
}
func TestLiveThreadService_Get(t *testing.T) {
client, mux, teardown := setup()
defer teardown()
blob, err := readFileContents("../testdata/live-thread/live-thread.json")
require.NoError(t, err)
mux.HandleFunc("/live/id123/about", func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodGet, r.Method)
fmt.Fprint(w, blob)
})
liveThread, _, err := client.LiveThread.Get(ctx, "id123")
require.NoError(t, err)
require.Equal(t, expectedLiveThread, liveThread)
}
+2
View File
@@ -71,6 +71,7 @@ type Client struct {
Flair *FlairService
Gold *GoldService
Listings *ListingsService
LiveThread *LiveThreadService
Message *MessageService
Moderation *ModerationService
Multi *MultiService
@@ -102,6 +103,7 @@ func newClient() *Client {
client.Flair = &FlairService{client: client}
client.Gold = &GoldService{client: client}
client.Listings = &ListingsService{client: client}
client.LiveThread = &LiveThreadService{client: client}
client.Message = &MessageService{client: client}
client.Moderation = &ModerationService{client: client}
client.Multi = &MultiService{client: client}
+1
View File
@@ -64,6 +64,7 @@ func testClientServices(t *testing.T, c *Client) {
"Flair",
"Gold",
"Listings",
"LiveThread",
"Message",
"Moderation",
"Multi",
+8
View File
@@ -17,6 +17,7 @@ const (
kindTrophyList = "TrophyList"
kindUserList = "UserList"
kindMore = "more"
kindLiveThread = "LiveUpdateEvent"
kindModAction = "modaction"
kindMulti = "LabeledMulti"
kindMultiDescription = "LabeledMultiDescription"
@@ -89,6 +90,8 @@ func (t *thing) UnmarshalJSON(b []byte) error {
v = new(Post)
case kindSubreddit:
v = new(Subreddit)
case kindLiveThread:
v = new(LiveThread)
case kindModAction:
v = new(ModAction)
case kindMulti:
@@ -152,6 +155,11 @@ func (t *thing) Subreddit() (v *Subreddit, ok bool) {
return
}
func (t *thing) LiveThread() (v *LiveThread, ok bool) {
v, ok = t.Data.(*LiveThread)
return
}
func (t *thing) ModAction() (v *ModAction, ok bool) {
v, ok = t.Data.(*ModAction)
return