diff --git a/src/Client.php b/src/Client.php index fdb331a..e197b21 100644 --- a/src/Client.php +++ b/src/Client.php @@ -6,7 +6,6 @@ use Classy\Exceptions\SDKException; use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Exception\BadResponseException; -use Symfony\Component\HttpFoundation\Request; class Client { @@ -30,6 +29,11 @@ class Client */ private $httpClient; + /** + * @var Session + */ + private $defaultSession; + /** * @param array $config * @throws SDKException @@ -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 @@ -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); diff --git a/tests/ClientTest.php b/tests/ClientTest.php index cb54178..a1db703 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -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 */