PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 2.2.5
WooCommerce Square v2.2.5
5.5.0 5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / vendor / square / connect / lib / ApiClient.php
woocommerce-square / vendor / square / connect / lib Last commit date
Api 7 years ago Model 7 years ago ApiClient.php 7 years ago ApiException.php 7 years ago Configuration.php 7 years ago ObjectSerializer.php 7 years ago
ApiClient.php
357 lines
1 <?php
2 /**
3 * NOTE: This class is auto generated by the swagger code generator program.
4 * https://github.com/swagger-api/swagger-codegen
5 * Do not edit the class manually.
6 */
7
8 namespace SquareConnect;
9
10 /**
11 * ApiClient Class Doc Comment
12 *
13 * @category Class
14 * @package SquareConnect
15 * @author Square Inc.
16 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache Licene v2
17 * @link https://squareup.com/developers
18 */
19 class ApiClient
20 {
21
22 public static $PATCH = "PATCH";
23 public static $POST = "POST";
24 public static $GET = "GET";
25 public static $HEAD = "HEAD";
26 public static $OPTIONS = "OPTIONS";
27 public static $PUT = "PUT";
28 public static $DELETE = "DELETE";
29
30 /**
31 * Configuration
32 * @var Configuration
33 */
34 protected $config;
35
36 /**
37 * Object Serializer
38 * @var ObjectSerializer
39 */
40 protected $serializer;
41
42 /**
43 * Constructor of the class
44 * @param Configuration $config config for this ApiClient
45 */
46 public function __construct(Configuration $config = null)
47 {
48 if ($config == null) {
49 $config = Configuration::getDefaultConfiguration();
50 }
51
52 $this->config = $config;
53 $this->serializer = new ObjectSerializer();
54 }
55
56 /**
57 * Get the config
58 * @return Configuration
59 */
60 public function getConfig()
61 {
62 return $this->config;
63 }
64
65 /**
66 * Get the serializer
67 * @return ObjectSerializer
68 */
69 public function getSerializer()
70 {
71 return $this->serializer;
72 }
73
74 /**
75 * Get API key (with prefix if set)
76 * @param string $apiKeyIdentifier name of apikey
77 * @return string API key with the prefix
78 */
79 public function getApiKeyWithPrefix($apiKeyIdentifier)
80 {
81 $prefix = $this->config->getApiKeyPrefix($apiKeyIdentifier);
82 $apiKey = $this->config->getApiKey($apiKeyIdentifier);
83
84 if (!isset($apiKey)) {
85 return null;
86 }
87
88 if (isset($prefix)) {
89 $keyWithPrefix = $prefix." ".$apiKey;
90 } else {
91 $keyWithPrefix = $apiKey;
92 }
93
94 return $keyWithPrefix;
95 }
96
97 /**
98 * Make the HTTP call (Sync)
99 * @param string $resourcePath path to method endpoint
100 * @param string $method method to call
101 * @param array $queryParams parameters to be place in query URL
102 * @param array $postData parameters to be placed in POST body
103 * @param array $headerParams parameters to be place in request header
104 * @param string $responseType expected response type of the endpoint
105 * @throws \SquareConnect\ApiException on a non 2xx response
106 * @return mixed
107 */
108 public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams, $responseType = null)
109 {
110
111 $headers = array();
112
113 // construct the http header
114 $headerParams = array_merge(
115 (array)$this->config->getDefaultHeaders(),
116 (array)$headerParams
117 );
118
119 foreach ($headerParams as $key => $val) {
120 if (strcasecmp($key, "Authorization") == 0 ) {
121 if (strrpos($val, "Bearer", -strlen($val)) === false) {
122 $headers[] = "$key: Bearer $val";
123 continue;
124 }
125 }
126 $headers[] = "$key: $val";
127 }
128
129 // form data
130 if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers)) {
131 $postData = http_build_query($postData);
132 } elseif ((is_object($postData) or is_array($postData)) and !in_array('Content-Type: multipart/form-data', $headers)) { // json model
133 $postData = json_encode(\SquareConnect\ObjectSerializer::sanitizeForSerialization($postData));
134 }
135
136 $url = $this->config->getHost() . $resourcePath;
137
138 $curl = curl_init();
139 // set timeout, if needed
140 if ($this->config->getCurlTimeout() != 0) {
141 curl_setopt($curl, CURLOPT_TIMEOUT, $this->config->getCurlTimeout());
142 }
143 // return the result on success, rather than just true
144 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
145
146 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
147
148 // disable SSL verification, if needed
149 if ($this->config->getSSLVerification() == false) {
150 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
151 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
152 }
153
154 if (! empty($queryParams)) {
155 $url = ($url . '?' . http_build_query($queryParams));
156 }
157
158 if ($method == self::$POST) {
159 curl_setopt($curl, CURLOPT_POST, true);
160 curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
161 } elseif ($method == self::$HEAD) {
162 curl_setopt($curl, CURLOPT_NOBODY, true);
163 } elseif ($method == self::$OPTIONS) {
164 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "OPTIONS");
165 curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
166 } elseif ($method == self::$PATCH) {
167 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
168 curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
169 } elseif ($method == self::$PUT) {
170 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
171 curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
172 } elseif ($method == self::$DELETE) {
173 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
174 curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
175 } elseif ($method != self::$GET) {
176 throw new ApiException('Method ' . $method . ' is not recognized.');
177 }
178 curl_setopt($curl, CURLOPT_URL, $url);
179
180 // Set user agent
181 curl_setopt($curl, CURLOPT_USERAGENT, $this->config->getUserAgent());
182
183 // debugging for curl
184 if ($this->config->getDebug()) {
185 error_log("[DEBUG] HTTP Request body ~BEGIN~\n".print_r($postData, true)."\n~END~\n", 3, $this->config->getDebugFile());
186
187 curl_setopt($curl, CURLOPT_VERBOSE, 1);
188 curl_setopt($curl, CURLOPT_STDERR, fopen($this->config->getDebugFile(), 'a'));
189 } else {
190 curl_setopt($curl, CURLOPT_VERBOSE, 0);
191 }
192
193 // obtain the HTTP response headers
194 curl_setopt($curl, CURLOPT_HEADER, 1);
195
196 // Make the request
197 $response = curl_exec($curl);
198 $http_header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
199 $http_header = $this->http_parse_headers(substr($response, 0, $http_header_size));
200 $http_body = substr($response, $http_header_size);
201 $response_info = curl_getinfo($curl);
202
203 // debug HTTP response body
204 if ($this->config->getDebug()) {
205 error_log("[DEBUG] HTTP Response body ~BEGIN~\n".print_r($http_body, true)."\n~END~\n", 3, $this->config->getDebugFile());
206 }
207
208 // Handle the response
209 if ($response_info['http_code'] == 0) {
210 $curl_error_message = curl_error($curl);
211
212 // curl_exec can sometimes fail but still return a blank message from curl_error().
213 if (!empty($curl_error_message)) {
214 $error_message = "API call to $url failed: " . curl_error($curl);
215 } else {
216 $error_message = "API call to $url failed, but for an unknown reason. " .
217 "This could happen if you are disconnected from the network.";
218 }
219 throw new ApiException($error_message, 0, null, null);
220 } elseif ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
221 // return raw body if response is a file
222 if ($responseType == '\SplFileObject' || $responseType == 'ByteArray') {
223 return array($http_body, $response_info['http_code'], $http_header);
224 }
225
226 $data = json_decode($http_body);
227 if (json_last_error() > 0) { // if response is a string
228 $data = $http_body;
229 }
230 } else {
231 $data = json_decode($http_body);
232 if (json_last_error() > 0) { // if response is a string
233 $data = $http_body;
234 }
235
236 throw new ApiException(
237 "[{$http_header[0]}] $http_body",
238 $response_info['http_code'], $http_header, $data
239 );
240 }
241 return array($data, $response_info['http_code'], $http_header);
242 }
243
244 /**
245 * Return the header 'Accept' based on an array of Accept provided
246 *
247 * @param string[] $accept Array of header
248 *
249 * @return string Accept (e.g. application/json)
250 */
251 public static function selectHeaderAccept($accept)
252 {
253 if (count($accept) === 0 or (count($accept) === 1 and $accept[0] === '')) {
254 return null;
255 } elseif (preg_grep("/application\/json/i", $accept)) {
256 return 'application/json';
257 } else {
258 return implode(',', $accept);
259 }
260 }
261
262 /**
263 * Return the content type based on an array of content-type provided
264 *
265 * @param string[] $content_type Array fo content-type
266 *
267 * @return string Content-Type (e.g. application/json)
268 */
269 public static function selectHeaderContentType($content_type)
270 {
271 if (count($content_type) === 0 or (count($content_type) === 1 and $content_type[0] === '')) {
272 return 'application/json';
273 } elseif (preg_grep("/application\/json/i", $content_type)) {
274 return 'application/json';
275 } else {
276 return implode(',', $content_type);
277 }
278 }
279
280 /**
281 * Return a batch_token if present on the Link header or null if no token
282 * is present
283 *
284 * @param string[] Array of HTTP response heaers
285 *
286 * @return
287 */
288 public static function getV1BatchTokenFromHeaders($http_headers) {
289 if (is_array($http_headers) && isset($http_headers['Link']))
290 {
291 $connect_link_regexp = "/^<([^>]+)>;rel='next'$/";
292 if (preg_match($connect_link_regexp, $http_headers['Link'], $match) === 1)
293 {
294 $link_uri = $match[1];
295 if ($query = parse_url($link_uri, PHP_URL_QUERY))
296 {
297 parse_str($query, $query_params);
298 if (is_array($query_params) && isset($query_params['batch_token']))
299 {
300 return $query_params['batch_token'];
301 }
302 }
303 }
304 }
305
306 return null;
307 }
308
309 /**
310 * Return an array of HTTP response headers
311 *
312 * @param string $raw_headers A string of raw HTTP response headers
313 *
314 * @return string[] Array of HTTP response heaers
315 */
316 protected function http_parse_headers($raw_headers)
317 {
318 // ref/credit: http://php.net/manual/en/function.http-parse-headers.php#112986
319 $headers = array();
320 $key = ''; // [+]
321
322 foreach(explode("\n", $raw_headers) as $i => $h)
323 {
324 $h = explode(':', $h, 2);
325
326 if (isset($h[1]))
327 {
328 if (!isset($headers[$h[0]]))
329 $headers[$h[0]] = trim($h[1]);
330 elseif (is_array($headers[$h[0]]))
331 {
332 // $tmp = array_merge($headers[$h[0]], array(trim($h[1]))); // [-]
333 // $headers[$h[0]] = $tmp; // [-]
334 $headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1]))); // [+]
335 }
336 else
337 {
338 // $tmp = array_merge(array($headers[$h[0]]), array(trim($h[1]))); // [-]
339 // $headers[$h[0]] = $tmp; // [-]
340 $headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1]))); // [+]
341 }
342
343 $key = $h[0]; // [+]
344 }
345 else // [+]
346 { // [+]
347 if (substr($h[0], 0, 1) == "\t") // [+]
348 $headers[$key] .= "\r\n\t".trim($h[0]); // [+]
349 elseif (!$key) // [+]
350 $headers[0] = trim($h[0]);trim($h[0]); // [+]
351 } // [+]
352 }
353
354 return $headers;
355 }
356 }
357