2024-03-02 22:45:59 -05:00
package extensions
import (
"context"
"encoding/json"
2024-03-07 19:41:05 -05:00
"fmt"
2024-03-02 22:45:59 -05:00
"net/http"
"github.com/google/go-querystring/query"
2024-03-07 20:52:42 -05:00
"go.fifitido.net/twitch/api/endpoint"
2024-03-02 22:45:59 -05:00
"go.fifitido.net/twitch/api/types"
)
type GetExtensionLiveChannelsParams struct {
// The ID of the extension to get. Returns the list of broadcasters that are live and that have installed or activated this extension.
ExtensionID string ` url:"extension_id" `
// The maximum number of items to return per page in the response. The minimum page size is 1 item per page and the maximum is 100 items per page. The default is 20.
First * int ` url:"first,omitempty" `
// The cursor used to get the next page of results. The pagination field in the response contains the cursor’ s value.
// Read More: https://dev.twitch.tv/docs/api/guide#pagination
After * types . Cursor ` url:"after,omitempty" `
}
type GetExtensionLiveChannelsResponse struct {
// The list of broadcasters that are streaming live and that have installed or activated the extension.
Data [ ] ExtensionLiveChannel ` json:"data" `
// The cursor used to get the next page of results. The pagination field in the response contains the cursor’ s value.
// Read More: https://dev.twitch.tv/docs/api/guide#pagination
Pagination types . Pagination ` json:"pagination" `
}
type ExtensionLiveChannel struct {
// The ID of the broadcaster that is streaming live and has installed or activated the extension.
BroadcasterID string ` json:"broadcaster_id" `
// The broadcaster’ s display name.
BroadcasterName string ` json:"broadcaster_name" `
// The name of the category or game being streamed.
GameName string ` json:"game_name" `
// The ID of the category or game being streamed.
GameID string ` json:"game_id" `
// The title of the broadcaster’ s stream. May be an empty string if not specified.
Title string ` json:"title" `
}
// Gets a list of broadcasters that are streaming live and have installed or activated the extension.
//
// It may take a few minutes for the list to include or remove broadcasters that have recently gone live or stopped broadcasting.
//
// Requires an app access token or user access token.
2024-03-07 20:52:42 -05:00
func ( e * Extensions ) GetExtensionLiveChannels ( ctx context . Context , params * GetExtensionLiveChannelsParams ) ( * GetExtensionLiveChannelsResponse , error ) {
2024-03-02 22:45:59 -05:00
v , _ := query . Values ( params )
2024-03-07 20:52:42 -05:00
req , err := http . NewRequestWithContext ( ctx , http . MethodGet , endpoint . Make ( e . baseUrl , "extensions/live" , v ) , nil )
2024-03-02 22:45:59 -05:00
if err != nil {
return nil , err
}
2024-03-07 20:52:42 -05:00
res , err := e . client . Do ( req )
2024-03-02 22:45:59 -05:00
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 extension live channels (%d)" , res . StatusCode )
}
2024-03-02 22:45:59 -05:00
var response GetExtensionLiveChannelsResponse
if err := json . NewDecoder ( res . Body ) . Decode ( & response ) ; err != nil {
return nil , err
}
return & response , nil
}