go-twitch/api/streams/create_stream_marker.go

76 lines
2.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package streams
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"go.fifitido.net/twitch/api/endpoint"
)
type CreateStreamMarkerRequest struct {
// The ID of the broadcaster thats streaming content.
// This ID must match the user ID in the access token or the user in the access token must be one of the broadcasters editors.
UserID string `json:"user_id"`
// A short description of the marker to help the user remember why they marked the location.
// The maximum length of the description is 140 characters.
Description string `json:"description"`
}
type CreateStreamMarkerResponse struct {
// A list that contains the single marker that you added.
Data []StreamMarker `json:"data"`
}
// Adds a marker to a live stream. A marker is an arbitrary point in a live stream that the broadcaster or editor wants to mark,
// so they can return to that spot later to create video highlights (see Video Producer, Highlights in the Twitch UX).
//
// You may not add markers:
//
// - If the stream is not live
//
// - If the stream has not enabled video on demand (VOD)
//
// - If the stream is a premiere (a live, first-viewing event that combines uploaded videos with live chat)
//
// - If the stream is a rerun of a past broadcast, including past premieres.
//
// Requires a user access token that includes the channel:manage:broadcast scope.
func (s *Streams) CreateStreamMarker(ctx context.Context, body *CreateStreamMarkerRequest) (*CreateStreamMarkerResponse, error) {
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, "streams/markers"), 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 create stream marker (%d)", res.StatusCode)
}
var data CreateStreamMarkerResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}