go-twitch/api/conduit/update_conduits.go

64 lines
1.5 KiB
Go
Raw Permalink 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
"io"
"net/http"
"go.fifitido.net/twitch/api/endpoint"
2024-02-28 13:50:07 -05:00
)
type UpdateConduitsRequest struct {
// Conduit ID.
ID string `json:"id"`
// The new number of shards for this conduit.
ShardCount int `json:"shard_count"`
}
type UpdateConduitsResponse struct {
// List of information about the clients conduits.
Data []ConduitData `json:"data"`
}
// Updates a conduits shard count. To delete shards, update the count to a lower number, and the shards above the count will be deleted.
// For example, if the existing shard count is 100, by resetting shard count to 50, shards 50-99 are disabled.
//
// Requires an app access token.
func (c *Conduit) UpdateConduits(ctx context.Context, body *UpdateConduitsRequest) (*UpdateConduitsResponse, error) {
r, w := io.Pipe()
go func() {
if err := json.NewEncoder(w).Encode(body); err != nil {
w.CloseWithError(err)
} else {
w.Close()
}
}()
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, endpoint.Make(c.baseUrl, "eventsub/conduits"), r)
2024-02-28 13:50:07 -05:00
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 update conduit (%d)", res.StatusCode)
}
2024-02-28 13:50:07 -05:00
var data UpdateConduitsResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}