go-twitch/api/predictions/end_prediction.go

72 lines
2.3 KiB
Go
Raw Permalink Normal View History

2024-03-03 15:50:12 -05:00
package predictions
import (
"context"
"encoding/json"
"fmt"
2024-03-03 15:50:12 -05:00
"net/http"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/endpoint"
2024-03-03 15:50:12 -05:00
)
type EndPredictionRequest struct {
// The ID of the broadcaster thats running the prediction. This ID must match the user ID in the user access token.
BroadcasterID string `json:"broadcaster_id"`
// The ID of the prediction to update.
ID string `json:"id"`
// The status to set the prediction to. Possible case-sensitive values are:
//
// RESOLVED — The winning outcome is determined and the Channel Points are distributed to the viewers who predicted the correct outcome.
//
// CANCELED — The broadcaster is canceling the prediction and sending refunds to the participants.
//
// LOCKED — The broadcaster is locking the prediction, which means viewers may no longer make predictions.
//
// The broadcaster can update an active prediction to LOCKED, RESOLVED, or CANCELED; and update a locked prediction to RESOLVED or CANCELED.
//
// The broadcaster has up to 24 hours after the prediction window closes to resolve the prediction.
// If not, Twitch sets the status to CANCELED and returns the points.
Status Status `json:"status"`
// The ID of the winning outcome. You must set this parameter if you set status to RESOLVED.
WinningOutcomeID *string `json:"winning_outcome_id"`
}
type EndPredictionResponse struct {
// A list that contains the single prediction that you updated.
Data []Prediction `json:"data"`
}
// Locks, resolves, or cancels a Channel Points Prediction.
//
// Requires a user access token that includes the channel:manage:predictions scope.
func (p *Predictions) EndPrediction(ctx context.Context, params *EndPredictionRequest) (*EndPredictionResponse, error) {
2024-03-03 15:50:12 -05:00
v, _ := query.Values(params)
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, endpoint.Make(p.baseUrl, "predictions", v), nil)
2024-03-03 15:50:12 -05:00
if err != nil {
return nil, err
}
res, err := p.client.Do(req)
2024-03-03 15:50:12 -05:00
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 end prediction (%d)", res.StatusCode)
}
2024-03-03 15:50:12 -05:00
var data EndPredictionResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}