97 lines
2.4 KiB
Go
97 lines
2.4 KiB
Go
package conduit
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"io"
|
||
"net/http"
|
||
"net/url"
|
||
)
|
||
|
||
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.
|
||
Transport *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) {
|
||
endpoint := c.baseUrl.ResolveReference(&url.URL{Path: "eventsub/conduits/shards"})
|
||
|
||
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()
|
||
|
||
var data UpdateConduitShardsResponse
|
||
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &data, nil
|
||
}
|