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"
|
2024-03-05 12:33:34 -05:00
|
|
|
|
|
2024-03-07 20:52:42 -05:00
|
|
|
|
"go.fifitido.net/twitch/api/endpoint"
|
2024-03-05 12:33:34 -05:00
|
|
|
|
"go.fifitido.net/twitch/api/eventsub"
|
2024-02-28 13:50:07 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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.
|
2024-03-05 12:33:34 -05:00
|
|
|
|
//
|
|
|
|
|
// Does not support conduit transport type.
|
|
|
|
|
Transport *eventsub.Transport `json:"transport"`
|
2024-02-28 13:50:07 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2024-03-07 20:52:42 -05:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, endpoint.Make(c.baseUrl, "eventsub/conduits/shards"), 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()
|
|
|
|
|
|
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 shards (%d)", res.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-28 13:50:07 -05:00
|
|
|
|
var data UpdateConduitShardsResponse
|
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &data, nil
|
|
|
|
|
}
|