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 22:13:57 -05:00
|
|
|
|
"io"
|
2024-02-27 23:03:40 -05:00
|
|
|
|
"net/http"
|
2024-03-07 20:52:42 -05:00
|
|
|
|
|
|
|
|
|
"go.fifitido.net/twitch/api/endpoint"
|
2024-02-27 22:13:57 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type StartCommercialRequest struct {
|
|
|
|
|
// The ID of the partner or affiliate broadcaster that wants to run the commercial. This ID must match the user ID found in the OAuth token.
|
|
|
|
|
BroadcasterID string `json:"broadcaster_id"`
|
|
|
|
|
|
|
|
|
|
// The length of the commercial to run, in seconds.
|
|
|
|
|
// Twitch tries to serve a commercial that’s the requested length, but it may be shorter or longer.
|
|
|
|
|
// The maximum length you should request is 180 seconds.
|
|
|
|
|
Duration int `json:"duration"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type StartCommercialResponse struct {
|
|
|
|
|
// An array that contains a single object with the status of your start commercial request.
|
|
|
|
|
Data []StartCommercialData `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type StartCommercialData struct {
|
|
|
|
|
// The length of the commercial you requested. If you request a commercial that’s longer than 180 seconds, the API uses 180 seconds.
|
|
|
|
|
Length int `json:"length"`
|
|
|
|
|
|
|
|
|
|
// A message that indicates whether Twitch was able to serve an ad.
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
|
|
|
|
|
// The number of seconds you must wait before running another commercial.
|
|
|
|
|
RetryAfter int `json:"retry_after"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Starts a commercial on the specified channel.
|
|
|
|
|
//
|
|
|
|
|
// NOTE: Only partners and affiliates may run commercials and they must be streaming live at the time.
|
|
|
|
|
//
|
|
|
|
|
// NOTE: Only the broadcaster may start a commercial; the broadcaster’s editors and moderators may not start commercials on behalf of the broadcaster.
|
|
|
|
|
//
|
|
|
|
|
// Requires a user access token that includes the channel:edit:commercial scope.
|
2024-03-07 20:52:42 -05:00
|
|
|
|
func (a *Ads) StartCommercial(ctx context.Context, body *StartCommercialRequest) (*StartCommercialResponse, error) {
|
2024-02-27 22:13:57 -05:00
|
|
|
|
|
|
|
|
|
r, w := io.Pipe()
|
|
|
|
|
|
|
|
|
|
go func() {
|
2024-02-27 23:03:40 -05:00
|
|
|
|
if err := json.NewEncoder(w).Encode(body); err != nil {
|
2024-02-27 22:13:57 -05:00
|
|
|
|
w.CloseWithError(err)
|
|
|
|
|
} else {
|
|
|
|
|
w.Close()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2024-03-07 20:52:42 -05:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.Make(a.baseUrl, "channels/commercial"), r)
|
2024-02-27 22:13:57 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-07 20:52:42 -05:00
|
|
|
|
res, err := a.client.Do(req)
|
2024-02-27 23:03:40 -05:00
|
|
|
|
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 start commercial (%d)", res.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 22:13:57 -05:00
|
|
|
|
var data StartCommercialResponse
|
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
|
|
|
|
|
}
|