[FEATURE] Add endpoint for skillset list

This commit is contained in:
Markus Klein 2023-07-07 12:49:17 +02:00
parent d5207ef3b5
commit ceabcd7482

View file

@ -26,6 +26,8 @@ namespace SkillDisplay\PHPToolKit\Api;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Request;
use InvalidArgumentException;
use RuntimeException;
use SkillDisplay\PHPToolKit\Configuration\Settings; use SkillDisplay\PHPToolKit\Configuration\Settings;
use SkillDisplay\PHPToolKit\Entity\SkillSet as Entity; use SkillDisplay\PHPToolKit\Entity\SkillSet as Entity;
@ -54,17 +56,19 @@ class SkillSet
$url .= '?includeFullSkills'; $url .= '?includeFullSkills';
} }
try { try {
$result = $this->client->send(new Request( $result = $this->client->send(
new Request(
'GET', 'GET',
$url, $url,
[ [
'Content-Type' => 'application/json', 'Content-Type' => 'application/json',
'x-api-key' => $this->settings->getApiKey() 'x-api-key' => $this->settings->getApiKey()
] ]
)); )
);
} catch (ClientException $e) { } catch (ClientException $e) {
if ($e->getCode() === 404) { if ($e->getCode() === 404) {
throw new \InvalidArgumentException('Given SkillSet with id "' . $id . '" not available.', 1601881616); throw new InvalidArgumentException('Given SkillSet with id "' . $id . '" not available.', 1601881616);
} }
throw $e; throw $e;
} }
@ -76,9 +80,59 @@ class SkillSet
$body = (string)$result->getBody(); $body = (string)$result->getBody();
if (strpos($body, 'Oops, an error occurred') !== false) { if (strpos($body, 'Oops, an error occurred') !== false) {
throw new \Exception('Did not get proper response for SkillSet. SkillSet with id "' . $id . '" does probably not exist.', 1600694312); throw new \Exception(
'Did not get proper response for SkillSet. SkillSet with id "' . $id . '" does probably not exist.',
1600694312
);
} }
return Entity::createFromJson($body, $this->settings); return Entity::createFromJson($body, $this->settings);
} }
/**
* @param bool $includeFullSkills
* @return Entity[]
*/
public function getAll(bool $includeFullSkills = false): array
{
$url = $this->settings->getAPIUrl() . '/api/v1/skillsets';
if ($includeFullSkills) {
$url .= '?includeFullSkills';
}
try {
$result = $this->client->send(
new Request(
'GET',
$url,
[
'Content-Type' => 'application/json',
'x-api-key' => $this->settings->getApiKey()
]
)
);
} catch (ClientException $e) {
if ($e->getCode() === 404) {
throw new InvalidArgumentException('Failed to fetch Skill Set data.', 1688726816);
}
throw $e;
}
if ($result->getStatusCode() !== 200) {
throw new RuntimeException('Did not get proper response for SkillSets.', 1688726814);
}
$body = (string)$result->getBody();
if (strpos($body, 'Oops, an error occurred') !== false) {
throw new RuntimeException('Did not get proper response for SkillSets.', 1688726813);
}
$skillSetsJson = json_decode($body, true);
$skillSets = [];
foreach ($skillSetsJson as $skillSet) {
$skillSets[] = Entity::createFromJson($skillSet, $this->settings);
}
return $skillSets;
}
} }