go-twitch/api/ads/get_ad_schedule.go

68 lines
2.3 KiB
Go
Raw Normal View History

2024-02-27 22:13:57 -05:00
package ads
import (
"context"
2024-02-27 22:13:57 -05:00
"encoding/json"
"fmt"
"net/http"
2024-02-27 22:13:57 -05:00
"net/url"
"time"
)
type GetAdScheduleResponse struct {
// A list that contains information related to the channels ad schedule.
Data []GetAdScheduleData `json:"data"`
}
type GetAdScheduleData struct {
// The number of snoozes available for the broadcaster.
SnoozeCount int `json:"snooze_count"`
// The UTC timestamp when the broadcaster will gain an additional snooze, in RFC3339 format.
SnoozeRefreshAt time.Time `json:"snooze_refresh_at"`
// The UTC timestamp of the broadcasters next scheduled ad, in RFC3339 format. Empty if the channel has no ad scheduled or is not live.
NextAdAt time.Time `json:"next_ad_at"`
// The length in seconds of the scheduled upcoming ad break.
Duration int `json:"duration"`
// The UTC timestamp of the broadcasters last ad-break, in RFC3339 format. Empty if the channel has not run an ad or is not live.
LastAdAt time.Time `json:"last_ad_at"`
// The amount of pre-roll free time remaining for the channel in seconds. Returns 0 if they are currently not pre-roll free.
PrerollFreeTime int `json:"preroll_free_time"`
}
// This endpoint returns ad schedule related information, including snooze, when the last ad was run, when the next ad is scheduled,
// and if the channel is currently in pre-roll free time. Note that a new ad cannot be run until 8 minutes after running a previous ad.
//
// Requires a user access token that includes the channel:read:ads scope.
// The user_id in the user access token must match the broadcaster_id.
func (e *Ads) GetAdSchedule(ctx context.Context, broadcasterID string) (*GetAdScheduleResponse, error) {
2024-02-27 22:13:57 -05:00
endpoint := e.baseUrl.ResolveReference(&url.URL{Path: "channels/ads", RawQuery: "broadcaster_id=" + broadcasterID})
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil)
2024-02-27 22:13:57 -05:00
if err != nil {
return nil, err
}
res, err := e.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
2024-02-27 22:13:57 -05:00
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
return nil, fmt.Errorf("failed to get ad schedule (%d)", res.StatusCode)
}
2024-02-27 22:13:57 -05:00
var data GetAdScheduleResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
2024-02-27 22:13:57 -05:00
return nil, err
}
return &data, nil
}