go-twitch/api/gueststar/create_gueststar_session.go

48 lines
1.3 KiB
Go
Raw Normal View History

2024-03-03 14:09:31 -05:00
package gueststar
import (
"context"
"encoding/json"
"fmt"
2024-03-03 14:09:31 -05:00
"net/http"
"net/url"
"go.fifitido.net/twitch/api/endpoint"
2024-03-03 14:09:31 -05:00
)
type CreateGuestStarSessionResponse struct {
// Summary of the session details.
Data []Session `json:"data"`
}
// Programmatically creates a Guest Star session on behalf of the broadcaster. Requires the broadcaster to be present in the call interface, or the call will be ended automatically.
//
// Query parameter broadcaster_id must match the user_id in the User-Access token
// Requires OAuth Scope: channel:manage:guest_star
func (g *GuestStar) CreateGuestStarSession(ctx context.Context, broadcasterID string) (*CreateGuestStarSessionResponse, error) {
v := url.Values{"broadcaster_id": {broadcasterID}}
2024-03-03 14:09:31 -05:00
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.Make(g.baseUrl, "guest_star/session", v), nil)
2024-03-03 14:09:31 -05:00
if err != nil {
return nil, err
}
res, err := g.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 guest star session (%d)", res.StatusCode)
}
2024-03-03 14:09:31 -05:00
var data CreateGuestStarSessionResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}