From 55b5f772fe1073ed85122f25e3170cdaab7b5e0d Mon Sep 17 00:00:00 2001 From: Evan Fiordeliso Date: Sun, 3 Mar 2024 18:00:22 -0500 Subject: [PATCH] Add Teams endpoints to API --- api/api.go | 3 ++ api/teams/get_channel_teams.go | 78 +++++++++++++++++++++++++++++ api/teams/get_teams.go | 91 ++++++++++++++++++++++++++++++++++ api/teams/teams.go | 18 +++++++ 4 files changed, 190 insertions(+) create mode 100644 api/teams/get_channel_teams.go create mode 100644 api/teams/get_teams.go create mode 100644 api/teams/teams.go diff --git a/api/api.go b/api/api.go index fef7a43..a6eea13 100644 --- a/api/api.go +++ b/api/api.go @@ -28,6 +28,7 @@ import ( "go.fifitido.net/twitch/api/search" "go.fifitido.net/twitch/api/streams" "go.fifitido.net/twitch/api/subscriptions" + "go.fifitido.net/twitch/api/teams" ) const HelixBaseUrl = "https://api.twitch.tv/helix" @@ -60,6 +61,7 @@ type API struct { Search *search.Search Streams *streams.Streams Subscriptions *subscriptions.Subscriptions + Teams *teams.Teams } func New(client *http.Client, baseUrl *url.URL) *API { @@ -91,6 +93,7 @@ func New(client *http.Client, baseUrl *url.URL) *API { Search: search.New(client, baseUrl), Streams: streams.New(client, baseUrl), Subscriptions: subscriptions.New(client, baseUrl), + Teams: teams.New(client, baseUrl), } } diff --git a/api/teams/get_channel_teams.go b/api/teams/get_channel_teams.go new file mode 100644 index 0000000..928c5bd --- /dev/null +++ b/api/teams/get_channel_teams.go @@ -0,0 +1,78 @@ +package teams + +import ( + "context" + "encoding/json" + "net/http" + "net/url" + "time" +) + +type GetChannelTeamsResponse struct { + // The list of teams that the broadcaster is a member of. + // Returns an empty array if the broadcaster is not a member of a team. + Teams []ChannelTeam `json:"teams"` +} + +type ChannelTeam struct { + // The ID that identifies the broadcaster. + BroadcasterID string `json:"broadcaster_id"` + + // The broadcaster’s login name. + BroadcasterLogin string `json:"broadcaster_login"` + + // The broadcaster’s display name. + BroadcasterName string `json:"broadcaster_name"` + + // A URL to the team’s background image. + BackgroundImageURL string `json:"background_image_url"` + + // A URL to the team’s banner. + Banner string `json:"banner"` + + // The UTC date and time (in RFC3339 format) of when the team was created. + CreatedAt time.Time `json:"created_at"` + + // The UTC date and time (in RFC3339 format) of the last time the team was updated. + UpdatedAt time.Time `json:"updated_at"` + + // The team’s description. The description may contain formatting such as Markdown, HTML, newline (\n) characters, etc. + Info string `json:"info"` + + // A URL to a thumbnail image of the team’s logo. + ThumbnailURL string `json:"thumbnail_url"` + + // The team’s name. + TeamName string `json:"team_name"` + + // The team’s display name. + TeamDisplayName string `json:"team_display_name"` + + // An ID that identifies the team. + ID string `json:"id"` +} + +// Gets the list of Twitch teams that the broadcaster is a member of. +// +// Requires an app access token or user access token. +func (c *Teams) GetChannelTeams(ctx context.Context, broadcasterID string) (*GetChannelTeamsResponse, error) { + endpoint := c.baseUrl.ResolveReference(&url.URL{Path: "teams/channel", RawQuery: url.Values{"broadcaster_id": {broadcasterID}}.Encode()}) + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil) + if err != nil { + return nil, err + } + + res, err := c.client.Do(req) + if err != nil { + return nil, err + } + defer res.Body.Close() + + var data GetChannelTeamsResponse + if err := json.NewDecoder(res.Body).Decode(&data); err != nil { + return nil, err + } + + return &data, nil +} diff --git a/api/teams/get_teams.go b/api/teams/get_teams.go new file mode 100644 index 0000000..a25951e --- /dev/null +++ b/api/teams/get_teams.go @@ -0,0 +1,91 @@ +package teams + +import ( + "context" + "encoding/json" + "net/http" + "net/url" + "time" + + "github.com/google/go-querystring/query" +) + +type GetTeamsParams struct { + // The name of the team to get. This parameter and the id parameter are mutually exclusive; you must specify the team’s name or ID but not both. + Name *string `url:"name,omitempty"` + + // The ID of the team to get. This parameter and the name parameter are mutually exclusive; you must specify the team’s name or ID but not both. + ID *string `url:"id,omitempty"` +} + +type GetTeamsResponse struct { + // A list that contains the single team that you requested. + Teams []Team `json:"teams"` +} + +type Team struct { + // The list of team members. + Users []struct { + // An ID that identifies the team member. + UserID string `json:"user_id"` + + // The team member’s login name. + UserLogin string `json:"user_login"` + + // The team member’s display name. + UserName string `json:"user_name"` + } `json:"users"` + + // A URL to the team’s background image. + BackgroundImageURL string `json:"background_image_url"` + + // A URL to the team’s banner. + Banner string `json:"banner"` + + // The UTC date and time (in RFC3339 format) of when the team was created. + CreatedAt time.Time `json:"created_at"` + + // The UTC date and time (in RFC3339 format) of the last time the team was updated. + UpdatedAt time.Time `json:"updated_at"` + + // The team’s description. The description may contain formatting such as Markdown, HTML, newline (\n) characters, etc. + Info string `json:"info"` + + // A URL to a thumbnail image of the team’s logo. + ThumbnailURL string `json:"thumbnail_url"` + + // The team’s name. + TeamName string `json:"team_name"` + + // The team’s display name. + TeamDisplayName string `json:"team_display_name"` + + // An ID that identifies the team. + ID string `json:"id"` +} + +// Gets information about the specified Twitch team. Read More +// +// Requires an app access token or user access token. +func (c *Teams) GetTeams(ctx context.Context, params *GetTeamsParams) (*GetTeamsResponse, error) { + v, _ := query.Values(params) + endpoint := c.baseUrl.ResolveReference(&url.URL{Path: "teams", RawQuery: v.Encode()}) + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil) + if err != nil { + return nil, err + } + + resp, err := c.client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + var data GetTeamsResponse + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return nil, err + } + + return &data, nil +} diff --git a/api/teams/teams.go b/api/teams/teams.go new file mode 100644 index 0000000..24c2d7f --- /dev/null +++ b/api/teams/teams.go @@ -0,0 +1,18 @@ +package teams + +import ( + "net/http" + "net/url" +) + +type Teams struct { + client *http.Client + baseUrl *url.URL +} + +func New(client *http.Client, baseUrl *url.URL) *Teams { + return &Teams{ + client: client, + baseUrl: baseUrl, + } +}