[TASK] Throw exception if required API key is missing

This commit is contained in:
Markus Klein 2023-07-06 18:30:20 +02:00
parent fee06f0fa1
commit a7e0532d06
4 changed files with 18 additions and 7 deletions

View file

@ -26,6 +26,7 @@ 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 SkillDisplay\PHPToolKit\Configuration\Settings; use SkillDisplay\PHPToolKit\Configuration\Settings;
use SkillDisplay\PHPToolKit\Entity\Campaign; use SkillDisplay\PHPToolKit\Entity\Campaign;
@ -45,6 +46,9 @@ class Campaigns
public function getForUser(): array public function getForUser(): array
{ {
if (!$this->settings->getApiKey()) {
throw new InvalidArgumentException('Missing API key', 1688660942);
}
$url = $this->settings->getAPIUrl() . '/api/v1/campaigns'; $url = $this->settings->getAPIUrl() . '/api/v1/campaigns';
try { try {
$result = $this->client->send(new Request( $result = $this->client->send(new Request(

View file

@ -6,11 +6,9 @@ namespace SkillDisplay\PHPToolKit\Api;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException; use InvalidArgumentException;
use SkillDisplay\PHPToolKit\Configuration\Settings; use SkillDisplay\PHPToolKit\Configuration\Settings;
use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Request;
use SkillDisplay\PHPToolKit\Entity\Campaign;
use SkillDisplay\PHPToolKit\Entity\MemberSkill;
use SkillDisplay\PHPToolKit\Entity\MemberSkill as Entity; use SkillDisplay\PHPToolKit\Entity\MemberSkill as Entity;
class MemberSkills class MemberSkills
@ -32,6 +30,9 @@ class MemberSkills
if ($id <= 0) { if ($id <= 0) {
throw new \Exception('ID of Organisation has to be a positive integer.'); throw new \Exception('ID of Organisation has to be a positive integer.');
} }
if (!$this->settings->getApiKey()) {
throw new InvalidArgumentException('Missing API key', 1688660942);
}
$url = $this->settings->getAPIUrl() . '/api/v1/organisation/' . $id . '/listVerifications/json'; $url = $this->settings->getAPIUrl() . '/api/v1/organisation/' . $id . '/listVerifications/json';
try { try {
$result = $this->client->send(new Request( $result = $this->client->send(new Request(

View file

@ -6,7 +6,7 @@ namespace SkillDisplay\PHPToolKit\Api;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException; use InvalidArgumentException;
use SkillDisplay\PHPToolKit\Configuration\Settings; use SkillDisplay\PHPToolKit\Configuration\Settings;
use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Request;
use SkillDisplay\PHPToolKit\Entity\Organisation as Entity; use SkillDisplay\PHPToolKit\Entity\Organisation as Entity;
@ -30,7 +30,10 @@ class Organisation
if ($id <= 0) { if ($id <= 0) {
throw new \Exception('ID of Organisation has to be a positive integer.'); throw new \Exception('ID of Organisation has to be a positive integer.');
} }
$url = $this->settings->getAPIUrl() . '/api/v1/organisation/' . $id . '/statistic'; if (!$this->settings->getApiKey()) {
throw new InvalidArgumentException('Missing API key', 1688660942);
}
$url = $this->settings->getAPIUrl() . '/api/v1/organisation/' . $id . '/statistics';
try { try {
$result = $this->client->send(new Request( $result = $this->client->send(new Request(
'GET', 'GET',

View file

@ -26,6 +26,7 @@ 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 SkillDisplay\PHPToolKit\Configuration\Settings; use SkillDisplay\PHPToolKit\Configuration\Settings;
use SkillDisplay\PHPToolKit\Entity\SkillSetProgress as Entity; use SkillDisplay\PHPToolKit\Entity\SkillSetProgress as Entity;
@ -47,7 +48,9 @@ class SkillSetProgress
if ($id <= 0) { if ($id <= 0) {
throw new \Exception('ID of SkillSet has to be a positive integer.', 1688639724754); throw new \Exception('ID of SkillSet has to be a positive integer.', 1688639724754);
} }
if (!$this->settings->getApiKey()) {
throw new InvalidArgumentException('Missing API key', 1688660942);
}
$url = $this->settings->getAPIUrl() . '/api/v1/skillset/' . $id . '/progress'; $url = $this->settings->getAPIUrl() . '/api/v1/skillset/' . $id . '/progress';
try { try {
$result = $this->client->send(new Request( $result = $this->client->send(new Request(
@ -60,7 +63,7 @@ class SkillSetProgress
)); ));
} catch (ClientException $e) { } catch (ClientException $e) {
if ($e->getCode() === 404) { if ($e->getCode() === 404) {
throw new \InvalidArgumentException('Given SkillSet with id "' . $id . '" not available.', 1688639748718); throw new InvalidArgumentException('Given SkillSet with id "' . $id . '" not available.', 1688639748718);
} }
throw $e; throw $e;
} }