go-twitch/api/api.go

137 lines
4.2 KiB
Go

package api
import (
"net/http"
"net/url"
"go.fifitido.net/twitch/api/ads"
"go.fifitido.net/twitch/api/analytics"
"go.fifitido.net/twitch/api/bits"
"go.fifitido.net/twitch/api/ccls"
"go.fifitido.net/twitch/api/channelpoints"
"go.fifitido.net/twitch/api/channels"
"go.fifitido.net/twitch/api/charity"
"go.fifitido.net/twitch/api/chat"
"go.fifitido.net/twitch/api/conduit"
"go.fifitido.net/twitch/api/entitlements"
"go.fifitido.net/twitch/api/eventsub"
"go.fifitido.net/twitch/api/extensions"
"go.fifitido.net/twitch/api/games"
"go.fifitido.net/twitch/api/goals"
"go.fifitido.net/twitch/api/gueststar"
"go.fifitido.net/twitch/api/hypetrain"
"go.fifitido.net/twitch/api/moderation"
"go.fifitido.net/twitch/api/polls"
"go.fifitido.net/twitch/api/predictions"
"go.fifitido.net/twitch/api/raids"
"go.fifitido.net/twitch/api/schedule"
"go.fifitido.net/twitch/api/search"
"go.fifitido.net/twitch/api/streams"
"go.fifitido.net/twitch/api/subscriptions"
"go.fifitido.net/twitch/api/teams"
"go.fifitido.net/twitch/api/users"
"go.fifitido.net/twitch/api/videos"
"go.fifitido.net/twitch/api/whispers"
"go.fifitido.net/twitch/auth"
"golang.org/x/oauth2"
)
const HelixBaseUrl = "https://api.twitch.tv/helix"
type API struct {
client *http.Client
baseUrl *url.URL
Auth *auth.Auth
Ads *ads.Ads
Analytics *analytics.Analytics
Bits *bits.Bits
Channels *channels.Channels
ChannelPoints *channelpoints.ChannelPoints
Charity *charity.Charity
Chat *chat.Chat
Conduit *conduit.Conduit
CCLS *ccls.CCLS
Entitlements *entitlements.Entitlements
Extensions *extensions.Extensions
EventSub *eventsub.EventSub
Games *games.Games
Goals *goals.Goals
GuestStar *gueststar.GuestStar
Hypetrain *hypetrain.Hypetrain
Moderation *moderation.Moderation
Polls *polls.Polls
Predictions *predictions.Predictions
Raids *raids.Raids
Schedule *schedule.Schedule
Search *search.Search
Streams *streams.Streams
Subscriptions *subscriptions.Subscriptions
Teams *teams.Teams
Users *users.Users
Videos *videos.Videos
Whispers *whispers.Whispers
}
func New(client *http.Client, baseUrl *url.URL, authClient *auth.Auth) *API {
return &API{
client: client,
baseUrl: baseUrl,
Auth: authClient,
Ads: ads.New(client, baseUrl),
Analytics: analytics.New(client, baseUrl),
Bits: bits.New(client, baseUrl),
Channels: channels.New(client, baseUrl),
ChannelPoints: channelpoints.New(client, baseUrl),
Charity: charity.New(client, baseUrl),
Chat: chat.New(client, baseUrl),
Conduit: conduit.New(client, baseUrl),
CCLS: ccls.New(client, baseUrl),
Entitlements: entitlements.New(client, baseUrl),
Extensions: extensions.New(client, baseUrl),
EventSub: eventsub.New(client, baseUrl),
Games: games.New(client, baseUrl),
Goals: goals.New(client, baseUrl),
GuestStar: gueststar.New(client, baseUrl),
Hypetrain: hypetrain.New(client, baseUrl),
Moderation: moderation.New(client, baseUrl),
Polls: polls.New(client, baseUrl),
Predictions: predictions.New(client, baseUrl),
Raids: raids.New(client, baseUrl),
Schedule: schedule.New(client, baseUrl),
Search: search.New(client, baseUrl),
Streams: streams.New(client, baseUrl),
Subscriptions: subscriptions.New(client, baseUrl),
Teams: teams.New(client, baseUrl),
Users: users.New(client, baseUrl),
Videos: videos.New(client, baseUrl),
Whispers: whispers.New(client, baseUrl),
}
}
func NewDefault(clientId, clientSecret, redirectUri string) *API {
client := &http.Client{
Transport: &apiTransport{
clientId: clientId,
},
}
baseUrl, _ := url.Parse(HelixBaseUrl)
authClient := auth.NewWithClient(clientId, clientSecret, redirectUri, client)
return New(client, baseUrl, authClient)
}
func (a *API) WithClient(client *http.Client) *API {
return New(client, a.baseUrl, a.Auth)
}
func (a *API) WithAuthToken(token *auth.Token) *API {
return a.WithClient(&http.Client{
Transport: &oauth2.Transport{
Source: a.Auth.TokenSource(token),
Base: a.client.Transport,
},
})
}