Skip to content

Commit

Permalink
Merge pull request #49 from unclecheese/clarifai-api-key
Browse files Browse the repository at this point in the history
REOPENED: Adding support for Clarifai API keys and removing support for tokens (#46)
  • Loading branch information
darrynten authored Mar 28, 2018
2 parents e80992d + 7764540 commit a2d1e05
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 151 deletions.
7 changes: 3 additions & 4 deletions src/Clarifai.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ class Clarifai
/**
* Clarifai constructor
*
* @param string $clientId The client ID
* @param string $clientSecret The client secret
* @param string $apiKey The Api Key
*/
public function __construct($clientId, $clientSecret)
public function __construct($apiKey)
{
$this->request = new RequestHandler($clientId, $clientSecret);
$this->request = new RequestHandler($apiKey);
}

/**
Expand Down
86 changes: 8 additions & 78 deletions src/Request/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,52 +49,21 @@ class RequestHandler
private $version = 'v2';

/**
* Clarifai Client ID
* Clarifai API Key
*
* @var string $clientId
* @var string $apiKey
*/
private $clientId;

/**
* The time until which token is valid
*
* @var \DateTime
*/
private $tokenExpireTime;

/**
* Clarifai Client Secret
*
* @var string $clientSecret
*/
private $clientSecret;

/**
* Clarifai Token
*
* @var string $token
*/
private $token;

/**
* Clarifai Token type
*
* @var string $token
*/
private $tokenType;
private $apiKey;


/**
* Request handler constructor
*
* @param string $clientId The client ID
* @param string $clientSecret The client secret
* @param $apiKey string Simple API Key provided by Clarifai
*/
public function __construct($clientId, $clientSecret)
public function __construct($apiKey)
{
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->tokenExpireTime = new \DateTime();
$this->apiKey = $apiKey;
$this->client = new Client();
}

Expand Down Expand Up @@ -138,61 +107,22 @@ public function handleRequest(string $method, string $uri = '', array $options =
}
}

/**
* Get token for Clarifai API requests
*
* @return string
*/
private function getAuthToken()
{
// Generate a new token if current is expired or empty
if (!$this->token || new \DateTime() > $this->tokenExpireTime) {
$this->requestToken();
}

return $this->tokenType . ' ' . $this->token;
}

/**
* Make request to Clarifai API for the new token
*/
private function requestToken()
{
$tokenResponse = $this->handleRequest(
'POST',
$this->url . '/v1/token', // endpoint is available only in v1
[
'form_params' => [
'grant_type' => 'client_credentials',
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
],
]
);

$this->tokenExpireTime->modify(
sprintf('+%s seconds', $tokenResponse['expires_in'])
);
$this->token = $tokenResponse['access_token'];
$this->tokenType = $tokenResponse['token_type'];
}

/**
* Makes a request to Clarifai
*
* @param string $method The API method
* @param string $path The path
* @param array $parameters The request parameters
*
* @return []
* @return array
*
* @throws ApiException
*/
public function request(string $method, string $path, array $parameters = [])
{
$options = [
'headers' => [
'Authorization' => $this->getAuthToken(),
'Authorization' => sprintf('Key %s', $this->apiKey),
'User-Agent' => sprintf(
'Clarifai PHP (https://github.com/darrynten/clarifai-php);v%s;%s',
\DarrynTen\Clarifai\Clarifai::VERSION,
Expand Down
2 changes: 1 addition & 1 deletion tests/Clarifai/ClarifaiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ClarifaiTest extends \PHPUnit_Framework_TestCase

public function setUp()
{
$this->clarifai = new Clarifai('clientId', 'clientSecret');
$this->clarifai = new Clarifai('apiKey');
}

/**
Expand Down
69 changes: 1 addition & 68 deletions tests/Clarifai/Request/RequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,74 +136,7 @@ public function testRequestException()
->end();
$this->http->setUp();

$clarifai = new RequestHandler('', '');
$clarifai = new RequestHandler('');
$clarifai->handleRequest('GET', 'http://localhost:8082/foo', []);
}

public function testRequestWithToken()
{
$clientId = 'client_id';
$clientSecret = 'client_secret';
$method = 'method';
$path = 'path';
$token = 'token';
$tokenType = 'token_type';
$result = 'result';

// Creates a partially mock of RequestHandler with mocked `handleRequest` method
$clarifai = \Mockery::mock(
'DarrynTen\Clarifai\Request\RequestHandler[handleRequest]',
[
$clientId,
$clientSecret,
]
);

$clarifai->shouldReceive('handleRequest')
->once()
->with(
'POST',
'https://api.clarifai.com/v1/token',
[
'form_params' => [
'grant_type' => 'client_credentials',
'client_id' => $clientId,
'client_secret' => $clientSecret,
],
]
)
->andReturn(
(array)[
'expires_in' => '60',
'access_token' => $token,
'token_type' => $tokenType,
]
);

$clarifai->shouldReceive('handleRequest')
->twice()
->with(
$method,
'https://api.clarifai.com/v2/'.$path,
[
'headers' => [
'Authorization' => $tokenType . ' ' . $token,
'User-Agent' => 'Clarifai PHP (https://github.com/darrynten/clarifai-php);v0.9.3;' . phpversion(),
],
],
[]
)
->andReturn($result);

// Only one token request should be made to Clarifai API
$this->assertEquals(
$result,
$clarifai->request($method, $path)
);

$this->assertEquals(
$result,
$clarifai->request($method, $path)
);
}
}

0 comments on commit a2d1e05

Please sign in to comment.