From e782ea2c050e4349e19245d93509ca69efc62567 Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Fri, 20 Mar 2015 14:45:12 -0700 Subject: [PATCH] Default Headers: allowing the setting of defaultHeaders on ApiClient --- .../src/main/resources/php/Swagger.mustache | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/php/Swagger.mustache b/modules/swagger-codegen/src/main/resources/php/Swagger.mustache index a7a18866345d..dfc672b37ad6 100644 --- a/modules/swagger-codegen/src/main/resources/php/Swagger.mustache +++ b/modules/swagger-codegen/src/main/resources/php/Swagger.mustache @@ -38,14 +38,24 @@ class APIClient { public static $PUT = "PUT"; public static $DELETE = "DELETE"; + /** + * @var string $host Host URL + */ + public $host; + + /** + * @var array $defaultHeaders Array of Headers you would like applied to all requests with this client + */ + public $defaultHeaders; + /** * @param string $host the address of the API server * @param string $headerName a header to pass on requests + * @param string $headerValue the value of the header to pass on requests */ function __construct($host, $headerName = null, $headerValue = null) { $this->host = $host; - $this->headerName = $headerName; - $this->headerValue = $headerValue; + $this->defaultHeaders[$headerName] = $headerValue; } /** @@ -58,6 +68,14 @@ class APIClient { $this->curl_timout = $seconds; } + /** + * @param string $headerName a header to pass on requests + * @param string $headerValue the value of the header to pass on requests + */ + public function setDefaultHeader($headerName, $headerValue) { + $this->defaultHeaders[$headerName] = $headerValue; + } + /** * @param string $resourcePath path to method endpoint @@ -72,19 +90,13 @@ class APIClient { $headers = array(); - # Allow API key from $headerParams to override default - $added_api_key = False; + # Allow headers passed in through $headerParams to take president over $this->defaultHeaders + $headerParams = array_merge($this->defaultHeaders, $headerParams); if ($headerParams != null) { foreach ($headerParams as $key => $val) { $headers[] = "$key: $val"; - if ($key == $this->headerName) { - $added_api_key = True; - } } } - if (! $added_api_key && $this->headerName != null) { - $headers[] = $this->headerName . ": " . $this->headerValue; - } if (strpos($headers['Content-Type'], "multipart/form-data") < 0 and (is_object($postData) or is_array($postData))) { $postData = json_encode($this->sanitizeForSerialization($postData));