2024-02-27 22:13:57 -05:00
|
|
|
|
package ads
|
|
|
|
|
|
|
|
|
|
import (
|
2024-02-27 23:03:40 -05:00
|
|
|
|
"context"
|
2024-02-27 22:13:57 -05:00
|
|
|
|
"encoding/json"
|
2024-03-07 19:41:05 -05:00
|
|
|
|
"fmt"
|
2024-02-27 23:03:40 -05:00
|
|
|
|
"net/http"
|
2024-02-27 22:13:57 -05:00
|
|
|
|
"net/url"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SnoozeNextAdResponse struct {
|
|
|
|
|
// A list that contains information about the channel’s snoozes and next upcoming ad after successfully snoozing.
|
|
|
|
|
Data []SnoozeNextAdData `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SnoozeNextAdData 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 broadcaster’s next scheduled ad, in RFC3339 format.
|
|
|
|
|
NextAdAt time.Time `json:"next_ad_at"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If available, pushes back the timestamp of the upcoming automatic mid-roll ad by 5 minutes.
|
|
|
|
|
// This endpoint duplicates the snooze functionality in the creator dashboard’s Ads Manager.
|
|
|
|
|
//
|
|
|
|
|
// Requires a user access token that includes the channel:manage:ads scope.
|
|
|
|
|
// The user_id in the user access token must match the broadcaster_id.
|
2024-02-27 23:03:40 -05:00
|
|
|
|
func (e *Ads) SnoozeNextAd(ctx context.Context, broadcasterID string) (*SnoozeNextAdResponse, error) {
|
2024-02-27 22:13:57 -05:00
|
|
|
|
endpoint := e.baseUrl.ResolveReference(&url.URL{Path: "channels/ads/schedule/snooze", RawQuery: "broadcaster_id=" + broadcasterID})
|
|
|
|
|
|
2024-02-27 23:03:40 -05:00
|
|
|
|
req, err := http.NewRequest(http.MethodPost, endpoint.String(), nil)
|
2024-02-27 22:13:57 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 23:03:40 -05:00
|
|
|
|
res, err := e.client.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer res.Body.Close()
|
2024-02-27 22:13:57 -05:00
|
|
|
|
|
2024-03-07 19:41:05 -05:00
|
|
|
|
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
|
|
|
|
|
if !statusOK {
|
|
|
|
|
return nil, fmt.Errorf("failed to snooze next ad (%d)", res.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 22:13:57 -05:00
|
|
|
|
var data SnoozeNextAdResponse
|
2024-02-27 23:03:40 -05:00
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
2024-02-27 22:13:57 -05:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &data, nil
|
|
|
|
|
}
|