go-twitch/api/teams/get_channel_teams.go

87 lines
2.3 KiB
Go
Raw Permalink Normal View History

2024-03-03 18:00:22 -05:00
package teams
import (
"context"
"encoding/json"
"fmt"
2024-03-03 18:00:22 -05:00
"net/http"
"net/url"
"time"
"go.fifitido.net/twitch/api/endpoint"
2024-03-03 18:00:22 -05:00
)
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 broadcasters login name.
BroadcasterLogin string `json:"broadcaster_login"`
// The broadcasters display name.
BroadcasterName string `json:"broadcaster_name"`
// A URL to the teams background image.
BackgroundImageURL string `json:"background_image_url"`
// A URL to the teams 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 teams 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 teams logo.
ThumbnailURL string `json:"thumbnail_url"`
// The teams name.
TeamName string `json:"team_name"`
// The teams 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 (t *Teams) GetChannelTeams(ctx context.Context, broadcasterID string) (*GetChannelTeamsResponse, error) {
v := url.Values{"broadcaster_id": {broadcasterID}}
2024-03-03 18:00:22 -05:00
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(t.baseUrl, "teams/channel", v), nil)
2024-03-03 18:00:22 -05:00
if err != nil {
return nil, err
}
res, err := t.client.Do(req)
2024-03-03 18:00:22 -05:00
if err != nil {
return nil, err
}
defer res.Body.Close()
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
return nil, fmt.Errorf("failed to get channel teams (%d)", res.StatusCode)
}
2024-03-03 18:00:22 -05:00
var data GetChannelTeamsResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}