Skip to content

Commit 601a519

Browse files
committed
Merge pull request #530 from FindTheBest/master
PHP Request Headers and Deserialization
2 parents 925caa2 + 8323fdf commit 601a519

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

modules/swagger-codegen/src/main/resources/php/Swagger.mustache

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,24 @@ class APIClient {
3838
public static $PUT = "PUT";
3939
public static $DELETE = "DELETE";
4040
41+
/**
42+
* @var string $host Host URL
43+
*/
44+
public $host;
45+
46+
/**
47+
* @var array $defaultHeaders Array of Headers you would like applied to all requests with this client
48+
*/
49+
public $defaultHeaders;
50+
4151
/**
4252
* @param string $host the address of the API server
4353
* @param string $headerName a header to pass on requests
54+
* @param string $headerValue the value of the header to pass on requests
4455
*/
4556
function __construct($host, $headerName = null, $headerValue = null) {
4657
$this->host = $host;
47-
$this->headerName = $headerName;
48-
$this->headerValue = $headerValue;
58+
$this->defaultHeaders[$headerName] = $headerValue;
4959
}
5060

5161
/**
@@ -58,6 +68,14 @@ class APIClient {
5868
$this->curl_timout = $seconds;
5969
}
6070

71+
/**
72+
* @param string $headerName a header to pass on requests
73+
* @param string $headerValue the value of the header to pass on requests
74+
*/
75+
public function setDefaultHeader($headerName, $headerValue) {
76+
$this->defaultHeaders[$headerName] = $headerValue;
77+
}
78+
6179

6280
/**
6381
* @param string $resourcePath path to method endpoint
@@ -72,19 +90,13 @@ class APIClient {
7290
7391
$headers = array();
7492
75-
# Allow API key from $headerParams to override default
76-
$added_api_key = False;
93+
# Allow headers passed in through $headerParams to take precedent over $this->defaultHeaders
94+
$headerParams = array_merge($this->defaultHeaders, $headerParams);
7795
if ($headerParams != null) {
7896
foreach ($headerParams as $key => $val) {
7997
$headers[] = "$key: $val";
80-
if ($key == $this->headerName) {
81-
$added_api_key = True;
82-
}
8398
}
8499
}
85-
if (! $added_api_key && $this->headerName != null) {
86-
$headers[] = $this->headerName . ": " . $this->headerValue;
87-
}
88100

89101
if (strpos($headers['Content-Type'], "multipart/form-data") < 0 and (is_object($postData) or is_array($postData))) {
90102
$postData = json_encode($this->sanitizeForSerialization($postData));
@@ -236,6 +248,7 @@ class APIClient {
236248
} elseif ($class == 'DateTime') {
237249
$deserialized = new \DateTime($data);
238250
} elseif (in_array($class, array('string', 'int', 'float', 'bool'))) {
251+
$data = (is_object($data) || is_array($data)) ? json_encode($data) : $data;
239252
settype($data, $class);
240253
$deserialized = $data;
241254
} else {

0 commit comments

Comments
 (0)