package schedule import ( "context" "encoding/json" "fmt" "io" "net/http" "time" "github.com/google/go-querystring/query" "go.fifitido.net/twitch/api/endpoint" ) 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 broadcast’s 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 broadcast’s content. // To get the category ID, use the Search Categories endpoint. CategoryId *string `json:"category_id"` // The broadcast’s 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 broadcaster’s streaming schedule. Data ChannelStreamSchedule `json:"data"` } // Updates a scheduled broadcast segment. // // For recurring segments, updating a segment’s 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) 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.Make(s.baseUrl, "schedule/segment", v), 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) } var data UpdateChannelStreamScheduleSegmentResponse if err := json.NewDecoder(res.Body).Decode(&data); err != nil { return nil, err } return &data, nil }