72 lines
2.3 KiB
Go
72 lines
2.3 KiB
Go
package predictions
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"net/http"
|
||
|
||
"github.com/google/go-querystring/query"
|
||
"go.fifitido.net/twitch/api/endpoint"
|
||
)
|
||
|
||
type EndPredictionRequest struct {
|
||
// The ID of the broadcaster that’s 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) {
|
||
v, _ := query.Values(params)
|
||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, endpoint.Make(p.baseUrl, "predictions", v), nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
res, err := p.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 end prediction (%d)", res.StatusCode)
|
||
}
|
||
|
||
var data EndPredictionResponse
|
||
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &data, nil
|
||
}
|