2024-02-28 13:50:07 -05:00
|
|
|
|
package conduit
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2024-03-07 19:41:05 -05:00
|
|
|
|
"fmt"
|
2024-02-28 13:50:07 -05:00
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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 client’s conduits.
|
|
|
|
|
Data []ConduitData `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Updates a conduit’s 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) {
|
|
|
|
|
endpoint := c.baseUrl.ResolveReference(&url.URL{Path: "eventsub/conduits"})
|
|
|
|
|
|
|
|
|
|
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.String(), r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := c.client.Do(req)
|
|
|
|
|
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 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
|
|
|
|
|
}
|