97 lines
2.6 KiB
Go
97 lines
2.6 KiB
Go
package teams
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"net/http"
|
||
"time"
|
||
|
||
"github.com/google/go-querystring/query"
|
||
"go.fifitido.net/twitch/api/endpoint"
|
||
)
|
||
|
||
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 (t *Teams) GetTeams(ctx context.Context, params *GetTeamsParams) (*GetTeamsResponse, error) {
|
||
v, _ := query.Values(params)
|
||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(t.baseUrl, "teams", v), nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
resp, err := t.client.Do(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer resp.Body.Close()
|
||
|
||
statusOK := resp.StatusCode >= 200 && resp.StatusCode < 300
|
||
if !statusOK {
|
||
return nil, fmt.Errorf("failed to get teams (%d)", resp.StatusCode)
|
||
}
|
||
|
||
var data GetTeamsResponse
|
||
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &data, nil
|
||
}
|