Skip to content
This repository has been archived by the owner on May 28, 2023. It is now read-only.

Commit

Permalink
Added possibility to get access service with token
Browse files Browse the repository at this point in the history
  • Loading branch information
percymamedy committed Jun 3, 2016
1 parent 7cbc965 commit c464a59
Show file tree
Hide file tree
Showing 10 changed files with 941 additions and 92 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/vendor/
/.idea
composer.lock
18 changes: 12 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@
"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": "~5.3|~6.0|~6.2",
"illuminate/support": "~5.0|~5.1|~5.2"
"illuminate/support": "~5.0|~5.1|~5.2",
"nesbot/carbon": "^1.21"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"phpunit/phpunit": "~4.0",
"mockery/mockery": "0.9.*"
"mockery/mockery": "0.9.*",
"symfony/var-dumper": "^3.1"
},
"autoload": {
"classmap": [
"tests/TestCase.php"
],
"psr-4": {
"FindBrok\\WatsonBridge\\" : "src/"
}
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
184 changes: 172 additions & 12 deletions src/Bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ class Bridge
*/
protected $client;

/**
* The WatsonToken
*
* @var \FindBrok\WatsonBridge\Token
*/
protected $token;

/**
* Decide which method to use when sending request
*
* @var string
*/
protected $authMethod = 'credentials';

/**
* The limit for which we can re request token,
* when performing request
*
* @var int
*/
protected $exceptionThrottle = 0;

/**
* Default headers
*
Expand All @@ -58,14 +80,18 @@ class Bridge
* @param string $password
* @param string $endpoint
*/
public function __construct($username = null, $password = null, $endpoint = null)
public function __construct($username, $password, $endpoint)
{
//Set Username, Password and Endpoint
$this->username = $username;
$this->password = $password;
$this->endpoint = $endpoint;

//Set HttpClient
$this->setClient();
$this->setClient($endpoint);

//Set Token
$this->token = new Token($this->username);
}

/**
Expand Down Expand Up @@ -107,16 +133,73 @@ public function getHeaders()
return $this->headers;
}

/**
* Fetch token from Watson and Save it locally
*
* @param bool $incrementThrottle
* @return void
*/
public function fetchToken($incrementThrottle = false)
{
//Increment throttle if needed
if($incrementThrottle) {
$this->incrementThrottle();
}
//Reset Client
$this->setClient($this->getAuthorizationEndpoint());
//Get the token response
$response = $this->get('v1/token', [
'url' => $this->endpoint
]);
//Extract
$token = json_decode($response->getBody()->getContents(), true);
//Reset client
$this->setClient($this->endpoint);
//Update token
$this->token->updateToken($token['token']);
}

/**
* Get a token for authorization from Watson or Storage
*
* @return string
*/
public function getToken()
{
//Token is not valid
if (! $this->token->isValid()) {
//Fetch from Watson
$this->fetchToken();
}

//Return token
return $this->token->getToken();
}

/**
* Get the authorization endpoint for getting tokens
*
* @return string
*/
public function getAuthorizationEndpoint()
{
//Parse the endpoint
$parsedEndpoint = collect(parse_url($this->endpoint));
//Return auth url
return $parsedEndpoint->get('scheme').'://'.$parsedEndpoint->get('host').'/authorization/api/';
}

/**
* Creates the http client
*
* @param string $endpoint
* @return void
*/
private function setClient()
public function setClient($endpoint = null)
{
//Create client using API endpoint
$this->client = new Client([
'base_uri' => $this->endpoint,
'base_uri' => ! is_null($endpoint) ? $endpoint : $this->endpoint,
]);
}

Expand Down Expand Up @@ -164,7 +247,7 @@ public function failedRequest($response)
}

/**
* Make a Request to Watson
* Make a Request to Watson with credentials Auth
*
* @param string $method
* @param string $uri
Expand All @@ -175,8 +258,17 @@ public function request($method = 'GET', $uri = '', $options = [])
{
try {
//Make the request
return $this->getClient()->request($method, $uri, $options);
return $this->getClient()->request($method, $uri, $this->getRequestOptions($options));
} catch (ClientException $e) {
//We are using token auth and probably token expired
if($this->authMethod == 'token' && $e->getCode() == 401 && ! $this->isThrottledReached()) {
//Try refresh token
$this->fetchToken(true);
//Try requesting again
return $this->request($method, $uri, $options);
}
//Clear throttle for this request
$this->clearThrottle();
//Call Failed Request
$this->failedRequest($e->getResponse());
}
Expand All @@ -193,12 +285,8 @@ public function request($method = 'GET', $uri = '', $options = [])
*/
private function send($method = 'POST', $uri, $data, $type = 'json')
{
//Make a Post Request
$response = $this->request($method, $uri, $this->cleanOptions([
'headers' => $this->getHeaders(),
'auth' => $this->getAuth(),
$type => $data
]));
//Make the Request to Watson
$response = $this->request($method, $uri, [$type => $data]);
//Request Failed
if ($response->getStatusCode() != 200) {
//Throw Watson Bridge Exception
Expand All @@ -208,6 +296,78 @@ private function send($method = 'POST', $uri, $data, $type = 'json')
return $response;
}

/**
* Get Request options to pass along
*
* @param array $initial
* @return array
*/
public function getRequestOptions($initial = [])
{
//Define options
$options = collect($initial);
//Define an auth option
if($this->authMethod == 'credentials') {
$options = $options->merge([
'auth' => $this->getAuth(),
]);
} elseif ($this->authMethod == 'token') {
$this->appendHeaders([
'X-Watson-Authorization-Token' => $this->getToken()
]);
}
//Put Headers in options
$options = $options->merge([
'headers' => $this->getHeaders(),
]);
//Clean and return
return $this->cleanOptions($options->all());
}

/**
* Change the auth method
*
* @param string $method
* @return self
*/
public function useAuthMethodAs($method = 'credentials')
{
//Change auth method
$this->authMethod = $method;
//Return object
return $this;
}

/**
* Checks if throttle is reached
*
* @return bool
*/
public function isThrottledReached()
{
return $this->exceptionThrottle >= 2;
}

/**
* Increment throttle
*
* @return void
*/
public function incrementThrottle()
{
$this->exceptionThrottle++;
}

/**
* Clears throttle counter
*
* @return void
*/
public function clearThrottle()
{
$this->exceptionThrottle = 0;
}

/**
* Make a GET Request to Watson
*
Expand Down
2 changes: 2 additions & 0 deletions src/Storage/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
Loading

0 comments on commit c464a59

Please sign in to comment.