go-twitch/api/teams/get_channel_teams.go

85 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package teams
import (
"context"
"encoding/json"
"fmt"
"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 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 (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()
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
return nil, fmt.Errorf("failed to get channel teams (%d)", res.StatusCode)
}
var data GetChannelTeamsResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}