go-twitch/api/streams/get_stream_key.go

52 lines
1.2 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"
"net/http"
"net/url"
"go.fifitido.net/twitch/api/endpoint"
)
type GetStreamKeyResponse struct {
// A list that contains the channels stream key.
Data []struct {
// The channels stream key.
StreamKey string `json:"stream_key"`
} `json:"data"`
}
// Gets the channels stream key.
//
// Requires a user access token that includes the channel:read:stream_key scope.
//
// The broadcaster ID must match the user ID in the access token.
func (s *Streams) GetStreamKey(ctx context.Context, broadcasterID string) (*GetStreamKeyResponse, error) {
v := url.Values{"broadcaster_id": {broadcasterID}}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(s.baseUrl, "streams/key", v), nil)
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 get stream key (%d)", res.StatusCode)
}
var data GetStreamKeyResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}