go-twitch/api/ads/get_ad_schedule.go

68 lines
2.3 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 ads
import (
"context"
"encoding/json"
"fmt"
"net/http"
"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) {
endpoint := e.baseUrl.ResolveReference(&url.URL{Path: "channels/ads", RawQuery: "broadcaster_id=" + broadcasterID})
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil)
if err != nil {
return nil, err
}
res, err := e.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 ad schedule (%d)", res.StatusCode)
}
var data GetAdScheduleResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}