85 lines
2.3 KiB
Go
85 lines
2.3 KiB
Go
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 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()
|
||
|
||
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
|
||
}
|