go-twitch/api/conduit/get_conduit_shards.go

63 lines
1.6 KiB
Go
Raw Normal View History

2024-02-28 13:50:07 -05:00
package conduit
import (
"context"
"encoding/json"
"fmt"
2024-02-28 13:50:07 -05:00
"net/http"
"net/url"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/types"
)
type GetConduitShardsParams struct {
// Conduit ID.
ConduitID string `url:"conduit_id"`
// Status to filter by.
Status *Status `url:"status,omitempty"`
// The cursor used to get the next page of results. The pagination object in the response contains the cursors value.
Cursor *types.Cursor `url:"cursor,omitempty"`
}
type GetConduitShardsResponse struct {
// List of information about a conduit's shards.
Data []Shard `json:"data"`
// The cursor used to get the next page of results. Use the cursor to set the requests after query parameter.
Pagination types.Pagination `json:"pagination"`
}
// Gets a lists of all shards for a conduit.
//
// Requires an app access token.
func (c *Conduit) GetConduitShards(ctx context.Context, params *GetConduitShardsParams) (*GetConduitShardsResponse, error) {
v, _ := query.Values(params)
endpoint := c.baseUrl.ResolveReference(&url.URL{Path: "eventsub/conduits/shards", RawQuery: v.Encode()})
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil)
if err != nil {
return nil, err
}
res, err := c.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 conduit shards (%d)", res.StatusCode)
}
2024-02-28 13:50:07 -05:00
var data GetConduitShardsResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}