Skip to content

Commit

Permalink
Adding the possibility to assign a default Session object to a Client
Browse files Browse the repository at this point in the history
  • Loading branch information
PGBI committed Aug 3, 2016
1 parent 93e41a2 commit 5ef8587
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Classy\Exceptions\SDKException;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\BadResponseException;
use Symfony\Component\HttpFoundation\Request;

class Client
{
Expand All @@ -30,6 +29,11 @@ class Client
*/
private $httpClient;

/**
* @var Session
*/
private $defaultSession;

/**
* @param array $config
* @throws SDKException
Expand Down Expand Up @@ -89,6 +93,16 @@ public function newAppSession()
return $session;
}

/**
* Default session will be used if $this->get(), $this->post(), $this->put() or $this->deleted() are invoked
* without session object.
* @param Session $session
*/
public function setDefaultSession(Session $session)
{
$this->defaultSession = $session;
}

/**
* @param $code
* @return Session
Expand Down Expand Up @@ -233,6 +247,10 @@ public function delete($endpoint, Session $session = null)
*/
public function request($verb, $endpoint, Session $session = null, $options = [])
{
if (is_null($session) && !is_null($this->defaultSession)) {
$session = $this->defaultSession;
}

if (!is_null($session)) {
if ($session->expired()) {
$this->refresh($session);
Expand Down
24 changes: 24 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,30 @@ public function testRequestWithExpiredSession()
$this->assertFalse($session->expired());
}

/**
* @covers Classy\Client::request
*/
public function testRequestWithDefaultSession()
{
$session = new Session([
'access_token' => 'abcdef',
'expires_in' => '1000'
]);
$this->client->setDefaultSession($session);

$this->guzzleMock->shouldReceive('request')
->once()
->with(
'GET',
'/3.0/endpoint',
Mockery::on(function($args) {
return $args === ['headers' => ['Authorization' => 'Bearer abcdef']];
}))
->andReturn(new Response(200, [], "{}"));

$this->client->request('GET', '/3.0/endpoint');
}

/**
* @covers Classy\Client::applyVersion
*/
Expand Down

0 comments on commit 5ef8587

Please sign in to comment.