2024-03-03 17:34:18 -05:00
|
|
|
|
package streams
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2024-03-07 19:41:05 -05:00
|
|
|
|
"fmt"
|
2024-03-03 17:34:18 -05:00
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
2024-03-07 20:52:42 -05:00
|
|
|
|
|
|
|
|
|
"go.fifitido.net/twitch/api/endpoint"
|
2024-03-03 17:34:18 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type GetStreamKeyResponse struct {
|
|
|
|
|
// A list that contains the channel’s stream key.
|
|
|
|
|
Data []struct {
|
|
|
|
|
// The channel’s stream key.
|
|
|
|
|
StreamKey string `json:"stream_key"`
|
|
|
|
|
} `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Gets the channel’s 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) {
|
2024-03-07 20:52:42 -05:00
|
|
|
|
v := url.Values{"broadcaster_id": {broadcasterID}}
|
2024-03-03 17:34:18 -05:00
|
|
|
|
|
2024-03-07 20:52:42 -05:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(s.baseUrl, "streams/key", v), nil)
|
2024-03-03 17:34:18 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := s.client.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
2024-03-07 19:41:05 -05:00
|
|
|
|
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
|
|
|
|
|
if !statusOK {
|
|
|
|
|
return nil, fmt.Errorf("failed to get stream key (%d)", res.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-03 17:34:18 -05:00
|
|
|
|
var data GetStreamKeyResponse
|
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &data, nil
|
|
|
|
|
}
|