-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSwagger.php
242 lines (207 loc) · 6.76 KB
/
Swagger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/**
* APIClient.php
*/
/* Autoload the model definition files */
/**
*
*
* @param string $className the class to attempt to load
*/
function swagger_autoloader($className) {
$currentDir = dirname(__FILE__);
if (file_exists($currentDir . '/' . $className . '.php')) {
include $currentDir . '/' . $className . '.php';
} elseif (file_exists($currentDir . '/models/' . $className . '.php')) {
include $currentDir . '/models/' . $className . '.php';
}
}
spl_autoload_register('swagger_autoloader');
class APIClient {
public static $POST = "POST";
public static $GET = "GET";
public static $PUT = "PUT";
public static $DELETE = "DELETE";
/**
* @param string $apiKey your API key
* @param string $apiServer the address of the API server
*/
function __construct($apiKey, $apiServer) {
$this->apiKey = $apiKey;
$this->apiServer = $apiServer;
}
/**
* @param string $resourcePath path to method endpoint
* @param string $method method to call
* @param array $queryParams parameters to be place in query URL
* @param array $postData parameters to be placed in POST body
* @param array $headerParams parameters to be place in request header
* @return unknown
*/
public function callAPI($resourcePath, $method, $queryParams, $postData,
$headerParams) {
$headers = array();
$headers[] = "Content-type: application/json";
# Allow API key from $headerParams to override default
$added_api_key = False;
if ($headerParams != null) {
foreach ($headerParams as $key => $val) {
$headers[] = "$key: $val";
if ($key == 'api_key') {
$added_api_key = True;
}
}
}
if (! $added_api_key) {
$headers[] = "api_key: " . $this->apiKey;
}
if (is_object($postData) or is_array($postData)) {
$postData = json_encode(self::sanitizeForSerialization($postData));
}
$url = $this->apiServer . $resourcePath;
$curl = curl_init();
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
// return the result on success, rather than just TRUE
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
if (! empty($queryParams)) {
$url = ($url . '?' . http_build_query($queryParams));
}
if ($method == self::$POST) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
} else if ($method == self::$PUT) {
$json_data = json_encode($postData);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
} else if ($method == self::$DELETE) {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
} else if ($method != self::$GET) {
throw new Exception('Method ' . $method . ' is not recognized.');
}
curl_setopt($curl, CURLOPT_URL, $url);
// Make the request
$response = curl_exec($curl);
$response_info = curl_getinfo($curl);
// Handle the response
if ($response_info['http_code'] == 0) {
throw new Exception("TIMEOUT: api call to " . $url .
" took more than 5s to return" );
} else if ($response_info['http_code'] == 200) {
$data = json_decode($response);
} else if ($response_info['http_code'] == 401) {
throw new Exception("Unauthorized API request to " . $url .
": ".json_decode($response)->message );
} else if ($response_info['http_code'] == 404) {
$data = null;
} else {
throw new Exception("Can't connect to the api: " . $url .
" response code: " .
$response_info['http_code']);
}
return $data;
}
/**
* Build a JSON POST object
*/
public static function sanitizeForSerialization($postData) {
foreach ($postData as $key => $value) {
if (is_a($value, "DateTime")) {
$postData->{$key} = $value->format(DateTime::ISO8601);
}
}
return $postData;
}
/**
* Take value and turn it into a string suitable for inclusion in
* the path, by url-encoding.
* @param string $value a string which will be part of the path
* @return string the serialized object
*/
public static function toPathValue($value) {
return rawurlencode($value);
}
/**
* Take value and turn it into a string suitable for inclusion in
* the query, by imploding comma-separated if it's an object.
* If it's a string, pass through unchanged. It will be url-encoded
* later.
* @param object $object an object to be serialized to a string
* @return string the serialized object
*/
public static function toQueryValue($object) {
if (is_array($object)) {
return implode(',', $object);
} else {
return $object;
}
}
/**
* Just pass through the header value for now. Placeholder in case we
* find out we need to do something with header values.
* @param string $value a string which will be part of the header
* @return string the header string
*/
public static function toHeaderValue($value) {
return $value;
}
/**
* Deserialize a JSON string into an object
*
* @param object $object object or primitive to be deserialized
* @param string $class class name is passed as a string
* @return object an instance of $class
*/
public static function deserialize($object, $class) {
if (gettype($object) == "NULL") {
return $object;
}
if (substr($class, 0, 6) == 'array[') {
$sub_class = substr($class, 6, -1);
$sub_objects = array();
foreach ($object as $sub_object) {
$sub_objects[] = self::deserialize($sub_object,
$sub_class);
}
return $sub_objects;
} elseif ($class == 'DateTime') {
return new DateTime($object);
} elseif (in_array($class, array('string', 'int', 'float', 'bool'))) {
settype($object, $class);
return $object;
} else {
$instance = new $class(); // this instantiates class named $class
$classVars = get_class_vars($class);
}
foreach ($object as $property => $value) {
// Need to handle possible pluralization differences
$true_property = $property;
if (! property_exists($class, $true_property)) {
if (substr($property, -1) == 's') {
$true_property = substr($property, 0, -1);
}
}
if (array_key_exists($true_property, $classVars['swaggerTypes'])) {
$type = $classVars['swaggerTypes'][$true_property];
} else {
$type = 'string';
}
if (in_array($type, array('string', 'int', 'float', 'bool'))) {
settype($value, $type);
$instance->{$true_property} = $value;
} elseif (preg_match("/array<(.*)>/", $type, $matches)) {
$sub_class = $matches[1];
$instance->{$true_property} = array();
foreach ($value as $sub_property => $sub_value) {
$instance->{$true_property}[] = self::deserialize($sub_value,
$sub_class);
}
} else {
$instance->{$true_property} = self::deserialize($value, $type);
}
}
return $instance;
}
}
?>