Skip to content

Commit

Permalink
Merge pull request #8 from auth0/1.x.x-dev
Browse files Browse the repository at this point in the history
SDK Client headers spec compliant #7
  • Loading branch information
glena committed May 12, 2015
2 parents 65f75ca + 2b5c3e0 commit 85aabd3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
33 changes: 31 additions & 2 deletions src/Api/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,51 @@

namespace Auth0\SDK\API;

use Auth0\SDK\API\Header\Header;

class ApiClient {


const API_VERSION = "1.0.1";
protected static $meta = array();

public static function addHeaderInfoMeta($info) {
self::$meta[] = $info;
}

protected $domain;
protected $basePath;
protected $headers;

public function __construct($config) {
$this->basePath = $config['basePath'];
$this->domain = $config['domain'];
$this->headers = isset($config['headers']) ? $config['headers'] : array();

$this->addInformationHeaders();
}

protected function addInformationHeaders() {

$meta = '';

if (!empty(self::$meta)) {
$meta = '(' . implode(',', self::$meta) . ')';
}

$this->headers[] = new Header('User-Agent', 'PHP/' . phpversion() . $meta);
$this->headers[] = new Header('Auth0-Client', 'PHP/' . self::API_VERSION);

}

public function __call($name, $arguments) {
return new RequestBuilder(array(
$builder = new RequestBuilder(array(
'domain' => $this->domain,
'method' => $name,
'path' => array( $this->basePath )
'path' => array( $this->basePath ),
));

return $builder->withHeaders($this->headers);
}

}
12 changes: 11 additions & 1 deletion src/Api/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct( $config ) {

$this->method = $config['method'];
$this->domain = $config['domain'];
$this->headers = isset($config['headers']) ? $config['headers'] : array();
if (array_key_exists('path', $config)) $this->path = $config['path'];

}
Expand Down Expand Up @@ -62,7 +63,7 @@ public function dump() {
echo "URL: {$this->getUrl()}\n";

echo "HEADERS:\n\t";
echo implode("\n\t",$this->headers);
echo implode("\n\t",array_map(function($k,$v){ return "$k: $v";}, array_keys($this->headers), $this->headers));
echo "\n";

echo "BODY: {$this->body}\n";
Expand Down Expand Up @@ -92,6 +93,15 @@ public function call() {

}

public function withHeaders($headers) {

foreach ($headers as $header) {
$this->withHeader($header);
}

return $this;
}

public function withHeader($header, $value = null) {

if ($header instanceof Header) {
Expand Down

0 comments on commit 85aabd3

Please sign in to comment.