Default Headers: allowing the setting of defaultHeaders on ApiClient

This commit is contained in:
James Ebentier
2015-03-20 14:45:12 -07:00
parent 925caa223a
commit e782ea2c05

View File

@@ -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));