go-twitch/api/channels/get_channel_editors.go

59 lines
1.5 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 channels
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
"go.fifitido.net/twitch/api/endpoint"
)
type GetChannelEditorsResponse struct {
// A list of users that are editors for the specified broadcaster. The list is empty if the broadcaster doesnt have editors.
Data []ChannelEditor `json:"data"`
}
type ChannelEditor struct {
// An ID that uniquely identifies a user with editor permissions.
UserID string `json:"user_id"`
// The users display name.
UserName string `json:"user_name"`
// The date and time, in RFC3339 format, when the user became one of the broadcasters editors.
CreatedAt time.Time `json:"created_at"`
}
// Gets the broadcasters list editors.
//
// Requires a user access token that includes the channel:read:editors scope.
func (c *Channels) GetChannelEditors(ctx context.Context, broadcasterID string) (*GetChannelEditorsResponse, error) {
v := url.Values{"broadcaster_id": {broadcasterID}}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(c.baseUrl, "channels/editors", v), 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 editors (%d)", res.StatusCode)
}
var data GetChannelEditorsResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}