Skip to content

Commit

Permalink
Syntax change
Browse files Browse the repository at this point in the history
  • Loading branch information
adeithe committed Sep 17, 2023
1 parent 039e46e commit 4860139
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 14 deletions.
21 changes: 20 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
coverage.out
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
__debug_bin

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Go workspace file
go.work

.DS_Store
.idea/
.vscode/
4 changes: 2 additions & 2 deletions api/twitch_channel_points.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (r *CustomRewardsResource) List(broadcasterId string) *CustomRewardsListCal
}

// ID filters the results to the specified reward IDs.
func (c *CustomRewardsListCall) ID(ids ...string) *CustomRewardsListCall {
func (c *CustomRewardsListCall) ID(ids []string) *CustomRewardsListCall {
for _, id := range ids {
c.opts = append(c.opts, SetQueryParameter("id", id))
}
Expand Down Expand Up @@ -400,7 +400,7 @@ func (c *CustomRewardsRedemptionListCall) Status(status string) *CustomRewardsRe
}

// ID filters the results to the specified reward redemption IDs.
func (c *CustomRewardsRedemptionListCall) ID(ids ...string) *CustomRewardsRedemptionListCall {
func (c *CustomRewardsRedemptionListCall) ID(ids []string) *CustomRewardsRedemptionListCall {
for _, id := range ids {
c.opts = append(c.opts, AddQueryParameter("id", id))
}
Expand Down
2 changes: 1 addition & 1 deletion api/twitch_channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (r *ChannelsResource) List() *ChannelsListCall {
}

// BroadcasterID filters the results to the specified broadcaster ID.
func (c *ChannelsListCall) BroadcasterID(ids ...string) *ChannelsListCall {
func (c *ChannelsListCall) BroadcasterID(ids []string) *ChannelsListCall {
for _, id := range ids {
c.opts = append(c.opts, AddQueryParameter("broadcaster_id", id))
}
Expand Down
8 changes: 4 additions & 4 deletions api/twitch_streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,23 @@ func (r *StreamsResource) List() *StreamsListCall {
}

// UserID filters the results to the specified user IDs.
func (c *StreamsListCall) UserID(ids ...string) *StreamsListCall {
func (c *StreamsListCall) UserID(ids []string) *StreamsListCall {
for _, id := range ids {
c.opts = append(c.opts, AddQueryParameter("user_id", id))
}
return c
}

// Username filters the results to the specified usernames.
func (c *StreamsListCall) Username(usernames ...string) *StreamsListCall {
func (c *StreamsListCall) Username(usernames []string) *StreamsListCall {
for _, username := range usernames {
c.opts = append(c.opts, AddQueryParameter("user_login", username))
}
return c
}

// GameID filters the results to the specified game IDs.
func (c *StreamsListCall) GameID(ids ...string) *StreamsListCall {
func (c *StreamsListCall) GameID(ids []string) *StreamsListCall {
for _, id := range ids {
c.opts = append(c.opts, AddQueryParameter("game_id", id))
}
Expand All @@ -83,7 +83,7 @@ func (c *StreamsListCall) Type(t string) *StreamsListCall {
}

// Language filters the results to the specified languages.
func (c *StreamsListCall) Languages(languages ...string) *StreamsListCall {
func (c *StreamsListCall) Languages(languages []string) *StreamsListCall {
for _, language := range languages {
c.opts = append(c.opts, AddQueryParameter("language", language))
}
Expand Down
4 changes: 2 additions & 2 deletions api/twitch_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ func (r *UsersResource) List() *UsersListCall {
}

// ID filters the results to the specified user IDs.
func (c *UsersListCall) ID(ids ...string) *UsersListCall {
func (c *UsersListCall) ID(ids []string) *UsersListCall {
for _, id := range ids {
c.opts = append(c.opts, AddQueryParameter("id", id))
}
return c
}

// Login filters the results to the specified usernames.
func (c *UsersListCall) Login(logins ...string) *UsersListCall {
func (c *UsersListCall) Login(logins []string) *UsersListCall {
for _, login := range logins {
c.opts = append(c.opts, AddQueryParameter("login", login))
}
Expand Down
167 changes: 167 additions & 0 deletions api/twitch_videos.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,176 @@
package api

import (
"context"
"fmt"
"net/http"
"time"
)

type Video struct {
ID string `json:"id"`
StreamID string `json:"stream_id"`
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserDisplayName string `json:"user_name"`
Title string `json:"title"`
Description string `json:"description"`
URL string `json:"url"`
ThumbnailURL string `json:"thumbnail_url"`
Viewable string `json:"viewable"`
ViewCount int `json:"view_count"`
Language string `json:"language"`
Type string `json:"type"`
Duration time.Duration `json:"duration"`
PublishedAt time.Time `json:"published_at"`
CreatedAt time.Time `json:"created_at"`
}

type VideosResource struct {
client *Client
}

func NewVideosResource(client *Client) *VideosResource {
return &VideosResource{client}
}

type VideosListCall struct {
resource *VideosResource
opts []RequestOption
}

type VideosListResponse struct {
Header http.Header
Data []Video
Cursor string
}

// List creates a new call to list videos.
//
// One of ID, UserID, or GameID must be specified.
func (r *VideosResource) List() *VideosListCall {
return &VideosListCall{resource: r}
}

// ID filters the results to those with the specified ID.
func (c *VideosListCall) ID(ids []string) *VideosListCall {
for _, id := range ids {
c.opts = append(c.opts, AddQueryParameter("id", id))
}
return c
}

// UserID filters the results to those with the specified user ID.
func (c *VideosListCall) UserID(id string) *VideosListCall {
c.opts = append(c.opts, SetQueryParameter("user_id", id))
return c
}

// GameID filters the results to those with the specified game ID.
func (c *VideosListCall) GameID(id string) *VideosListCall {
c.opts = append(c.opts, SetQueryParameter("game_id", id))
return c
}

// Language filters the results to those with the specified language.
func (c *VideosListCall) Language(language string) *VideosListCall {
c.opts = append(c.opts, SetQueryParameter("language", language))
return c
}

// Period filters the results to those with a specified period.
//
// Possible values: "all", "day", "week", "month" (default: all)
func (c *VideosListCall) Period(p string) *VideosListCall {
c.opts = append(c.opts, SetQueryParameter("period", p))
return c
}

// Sort sets the order in which to list videos.
//
// Possible values: "time", "trending", "views" (default: time)
func (c *VideosListCall) Sort(s string) *VideosListCall {
c.opts = append(c.opts, SetQueryParameter("sort", s))
return c
}

func (c *VideosListCall) First(n int) *VideosListCall {
c.opts = append(c.opts, SetQueryParameter("first", fmt.Sprint(n)))
return c
}

// Type filters the results to those with the specified type.
//
// Possible values: "all", "upload", "archive", "highlight" (default: all)
func (c *VideosListCall) Type(t string) *VideosListCall {
c.opts = append(c.opts, SetQueryParameter("type", t))
return c
}

// Before filters the results to those with a cursor value before the specified cursor.
func (c *VideosListCall) Before(cursor string) *VideosListCall {
c.opts = append(c.opts, SetQueryParameter("before", cursor))
return c
}

// After filters the results to those with a cursor value after the specified cursor.
func (c *VideosListCall) After(cursor string) *VideosListCall {
c.opts = append(c.opts, SetQueryParameter("after", cursor))
return c
}

func (c *VideosListCall) Do(ctx context.Context, opts ...RequestOption) (*VideosListResponse, error) {
res, err := c.resource.client.doRequest(ctx, http.MethodGet, "/videos", nil, append(c.opts, opts...)...)
if err != nil {
return nil, err
}
defer res.Body.Close()

data, err := decodeResponse[Video](res)
if err != nil {
return nil, err
}

return &VideosListResponse{
Header: res.Header,
Data: data.Data,
Cursor: data.Pagination.Cursor,
}, nil
}

type VideosDeleteCall struct {
resource *VideosResource
opts []RequestOption
}

type VideosDeleteResponse struct {
Header http.Header
Data []string
}

// Delete creates a new call to delete videos.
func (r *VideosResource) Delete(ids []string) *VideosDeleteCall {
c := &VideosDeleteCall{resource: r}
for _, id := range ids {
c.opts = append(c.opts, AddQueryParameter("id", id))
}
return c
}

func (c *VideosDeleteCall) Do(ctx context.Context, opts ...RequestOption) (*VideosDeleteResponse, error) {
res, err := c.resource.client.doRequest(ctx, http.MethodDelete, "/videos", nil, append(c.opts, opts...)...)
if err != nil {
return nil, err
}
defer res.Body.Close()

data, err := decodeResponse[string](res)
if err != nil {
return nil, err
}

return &VideosDeleteResponse{
Header: res.Header,
Data: data.Data,
}, nil
}
8 changes: 4 additions & 4 deletions api/twitch_whispers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func NewWhispersResource(client *Client) *WhispersResource {
type WhispersInsertCall struct {
resource *WhispersResource
opts []RequestOption
message string
body map[string]interface{}
}

func (r *WhispersResource) Insert(senderId, recipientId string) *WhispersInsertCall {
c := &WhispersInsertCall{resource: r}
c := &WhispersInsertCall{resource: r, body: make(map[string]interface{})}
c.opts = append(c.opts, SetQueryParameter("from_user_id", senderId))
c.opts = append(c.opts, SetQueryParameter("to_user_id", recipientId))
return c
Expand All @@ -36,7 +36,7 @@ func (r *WhispersResource) Insert(senderId, recipientId string) *WhispersInsertC
//
// Messages that exceed the maximum length are truncated.
func (c *WhispersInsertCall) Message(message string) *WhispersInsertCall {
c.message = message
c.body["message"] = message
return c
}

Expand All @@ -45,7 +45,7 @@ func (c *WhispersInsertCall) Message(message string) *WhispersInsertCall {
// req := client.Whispers.SendWhisper("123", "456").Message("Hello")
// data, err := req.Do(ctx, api.WithBearerToken("kpvy3cjboyptmdkiacwr0c19hotn5s")
func (c *WhispersInsertCall) Do(ctx context.Context, opts ...RequestOption) error {
bs, err := json.Marshal(map[string]any{"message": c.message})
bs, err := json.Marshal(c.body)
if err != nil {
return err
}
Expand Down

0 comments on commit 4860139

Please sign in to comment.