@@ -38,14 +38,24 @@ class APIClient {
38
38
public static $PUT = " PUT" ;
39
39
public static $DELETE = " DELETE" ;
40
40
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
+
41
51
/**
42
52
* @param string $host the address of the API server
43
53
* @param string $headerName a header to pass on requests
54
+ * @param string $headerValue the value of the header to pass on requests
44
55
*/
45
56
function __construct($host , $headerName = null, $headerValue = null) {
46
57
$this -> host = $host ;
47
- $this -> headerName = $headerName ;
48
- $this -> headerValue = $headerValue ;
58
+ $this -> defaultHeaders [$headerName ] = $headerValue ;
49
59
}
50
60
51
61
/**
@@ -58,6 +68,14 @@ class APIClient {
58
68
$this->curl_timout = $seconds;
59
69
}
60
70
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
+
61
79
62
80
/**
63
81
* @param string $resourcePath path to method endpoint
@@ -72,19 +90,13 @@ class APIClient {
72
90
73
91
$headers = array();
74
92
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 ) ;
77
95
if ($headerParams != null) {
78
96
foreach ($headerParams as $key => $val ) {
79
97
$headers [] = " $key: $val" ;
80
- if ($key == $this -> headerName ) {
81
- $added_api_key = True;
82
- }
83
98
}
84
99
}
85
- if (! $added_api_key && $this->headerName != null) {
86
- $headers [] = $this -> headerName . " : " . $this -> headerValue ;
87
- }
88
100
89
101
if (strpos($headers['Content-Type'], "multipart/form-data") < 0 and (is_object($postData) or is_array($postData))) {
90
102
$postData = json_encode($this -> sanitizeForSerialization ($postData ));
@@ -236,6 +248,7 @@ class APIClient {
236
248
} elseif ($class == 'DateTime') {
237
249
$deserialized = new \DateTime($data );
238
250
} elseif (in_array($class, array('string', 'int', 'float', 'bool'))) {
251
+ $data = (is_object($data ) || is_array($data )) ? json_encode($data ) : $data ;
239
252
settype($data , $class );
240
253
$deserialized = $data ;
241
254
} else {
0 commit comments