105 lines
2.6 KiB
Go
105 lines
2.6 KiB
Go
package conduit
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"io"
|
||
"net/http"
|
||
|
||
"go.fifitido.net/twitch/api/endpoint"
|
||
"go.fifitido.net/twitch/api/eventsub"
|
||
)
|
||
|
||
type UpdateConduitShardsRequest struct {
|
||
// Conduit ID.
|
||
ConduitID string `json:"conduit_id"`
|
||
|
||
// List of Shards to update.
|
||
Shards []ShardUpdate `json:"shards"`
|
||
}
|
||
|
||
type ShardUpdate struct {
|
||
// Shard ID.
|
||
ID string `json:"id"`
|
||
|
||
// The transport details that you want Twitch to use when sending you notifications.
|
||
//
|
||
// Does not support conduit transport type.
|
||
Transport *eventsub.Transport `json:"transport"`
|
||
}
|
||
|
||
type UpdateConduitShardsResponse struct {
|
||
// List of successful shard updates.
|
||
Data []ConduitData `json:"data"`
|
||
|
||
// List of unsuccessful updates.
|
||
Errors []UpdateConduitShardsError `json:"errors"`
|
||
}
|
||
|
||
type UpdateConduitShardsError struct {
|
||
// Shard ID.
|
||
ID string `json:"id"`
|
||
|
||
// The error that occurred while updating the shard.
|
||
// Possible errors:
|
||
//
|
||
// The length of the string in the secret field is not valid.
|
||
//
|
||
// The URL in the transport's callback field is not valid. The URL must use the HTTPS protocol and the 443 port number.
|
||
//
|
||
// The value specified in the method field is not valid.
|
||
//
|
||
// The callback field is required if you specify the webhook transport method.
|
||
//
|
||
// The session_id field is required if you specify the WebSocket transport method.
|
||
//
|
||
// The websocket session is not connected.
|
||
//
|
||
// The shard id is outside of the conduit’s range.
|
||
Message string `json:"message"`
|
||
|
||
// Error codes used to represent a specific error condition while attempting to update shards.
|
||
Code string `json:"code"`
|
||
}
|
||
|
||
// Updates shard(s) for a conduit.
|
||
//
|
||
// NOTE: Shard IDs are indexed starting at 0, so a conduit with a shard_count of 5 will have shards with IDs 0 through 4.
|
||
//
|
||
// Requires an app access token.
|
||
func (c *Conduit) UpdateConduitShards(ctx context.Context, body *UpdateConduitShardsRequest) (*UpdateConduitShardsResponse, 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/shards"), 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 shards (%d)", res.StatusCode)
|
||
}
|
||
|
||
var data UpdateConduitShardsResponse
|
||
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &data, nil
|
||
}
|