Skip to content

Commit

Permalink
Always have a '/' as the end of the api host
Browse files Browse the repository at this point in the history
This will allow for apis like 'https://someapi.com/v2'
  • Loading branch information
colindecarlo committed Dec 13, 2015
1 parent c4e11d5 commit bd22aaa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/Uhura.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class Uhura
private $http;
private $responseHandler;

private $resource = '';
private $resource = [];
private $token = null;

public function __construct($api)
{
$this->api = $api;
$this->setApiRoot($api);
$this->http = new Client(['base_uri' => $this->api]);
$this->responseHandler = new ResponseHandler\Passthru;
}
Expand All @@ -26,13 +26,18 @@ public static function test($api)
$uhura = new static($api);

$uhura->http = new Client([
'base_uri' => $api,
'base_uri' => $uhura->api,
'handler' => new MockHandler
]);

return $uhura;
}

protected function setApiRoot($api)
{
$this->api = strrev($api)[0] == '/' ? $api : $api . '/';
}

public function useResponseHandler($handler)
{
$this->responseHandler = $handler;
Expand Down Expand Up @@ -77,15 +82,20 @@ public function delete()

public function url()
{
return sprintf("%s%s", $this->api, $this->resource);
return sprintf("%s%s", $this->api, $this->getResource());
}

public function getResource()
{
return implode('/', $this->resource);
}

private function request($method, $payload = null)
{
$options = $this->buildOptionsForRequest($method, $payload);

return $this->responseHandler->handle(
$this->http->request($method, $this->resource, $options)
$this->http->request($method, $this->getResource(), $options)
);
}

Expand All @@ -104,15 +114,20 @@ private function buildOptionsForRequest($method, $payload)
return $options;
}

protected function appendToResource(/* $args */)
{
$this->resource = array_merge($this->resource, func_get_args());
}

public function __get($name)
{
$this->resource .= sprintf("/%s", $name);
$this->appendToResource($name);
return $this;
}

public function __call($method, $args)
{
$this->resource .= sprintf("/%s/%s", $method, $args[0]);
$this->appendToResource($method, $args[0]);
return $this;
}
}
11 changes: 11 additions & 0 deletions tests/UhuraTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,15 @@ public function test_that_uhura_can_attach_a_query_string_to_get_requests()

$this->assertEquals('foo=bar', $handler->getLastRequest()->getUri()->getQuery());
}

public function test_that_uhura_respects_the_version_specifier_of_apis()
{
$this->uhura = Uhura::test('https://example.com/v2');
$handler = $this->uhura->getHttp()->getConfig('handler');
$handler->append(new Response);

$this->uhura->users(1)->blog->get();

$this->assertEquals('https://example.com/v2/users/1/blog', (string)$handler->getLastRequest()->getUri());
}
}

0 comments on commit bd22aaa

Please sign in to comment.