Skip to content
This repository has been archived by the owner on Nov 15, 2018. It is now read-only.

Commit

Permalink
Added API key validation helper
Browse files Browse the repository at this point in the history
  • Loading branch information
dragosprotung committed Jun 3, 2016
1 parent 636af9e commit b06a01a
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/ApiKeyExtractor.php → src/ApiKeyHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace Speicher210\KontaktIO;

use GuzzleHttp\Client as GuzzleHttpClient;
use GuzzleHttp\Exception\ClientException;
use Speicher210\KontaktIO\Exception\ApiKeyExtractionInvalidCredentialsException;

/**
* Get the API key using username and password.
* Helper functions around the API key.
*/
class ApiKeyExtractor
class ApiKeyHelper
{
/**
* Get the API key using a username and password combination.
Expand Down Expand Up @@ -38,4 +39,21 @@ public static function getApiKey($username, $password)

return (string)$response->getBody();
}

public static function apiKeyIsValid($apiKey)
{
$client = new Client($apiKey);

try {
$client->get('/manager/me');

return true;
} catch (ClientException $e) {
if ($e->getResponse()->getStatusCode() === 401) {
return false;
}

throw $e;
}
}
}

0 comments on commit b06a01a

Please sign in to comment.