client code for poll voting
This commit is contained in:
parent
c53908f3cf
commit
d3b541dc86
1 changed files with 24 additions and 0 deletions
|
@ -5,6 +5,8 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
|
@ -29,3 +31,25 @@ func (g *Client) GetPoll(pollID string) (model.Poll, error) {
|
|||
|
||||
return poll, nil
|
||||
}
|
||||
|
||||
func (g *Client) VoteInPoll(pollID string, choices []int) error {
|
||||
form := struct {
|
||||
Choices []int `json:"choices"`
|
||||
}{
|
||||
Choices: choices,
|
||||
}
|
||||
|
||||
data, err := json.Marshal(form)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to encode the JSON form: %w", err)
|
||||
}
|
||||
|
||||
requestBody := bytes.NewBuffer(data)
|
||||
url := g.Authentication.Instance + pollPath + "/" + pollID + "/vote"
|
||||
|
||||
if err := g.sendRequest(http.MethodPost, url, requestBody, nil); err != nil {
|
||||
return fmt.Errorf("received an error after sending the request to vote in the poll: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue