go-twitch/api/conduit/update_conduits.go

64 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package conduit
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"go.fifitido.net/twitch/api/endpoint"
)
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)
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)
}
var data UpdateConduitsResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}