Skip to content

Commit 8d6d1cb

Browse files
committed
recovered APIclient due to merge
1 parent 6b2035e commit 8d6d1cb

File tree

1 file changed

+297
-0
lines changed

1 file changed

+297
-0
lines changed
Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
<?php
2+
/**
3+
* Copyright 2015 Reverb Technologies, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace {{invokerPackage}};
19+
20+
class APIClient {
21+
22+
public static $PATCH = "PATCH";
23+
public static $POST = "POST";
24+
public static $GET = "GET";
25+
public static $PUT = "PUT";
26+
public static $DELETE = "DELETE";
27+
28+
/**
29+
* @param string $host the address of the API server
30+
* @param string $headerName a header to pass on requests
31+
*/
32+
function __construct($host, $headerName = null, $headerValue = null) {
33+
$this->host = $host;
34+
$this->headerName = $headerName;
35+
$this->headerValue = $headerValue;
36+
}
37+
38+
/**
39+
* Set the user agent of the API client
40+
*
41+
* @param string $user_agent The user agent of the API client
42+
*/
43+
public function setUserAgent($user_agent) {
44+
if (!is_string($user_agent)) {
45+
throw new Exception('User-agent must be a string.');
46+
}
47+
$this->user_agent= $user_agent;
48+
}
49+
50+
/**
51+
* @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
52+
*/
53+
public function setTimeout($seconds) {
54+
if (!is_numeric($seconds)) {
55+
throw new Exception('Timeout variable must be numeric.');
56+
}
57+
$this->curl_timout = $seconds;
58+
}
59+
60+
/**
61+
* @param string $resourcePath path to method endpoint
62+
* @param string $method method to call
63+
* @param array $queryParams parameters to be place in query URL
64+
* @param array $postData parameters to be placed in POST body
65+
* @param array $headerParams parameters to be place in request header
66+
* @return mixed
67+
*/
68+
public function callAPI($resourcePath, $method, $queryParams, $postData,
69+
$headerParams) {
70+
71+
$headers = array();
72+
73+
# Allow API key from $headerParams to override default
74+
$added_api_key = False;
75+
if ($headerParams != null) {
76+
foreach ($headerParams as $key => $val) {
77+
$headers[] = "$key: $val";
78+
if ($key == $this->headerName) {
79+
$added_api_key = True;
80+
}
81+
}
82+
}
83+
if (! $added_api_key && $this->headerName != null) {
84+
$headers[] = $this->headerName . ": " . $this->headerValue;
85+
}
86+
87+
if (strpos($headers['Content-Type'], "multipart/form-data") < 0 and (is_object($postData) or is_array($postData))) {
88+
$postData = json_encode($this->sanitizeForSerialization($postData));
89+
}
90+
91+
$url = $this->host . $resourcePath;
92+
93+
$curl = curl_init();
94+
if ($this->curl_timout) {
95+
curl_setopt($curl, CURLOPT_TIMEOUT, $this->curl_timout);
96+
}
97+
// return the result on success, rather than just TRUE
98+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
99+
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
100+
101+
if (! empty($queryParams)) {
102+
$url = ($url . '?' . http_build_query($queryParams));
103+
}
104+
105+
if ($method == self::$POST) {
106+
curl_setopt($curl, CURLOPT_POST, true);
107+
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
108+
} else if ($method == self::$PATCH) {
109+
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
110+
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
111+
} else if ($method == self::$PUT) {
112+
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
113+
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
114+
} else if ($method == self::$DELETE) {
115+
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
116+
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
117+
} else if ($method != self::$GET) {
118+
throw new Exception('Method ' . $method . ' is not recognized.');
119+
}
120+
curl_setopt($curl, CURLOPT_URL, $url);
121+
122+
// Set user agent
123+
if ($this->user_agent) {
124+
curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent);
125+
} else { // use PHP-Swagger as the default user agent
126+
curl_setopt($curl, CURLOPT_USERAGENT, 'PHP-Swagger');
127+
}
128+
129+
// Make the request
130+
$response = curl_exec($curl);
131+
$response_info = curl_getinfo($curl);
132+
133+
// Handle the response
134+
if ($response_info['http_code'] == 0) {
135+
throw new APIClientException("TIMEOUT: api call to " . $url .
136+
" took more than 5s to return", 0, $response_info, $response);
137+
} else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
138+
$data = json_decode($response);
139+
if (json_last_error() > 0) { // if response is a string
140+
$data = $response;
141+
}
142+
} else if ($response_info['http_code'] == 401) {
143+
throw new APIClientException("Unauthorized API request to " . $url .
144+
": " . serialize($response), 0, $response_info, $response);
145+
} else if ($response_info['http_code'] == 404) {
146+
$data = null;
147+
} else {
148+
throw new APIClientException("Can't connect to the api: " . $url .
149+
" response code: " .
150+
$response_info['http_code'], 0, $response_info, $response);
151+
}
152+
return $data;
153+
}
154+
155+
/**
156+
* Build a JSON POST object
157+
*/
158+
protected function sanitizeForSerialization($data)
159+
{
160+
if (is_scalar($data) || null === $data) {
161+
$sanitized = $data;
162+
} else if ($data instanceof \DateTime) {
163+
$sanitized = $data->format(\DateTime::ISO8601);
164+
} else if (is_array($data)) {
165+
foreach ($data as $property => $value) {
166+
$data[$property] = $this->sanitizeForSerialization($value);
167+
}
168+
$sanitized = $data;
169+
} else if (is_object($data)) {
170+
$values = array();
171+
foreach (array_keys($data::$swaggerTypes) as $property) {
172+
$values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property);
173+
}
174+
$sanitized = $values;
175+
} else {
176+
$sanitized = (string)$data;
177+
}
178+
179+
return $sanitized;
180+
}
181+
182+
/**
183+
* Take value and turn it into a string suitable for inclusion in
184+
* the path, by url-encoding.
185+
* @param string $value a string which will be part of the path
186+
* @return string the serialized object
187+
*/
188+
public static function toPathValue($value) {
189+
return rawurlencode(self::toString($value));
190+
}
191+
192+
/**
193+
* Take value and turn it into a string suitable for inclusion in
194+
* the query, by imploding comma-separated if it's an object.
195+
* If it's a string, pass through unchanged. It will be url-encoded
196+
* later.
197+
* @param object $object an object to be serialized to a string
198+
* @return string the serialized object
199+
*/
200+
public static function toQueryValue($object) {
201+
if (is_array($object)) {
202+
return implode(',', $object);
203+
} else {
204+
return self::toString($object);
205+
}
206+
}
207+
208+
/**
209+
* Take value and turn it into a string suitable for inclusion in
210+
* the header. If it's a string, pass through unchanged
211+
* If it's a datetime object, format it in ISO8601
212+
* @param string $value a string which will be part of the header
213+
* @return string the header string
214+
*/
215+
public static function toHeaderValue($value) {
216+
return self::toString($value);
217+
}
218+
219+
/**
220+
* Take value and turn it into a string suitable for inclusion in
221+
* the http body (form parameter). If it's a string, pass through unchanged
222+
* If it's a datetime object, format it in ISO8601
223+
* @param string $value the value of the form parameter
224+
* @return string the form string
225+
*/
226+
public static function toFormValue($value) {
227+
return self::toString($value);
228+
}
229+
230+
/**
231+
* Take value and turn it into a string suitable for inclusion in
232+
* the parameter. If it's a string, pass through unchanged
233+
* If it's a datetime object, format it in ISO8601
234+
* @param string $value the value of the parameter
235+
* @return string the header string
236+
*/
237+
public static function toString($value) {
238+
if ($value instanceof \DateTime) { // datetime in ISO8601 format
239+
return $value->format(\DateTime::ISO8601);
240+
}
241+
else {
242+
return $value;
243+
}
244+
}
245+
246+
/**
247+
* Deserialize a JSON string into an object
248+
*
249+
* @param object $object object or primitive to be deserialized
250+
* @param string $class class name is passed as a string
251+
* @return object an instance of $class
252+
*/
253+
254+
public static function deserialize($data, $class)
255+
{
256+
if (null === $data) {
257+
$deserialized = null;
258+
} elseif (substr($class, 0, 4) == 'map[') {
259+
$inner = substr($class, 4, -1);
260+
$values = array();
261+
if(strrpos($inner, ",") !== false) {
262+
$subClass_array = explode(',', $inner, 2);
263+
$subClass = $subClass_array[1];
264+
foreach ($data as $key => $value) {
265+
$values[] = array($key => self::deserialize($value, $subClass));
266+
}
267+
}
268+
$deserialized = $values;
269+
} elseif (strcasecmp(substr($class, 0, 6),'array[') == 0) {
270+
$subClass = substr($class, 6, -1);
271+
$values = array();
272+
foreach ($data as $key => $value) {
273+
$values[] = self::deserialize($value, $subClass);
274+
}
275+
$deserialized = $values;
276+
} elseif ($class == 'DateTime') {
277+
$deserialized = new \DateTime($data);
278+
} elseif (in_array($class, array('string', 'int', 'float', 'bool'))) {
279+
settype($data, $class);
280+
$deserialized = $data;
281+
} else {
282+
$class = "{{invokerPackage}}\\models\\".$class;
283+
$instance = new $class();
284+
foreach ($instance::$swaggerTypes as $property => $type) {
285+
if (isset($data->$property)) {
286+
$original_property_name = $instance::$attributeMap[$property];
287+
$instance->$property = self::deserialize($data->$original_property_name, $type);
288+
}
289+
}
290+
$deserialized = $instance;
291+
}
292+
293+
return $deserialized;
294+
}
295+
296+
}
297+

0 commit comments

Comments
 (0)