From 2b5c3e0a2fd269485f95cb15d976513a1a107988 Mon Sep 17 00:00:00 2001 From: German Lena Date: Tue, 12 May 2015 11:28:52 -0300 Subject: [PATCH] SDK Client headers spec compliant #7 --- src/Api/ApiClient.php | 33 +++++++++++++++++++++++++++++++-- src/Api/RequestBuilder.php | 12 +++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/Api/ApiClient.php b/src/Api/ApiClient.php index 156803f9..e3e0da33 100644 --- a/src/Api/ApiClient.php +++ b/src/Api/ApiClient.php @@ -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); } } diff --git a/src/Api/RequestBuilder.php b/src/Api/RequestBuilder.php index 1d27e351..f0a562cd 100644 --- a/src/Api/RequestBuilder.php +++ b/src/Api/RequestBuilder.php @@ -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']; } @@ -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"; @@ -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) {