go-twitch/api/schedule/update_channel_stream_sched...

97 lines
3.3 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"
"github.com/google/go-querystring/query"
)
type UpdateChannelStreamScheduleSegmentParams struct {
// The ID of the broadcaster who owns the broadcast segment to update. This ID must match the user ID in the user access token.
BroadcasterId string `url:"broadcaster_id"`
// The ID of the broadcast segment to update.
Id string `url:"id"`
}
type UpdateChannelStreamScheduleSegmentRequest 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).
//
// NOTE: Only partners and affiliates may update a broadcasts start time and only for non-recurring segments.
StartTime *time.Time `json:"start_time"`
// 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"`
// 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"`
// A Boolean value that indicates whether the broadcast is canceled. Set to true to cancel the segment.
//
// NOTE: For recurring segments, the API cancels the first segment after the current UTC date and time and not the specified segment
// (unless the specified segment is the next segment after the current UTC date and time).
IsCanceled *bool `json:"is_canceled"`
// The time zone where the broadcast takes place. Specify the time zone using IANA time zone database format (for example, America/New_York).
Timezone *time.Location `json:"timezone"`
}
type UpdateChannelStreamScheduleSegmentResponse struct {
// The broadcasters streaming schedule.
Data ChannelStreamSchedule `json:"data"`
}
// Updates a scheduled broadcast segment.
//
// For recurring segments, updating a segments title, category, duration, and timezone,
// changes all segments in the recurring schedule, not just the specified segment.
//
// Requires a user access token that includes the channel:manage:schedule scope.
func (s *Schedule) UpdateChannelStreamScheduleSegment(ctx context.Context, params *UpdateChannelStreamScheduleSegmentParams, body *UpdateChannelStreamScheduleSegmentRequest) (*UpdateChannelStreamScheduleSegmentResponse, error) {
v, _ := query.Values(params)
endpoint := s.baseUrl.ResolveReference(&url.URL{Path: "schedule/segment", RawQuery: v.Encode()})
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.MethodPatch, endpoint.String(), r)
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 update channel stream schedule segment (%d)", res.StatusCode)
}
2024-03-03 16:30:10 -05:00
var data UpdateChannelStreamScheduleSegmentResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}