go-twitch/api/schedule/create_channel_stream_sched...

83 lines
2.6 KiB
Go
Raw Normal View History

2024-03-03 16:30:10 -05:00
package schedule
import (
"context"
"encoding/json"
"fmt"
2024-03-03 16:30:10 -05:00
"io"
"net/http"
"net/url"
"time"
"go.fifitido.net/twitch/api/endpoint"
2024-03-03 16:30:10 -05:00
)
type CreateChannelStreamScheduleSegmentRequest struct {
// The date and time that the broadcast segment starts. Specify the date and time in RFC3339 format (for example, 2021-07-01T18:00:00Z).
StartTime time.Time `json:"start_time"`
// The time zone where the broadcast takes place. Specify the time zone using IANA time zone database format (for example, America/New_York).
Timezone string `json:"timezone"`
// The length of time, in minutes, that the broadcast is scheduled to run. The duration must be in the range 30 through 1380 (23 hours).
Duration int `json:"duration"`
// A Boolean value that determines whether the broadcast recurs weekly. Is true if the broadcast recurs weekly.
// Only partners and affiliates may add non-recurring broadcasts.
IsRecurring *bool `json:"is_recurring,omitempty"`
// The ID of the category that best represents the broadcasts content.
// To get the category ID, use the Search Categories endpoint.
CategoryId *string `json:"category_id"`
// The broadcasts title. The title may contain a maximum of 140 characters.
Title *string `json:"title"`
}
type CreateChannelStreamScheduleSegmentResponse struct {
// The broadcasters streaming schedule.
Data ChannelStreamSchedule `json:"data"`
}
// Adds a single or recurring broadcast to the broadcasters streaming schedule. For information about scheduling broadcasts, see Stream Schedule.
//
// Requires a user access token that includes the channel:manage:schedule scope.
//
// The broadcaster ID must match the user ID in the user access token.
func (s *Schedule) CreateChannelStreamScheduleSegment(ctx context.Context, broadcasterId string, body *CreateChannelStreamScheduleSegmentRequest) (*CreateChannelStreamScheduleSegmentResponse, error) {
v := url.Values{"broadcaster_id": {broadcasterId}}
2024-03-03 16:30:10 -05:00
r, w := io.Pipe()
go func() {
if err := json.NewEncoder(w).Encode(body); err != nil {
w.CloseWithError(err)
} else {
w.Close()
}
}()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.Make(s.baseUrl, "schedule/segment", v), r)
2024-03-03 16:30:10 -05:00
if err != nil {
return nil, err
}
res, err := s.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 create channel stream schedule segment (%d)", res.StatusCode)
}
2024-03-03 16:30:10 -05:00
var data CreateChannelStreamScheduleSegmentResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}