Added poll creation

This commit is contained in:
aymm 2021-04-04 21:22:38 +02:00
parent 3604b1ae19
commit 9f0c8ba3c6
Signed by: phlaym
GPG Key ID: A06651BAB6777237
4 changed files with 43 additions and 2 deletions

View File

@ -1,6 +1,7 @@
{
"name": "hutattedonmyarm/apnuti",
"type": "library",
"version": "0.1.0",
"description": "PHP Pnut library",
"keywords": ["pnut", "api"],
"license": "MIT",

View File

@ -36,7 +36,6 @@ class APnutI
protected array $headers = [];
protected ?string $server_token;
protected ?string $access_token;
protected LoggerInterface $logger;
protected string $token_session_key;
protected string $token_redirect_after_auth;
protected ?string $server_token_file_path = null;
@ -45,6 +44,7 @@ class APnutI
public ?Meta $meta = null;
public string $app_name = 'Abstract API';
public LoggerInterface $logger;
/*
* Error codes:

View File

@ -151,6 +151,45 @@ class Poll
return $this-api->voteInPoll($this->id, $options, $this->token);
}
/*
* This is inconsistend with most other functions (which are provided directly by the API object.
* I should probably settle on one of the two styles.
* I prefer having the methods in here, but having to pass the API object along is not so great,
* neither is having to make the API's logger public
* TODO for v2 I guess
*/
public static function create(
APnutI $api,
string $prompt,
array $options,
int $max_options,
int $duration_minutes,
bool $is_anonymous,
bool $is_public
): Poll {
$options = array_filter($options);
$options = array_map(
function ($v) {
return ['text' => $v];
},
$options
);
$params = [
'duration' => $duration_minutes,
'options' => array_filter($options), #filters empty options
'prompt' => $prompt,
'type' => 'io.pnut.core.poll',
'is_anonymous' => $is_anonymous,
'is_public' => $is_public,
'max_options' => $max_options
];
$api->logger->debug('Creating poll');
$api->logger->debug(json_encode($params));
$resp = $api->post('/polls', $params, 'application/json');
#TODO: Use getPollFromEndpoint
return new Poll($resp, $api);
}
public function __toString(): string
{
if (!empty($this->user)) {

View File

@ -33,7 +33,8 @@ class Meta
$this->code = $meta['code'];
if ($this->code === 400) {
throw new PnutException($meta['error_message']);
}if ($this->code === 401) {
}
if ($this->code === 401) {
throw new NotAuthorizedException($meta['error_message']);
}
if ($this->code === 403) {