Skip to content

Commit

Permalink
Add get / create / end prediction features
Browse files Browse the repository at this point in the history
Add few fix on polls keys

End of second part of 2021‑05‑03 changelog
  • Loading branch information
Scorfly committed May 7, 2021
1 parent c260cec commit 43a817b
Show file tree
Hide file tree
Showing 7 changed files with 484 additions and 28 deletions.
6 changes: 3 additions & 3 deletions docs/polls_docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ if err != nil {
resp, err := client.CreatePoll(&helix.CreatePollParams{
BroadcasterID: "145328278",
Title: "Test",
Choices: []helix.ChoiceParam{
helix.ChoiceParam{ Title: "choice 1" },
helix.ChoiceParam{ Title: "choice 2" },
Choices: []helix.PollChoiceParam{
helix.PollChoiceParam{ Title: "choice 1" },
helix.PollChoiceParam{ Title: "choice 2" },
},
Duration: 30,
})
Expand Down
77 changes: 77 additions & 0 deletions docs/predictions_docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Predictions Documentation

## Get Predictions

This is an example of how to get predictions.

```go
client, err := helix.NewClient(&helix.Options{
ClientID: "your-client-id",
})
if err != nil {
// handle error
}

resp, err := client.GetPredictions(&helix.PredictionsParams{
BroadcasterID: "145328278",
})
if err != nil {
// handle error
}

fmt.Printf("%+v\n", resp)
```

## Create Prediction

This is an example of how to create a prediction.

```go
client, err := helix.NewClient(&helix.Options{
ClientID: "your-client-id",
})
if err != nil {
// handle error
}

resp, err := client.CreatePrediction(&helix.CreatePredictionParams{
BroadcasterID: "145328278",
Title: "Test",
Outcomes: []helix.PredictionChoiceParam{
helix.PredictionChoiceParam{ Title: "choice 1" },
helix.PredictionChoiceParam{ Title: "choice 2" },
},
PredictionWindow: 300,
})
if err != nil {
// handle error
}

fmt.Printf("%+v\n", resp)
```

## End Prediction

This is an example of how to end a prediction.

```go
client, err := helix.NewClient(&helix.Options{
ClientID: "your-client-id",
})
if err != nil {
// handle error
}

resp, err := client.EndPrediction(&helix.EndPredictionParams{
BroadcasterID: "145328278",
ID: "c36165d9-d5f5-4f81-ab56-17e7347110c8",
Status: "RESOLVED",
WinningOutcomeID: "d0c0194a-6016-4ca3-b8eb-0c61183758ab",
})
if err != nil {
// handle error
}

fmt.Printf("%+v\n", resp)
```

8 changes: 4 additions & 4 deletions eventsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ type EventSubChannelPollBeginEvent struct {
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
Title string `json:"title"`
Choices []Choice `json:"choices"`
BitVoting EventSubBitVoting `json:"bits_voting"`
Choices []PollChoice `json:"choices"`
BitsVoting EventSubBitVoting `json:"bits_voting"`
ChannelPointsVoting EventSubChannelPointsVoting `json:"channel_points_voting"`
StartedAt Time `json:"started_at"`
EndsAt Time `json:"ends_at"`
Expand All @@ -228,8 +228,8 @@ type EventSubChannelPollEndEvent struct {
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
Title string `json:"title"`
Choices []Choice `json:"choices"`
BitVoting EventSubBitVoting `json:"bits_voting"`
Choices []PollChoice `json:"choices"`
BitsVoting EventSubBitVoting `json:"bits_voting"`
ChannelPointsVoting EventSubChannelPointsVoting `json:"channel_points_voting"`
Status string `json:"status"`
StartedAt Time `json:"started_at"`
Expand Down
40 changes: 22 additions & 18 deletions polls.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ package helix
// Poll ...
type Poll struct {
ID string `json:"id"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
BroadcasterID string `json:"broadcaster_id"`
BroadcasterLogin string `json:"broadcaster_login"`
BroadcasterName string `json:"broadcaster_name"`
Title string `json:"title"`
Choices []Choice `json:"choices"`
Choices []PollChoice `json:"choices"`
BitsVotingEnabled bool `json:"bits_voting_enabled"`
BitsPerVote int `json:"bits_per_vote"`
ChannelPointsVotingEnabled bool `json:"channel_points_voting_enabled"`
ChannelPointsPerVote int `json:"channel_points_per_vote"`
Status string `json:"status"`
Duration int `json:"duration"`
StartedAt Time `json:"started_at"`
EndedAt Time `json:"ended_at"`
EndedAt Time `json:"ended_at"`
}

// Choice ...
type Choice struct {
// PollChoice ...
type PollChoice struct {
ID string `json:"id"`
Title string `json:"title"`
BitVotes int `json:"bit_votes"`
BitsVotes int `json:"bits_votes"`
ChannelPointsVotes int `json:"channel_points_votes"`
Votes int `json:"votes"`
}
Expand All @@ -41,6 +42,9 @@ type PollsResponse struct {
// PollsParams ...
type PollsParams struct {
BroadcasterID string `query:"broadcaster_id"`
ID string `query:"id"`
After string `query:"after"`
First string `query:"first`
}

// GetPollsResponse ...
Expand All @@ -67,18 +71,18 @@ func (c *Client) GetPolls(params *PollsParams) (*PollsResponse, error) {

// CreatePollParams ...
type CreatePollParams struct {
BroadcasterID string `json:"broadcaster_id"`
Title string `json:"title"` // Maximum: 60 characters.
Choices []ChoiceParam `json:"choices"` // Minimum: 2 choices. Maximum: 5 choices.
Duration int `json:"duration"` // Minimum: 15. Maximum: 1800.
BitsVotingEnabled bool `json:"bits_voting_enabled"` // Default: false
BitsPerVote int `json:"bits_per_vote"` // Minimum: 0. Maximum: 10000.
ChannelPointsVotingEnabled bool `json:"channel_points_voting_enabled"` // Default: false
ChannelPointsPerVote int `json:"channel_points_per_vote"` // Minimum: 0. Maximum: 1000000.
BroadcasterID string `json:"broadcaster_id"`
Title string `json:"title"` // Maximum: 60 characters.
Choices []PollChoiceParam `json:"choices"` // Minimum: 2 choices. Maximum: 5 choices.
Duration int `json:"duration"` // Minimum: 15. Maximum: 1800.
BitsVotingEnabled bool `json:"bits_voting_enabled"` // Default: false
BitsPerVote int `json:"bits_per_vote"` // Minimum: 0. Maximum: 10000.
ChannelPointsVotingEnabled bool `json:"channel_points_voting_enabled"` // Default: false
ChannelPointsPerVote int `json:"channel_points_per_vote"` // Minimum: 0. Maximum: 1000000.
}

// ChoiceParam ...
type ChoiceParam struct {
// PollChoiceParam ...
type PollChoiceParam struct {
Title string `json:"title"` // Maximum: 25 characters.
}

Expand Down
6 changes: 3 additions & 3 deletions polls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ func TestCreatePoll(t *testing.T) {
&CreatePollParams{
BroadcasterID: "145328278",
Title: "Test",
Choices: []ChoiceParam{
ChoiceParam{ Title: "choix 1" },
ChoiceParam{ Title: "choix 2" },
Choices: []PollChoiceParam{
PollChoiceParam{ Title: "choix 1" },
PollChoiceParam{ Title: "choix 2" },
},
Duration: 30,
},
Expand Down
133 changes: 133 additions & 0 deletions predictions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package helix

// Prediction ... same struct as Poll
type Prediction struct {
ID string `json:"id"`
BroadcasterUserID string `json:"broadcaster_id"`
BroadcasterUserLogin string `json:"broadcaster_login"`
BroadcasterUserName string `json:"broadcaster_name"`
Title string `json:"title"`
WinningOutcomeID string `json:"winning_outcome_id"`
Outcomes []Outcomes `json:"outcomes"`
PredictionWindow int `json:"prediction_window"`
Status string `json:"status"`
CreatedAt Time `json:"created_at"`
EndedAt Time `json:"ended_at"`
LockedAt Time `json:"locked_at"`
}

// Outcomes ...
type Outcomes struct {
ID string `json:"id"`
Title string `json:"title"`
Users int `json:"users"`
ChannelPoints int `json:"channel_points"`
TopPredictors []TopPredictor `json:"top_predictors"`
Color string `json:"color"`
}

// TopPredictor ...
type TopPredictor struct {
UserID string `json:"user_id"`
UserName string `json:"user_name"`
UserLogin string `json:"user_login"`
ChannelPointsUsed int `json:"channel_points_used"`
ChannelPointsWon int `json:"channel_points_won"`
}

// ManyPredictions ...
type ManyPredictions struct {
Predictions []Prediction `json:"data"`
Pagination Pagination `json:"pagination"`
}

// PredictionsResponse ...
type PredictionsResponse struct {
ResponseCommon
Data ManyPredictions
}

// PredictionsParams ...
type PredictionsParams struct {
BroadcasterID string `query:"broadcaster_id"`
ID string `query:"id"`
After string `query:"after"`
First string `query:"first`
}

// GetPredictionsResponse ...
type GetPredictionsResponse struct {
ResponseCommon
Data ManyPredictions
}

// GetPredictions ...
// Required scope: channel:read:predictions
func (c *Client) GetPredictions(params *PredictionsParams) (*PredictionsResponse, error) {
resp, err := c.get("/predictions", &ManyPredictions{}, params)
if err != nil {
return nil, err
}

predictions := &PredictionsResponse{}
resp.HydrateResponseCommon(&predictions.ResponseCommon)
predictions.Data.Predictions = resp.Data.(*ManyPredictions).Predictions
predictions.Data.Pagination = resp.Data.(*ManyPredictions).Pagination

return predictions, nil
}

// CreatePredictionParams ...
type CreatePredictionParams struct {
BroadcasterID string `json:"broadcaster_id"`
Title string `json:"title"` // Maximum: 45 characters.
Outcomes []PredictionChoiceParam `json:"outcomes"` // 2 choices mandatory
PredictionWindow int `json:"prediction_window"` // Minimum: 1. Maximum: 1800.
}

// PredictionChoiceParam ...
type PredictionChoiceParam struct {
Title string `json:"title"` // Maximum: 25 characters.
}

// CreatePrediction ...
// Required scope: channel:manage:predictions
func (c *Client) CreatePrediction(params *CreatePredictionParams) (*PredictionsResponse, error) {
resp, err := c.postAsJSON("/predictions", &ManyPredictions{}, params)
if err != nil {
return nil, err
}

predictions := &PredictionsResponse{}
resp.HydrateResponseCommon(&predictions.ResponseCommon)
predictions.Data.Predictions = resp.Data.(*ManyPredictions).Predictions
predictions.Data.Pagination = resp.Data.(*ManyPredictions).Pagination

return predictions, nil
}

// EndPredictionParams ...
type EndPredictionParams struct {
BroadcasterID string `json:"broadcaster_id"`
ID string `json:"id"`
Status string `json:"status"`
WinningOutcomeID string `json:"winning_outcome_id"`
}

// EndPrediction ...
// Required scope: channel:manage:predictions
func (c *Client) EndPrediction(params *EndPredictionParams) (*PredictionsResponse, error) {
resp, err := c.patchAsJSON("/predictions", &ManyPredictions{}, params)
if err != nil {
return nil, err
}

predictions := &PredictionsResponse{}
resp.HydrateResponseCommon(&predictions.ResponseCommon)
predictions.Data.Predictions = resp.Data.(*ManyPredictions).Predictions
predictions.Data.Pagination = resp.Data.(*ManyPredictions).Pagination

return predictions, nil
}


Loading

0 comments on commit 43a817b

Please sign in to comment.