diff --git a/src/APnutI.php b/src/APnutI.php index 1c7ec23..5a27ff6 100644 --- a/src/APnutI.php +++ b/src/APnutI.php @@ -200,7 +200,7 @@ class APnutI // if they passed an array, build a list of parameters from it curl_setopt($ch, CURLOPT_POST, true); if (is_array($parameters) && $method !== 'POST-RAW') { - $parameters = http_build_query($parameters); + $parameters = $content_type === 'application/json' ? json_encode($parameters) : http_build_query($parameters); } curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); $headers[] = "Content-Type: ".$content_type; @@ -217,8 +217,7 @@ class APnutI $this->logger->info("Using server token for auth"); $headers[] = 'Authorization: Bearer '.$this->server_token; } - #$this->logger->info("Access token: ".$this->access_token); - #$this->logger->info("Headers: ".json_encode($headers)); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLINFO_HEADER_OUT, true); @@ -529,6 +528,25 @@ class APnutI } } + public function voteInPoll(int $poll_id, array $options, ?string $poll_token): Poll + { + $params = [ + 'positions' => $options + ]; + if (!empty($poll_token)) { + $params['poll_token'] = $poll_token; + } + $this->logger->debug(json_encode($params)); + $resp = $this->makeRequest( + 'PUT', + "/polls/$poll_id/response", + $params, + 'application/json' + ); + #TODO: Use getPollFromEndpoint + return new Poll($resp, $this); + } + public function getAuthorizedUser(): User { $this->current_user ??= new User($this->get('/users/me'), $this); diff --git a/src/Entities/Poll.php b/src/Entities/Poll.php index 8ca51d3..262436d 100644 --- a/src/Entities/Poll.php +++ b/src/Entities/Poll.php @@ -119,7 +119,8 @@ class Poll return $optns; } - public function getMyVotes(): array { + public function getMyVotes(): array + { $optns = []; foreach ($this->options as $option) { if ($option->is_your_response) { @@ -145,6 +146,11 @@ class Poll return $this->closed_at <= new \DateTime(); } + public function vote(array $options): Poll + { + return $this-api->voteInPoll($this->id, $options, $this->token); + } + public function __toString(): string { if (!empty($this->user)) {