1: | <?php |
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: | namespace Ally\PetStore\Api; |
29: | |
30: | use GuzzleHttp\Client; |
31: | use GuzzleHttp\ClientInterface; |
32: | use GuzzleHttp\Exception\ConnectException; |
33: | use GuzzleHttp\Exception\RequestException; |
34: | use GuzzleHttp\Psr7\MultipartStream; |
35: | use GuzzleHttp\Psr7\Request; |
36: | use GuzzleHttp\RequestOptions; |
37: | use Ally\PetStore\ApiException; |
38: | use Ally\PetStore\Configuration; |
39: | use Ally\PetStore\HeaderSelector; |
40: | use Ally\PetStore\ObjectSerializer; |
41: | |
42: | |
43: | |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | class StoreApi |
51: | { |
52: | |
53: | |
54: | |
55: | protected $client; |
56: | |
57: | |
58: | |
59: | |
60: | protected $config; |
61: | |
62: | |
63: | |
64: | |
65: | protected $headerSelector; |
66: | |
67: | |
68: | |
69: | |
70: | protected $hostIndex; |
71: | |
72: | |
73: | |
74: | |
75: | |
76: | |
77: | |
78: | public function __construct( |
79: | ClientInterface $client = null, |
80: | Configuration $config = null, |
81: | HeaderSelector $selector = null, |
82: | $hostIndex = 0 |
83: | ) { |
84: | $this->client = $client ?: new Client(); |
85: | $this->config = $config ?: new Configuration(); |
86: | $this->headerSelector = $selector ?: new HeaderSelector(); |
87: | $this->hostIndex = $hostIndex; |
88: | } |
89: | |
90: | |
91: | |
92: | |
93: | |
94: | |
95: | public function setHostIndex($hostIndex): void |
96: | { |
97: | $this->hostIndex = $hostIndex; |
98: | } |
99: | |
100: | |
101: | |
102: | |
103: | |
104: | |
105: | public function getHostIndex() |
106: | { |
107: | return $this->hostIndex; |
108: | } |
109: | |
110: | |
111: | |
112: | |
113: | public function getConfig() |
114: | { |
115: | return $this->config; |
116: | } |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | |
125: | |
126: | |
127: | |
128: | |
129: | public function deleteOrder($order_id) |
130: | { |
131: | $this->deleteOrderWithHttpInfo($order_id); |
132: | } |
133: | |
134: | |
135: | |
136: | |
137: | |
138: | |
139: | |
140: | |
141: | |
142: | |
143: | |
144: | |
145: | public function deleteOrderWithHttpInfo($order_id) |
146: | { |
147: | $request = $this->deleteOrderRequest($order_id); |
148: | |
149: | try { |
150: | $options = $this->createHttpClientOption(); |
151: | try { |
152: | $response = $this->client->send($request, $options); |
153: | } catch (RequestException $e) { |
154: | throw new ApiException( |
155: | "[{$e->getCode()}] {$e->getMessage()}", |
156: | (int) $e->getCode(), |
157: | $e->getResponse() ? $e->getResponse()->getHeaders() : null, |
158: | $e->getResponse() ? (string) $e->getResponse()->getBody() : null |
159: | ); |
160: | } catch (ConnectException $e) { |
161: | throw new ApiException( |
162: | "[{$e->getCode()}] {$e->getMessage()}", |
163: | (int) $e->getCode(), |
164: | null, |
165: | null |
166: | ); |
167: | } |
168: | |
169: | $statusCode = $response->getStatusCode(); |
170: | |
171: | if ($statusCode < 200 || $statusCode > 299) { |
172: | throw new ApiException( |
173: | sprintf( |
174: | '[%d] Error connecting to the API (%s)', |
175: | $statusCode, |
176: | (string) $request->getUri() |
177: | ), |
178: | $statusCode, |
179: | $response->getHeaders(), |
180: | (string) $response->getBody() |
181: | ); |
182: | } |
183: | |
184: | return [null, $statusCode, $response->getHeaders()]; |
185: | |
186: | } catch (ApiException $e) { |
187: | switch ($e->getCode()) { |
188: | } |
189: | throw $e; |
190: | } |
191: | } |
192: | |
193: | |
194: | |
195: | |
196: | |
197: | |
198: | |
199: | |
200: | |
201: | |
202: | |
203: | public function deleteOrderAsync($order_id) |
204: | { |
205: | return $this->deleteOrderAsyncWithHttpInfo($order_id) |
206: | ->then( |
207: | function ($response) { |
208: | return $response[0]; |
209: | } |
210: | ); |
211: | } |
212: | |
213: | |
214: | |
215: | |
216: | |
217: | |
218: | |
219: | |
220: | |
221: | |
222: | |
223: | public function deleteOrderAsyncWithHttpInfo($order_id) |
224: | { |
225: | $returnType = ''; |
226: | $request = $this->deleteOrderRequest($order_id); |
227: | |
228: | return $this->client |
229: | ->sendAsync($request, $this->createHttpClientOption()) |
230: | ->then( |
231: | function ($response) use ($returnType) { |
232: | return [null, $response->getStatusCode(), $response->getHeaders()]; |
233: | }, |
234: | function ($exception) { |
235: | $response = $exception->getResponse(); |
236: | $statusCode = $response->getStatusCode(); |
237: | throw new ApiException( |
238: | sprintf( |
239: | '[%d] Error connecting to the API (%s)', |
240: | $statusCode, |
241: | $exception->getRequest()->getUri() |
242: | ), |
243: | $statusCode, |
244: | $response->getHeaders(), |
245: | (string) $response->getBody() |
246: | ); |
247: | } |
248: | ); |
249: | } |
250: | |
251: | |
252: | |
253: | |
254: | |
255: | |
256: | |
257: | |
258: | |
259: | public function deleteOrderRequest($order_id) |
260: | { |
261: | |
262: | |
263: | if ($order_id === null || (is_array($order_id) && count($order_id) === 0)) { |
264: | throw new \InvalidArgumentException( |
265: | 'Missing the required parameter $order_id when calling deleteOrder' |
266: | ); |
267: | } |
268: | |
269: | $resourcePath = '/store/order/{orderId}'; |
270: | $formParams = []; |
271: | $queryParams = []; |
272: | $headerParams = []; |
273: | $httpBody = ''; |
274: | $multipart = false; |
275: | |
276: | |
277: | |
278: | |
279: | if ($order_id !== null) { |
280: | $resourcePath = str_replace( |
281: | '{' . 'orderId' . '}', |
282: | ObjectSerializer::toPathValue($order_id), |
283: | $resourcePath |
284: | ); |
285: | } |
286: | |
287: | |
288: | if ($multipart) { |
289: | $headers = $this->headerSelector->selectHeadersForMultipart( |
290: | [] |
291: | ); |
292: | } else { |
293: | $headers = $this->headerSelector->selectHeaders( |
294: | [], |
295: | [] |
296: | ); |
297: | } |
298: | |
299: | |
300: | if (count($formParams) > 0) { |
301: | if ($multipart) { |
302: | $multipartContents = []; |
303: | foreach ($formParams as $formParamName => $formParamValue) { |
304: | $formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue]; |
305: | foreach ($formParamValueItems as $formParamValueItem) { |
306: | $multipartContents[] = [ |
307: | 'name' => $formParamName, |
308: | 'contents' => $formParamValueItem |
309: | ]; |
310: | } |
311: | } |
312: | |
313: | $httpBody = new MultipartStream($multipartContents); |
314: | |
315: | } elseif ($headers['Content-Type'] === 'application/json') { |
316: | $httpBody = \GuzzleHttp\json_encode($formParams); |
317: | |
318: | } else { |
319: | |
320: | $httpBody = ObjectSerializer::buildQuery($formParams); |
321: | } |
322: | } |
323: | |
324: | |
325: | $defaultHeaders = []; |
326: | if ($this->config->getUserAgent()) { |
327: | $defaultHeaders['User-Agent'] = $this->config->getUserAgent(); |
328: | } |
329: | |
330: | $headers = array_merge( |
331: | $defaultHeaders, |
332: | $headerParams, |
333: | $headers |
334: | ); |
335: | |
336: | $operationHost = $this->config->getHost(); |
337: | $query = ObjectSerializer::buildQuery($queryParams); |
338: | return new Request( |
339: | 'DELETE', |
340: | $operationHost . $resourcePath . ($query ? "?{$query}" : ''), |
341: | $headers, |
342: | $httpBody |
343: | ); |
344: | } |
345: | |
346: | |
347: | |
348: | |
349: | |
350: | |
351: | |
352: | |
353: | |
354: | |
355: | |
356: | public function getInventory() |
357: | { |
358: | list($response) = $this->getInventoryWithHttpInfo(); |
359: | return $response; |
360: | } |
361: | |
362: | |
363: | |
364: | |
365: | |
366: | |
367: | |
368: | |
369: | |
370: | |
371: | |
372: | public function getInventoryWithHttpInfo() |
373: | { |
374: | $request = $this->getInventoryRequest(); |
375: | |
376: | try { |
377: | $options = $this->createHttpClientOption(); |
378: | try { |
379: | $response = $this->client->send($request, $options); |
380: | } catch (RequestException $e) { |
381: | throw new ApiException( |
382: | "[{$e->getCode()}] {$e->getMessage()}", |
383: | (int) $e->getCode(), |
384: | $e->getResponse() ? $e->getResponse()->getHeaders() : null, |
385: | $e->getResponse() ? (string) $e->getResponse()->getBody() : null |
386: | ); |
387: | } catch (ConnectException $e) { |
388: | throw new ApiException( |
389: | "[{$e->getCode()}] {$e->getMessage()}", |
390: | (int) $e->getCode(), |
391: | null, |
392: | null |
393: | ); |
394: | } |
395: | |
396: | $statusCode = $response->getStatusCode(); |
397: | |
398: | if ($statusCode < 200 || $statusCode > 299) { |
399: | throw new ApiException( |
400: | sprintf( |
401: | '[%d] Error connecting to the API (%s)', |
402: | $statusCode, |
403: | (string) $request->getUri() |
404: | ), |
405: | $statusCode, |
406: | $response->getHeaders(), |
407: | (string) $response->getBody() |
408: | ); |
409: | } |
410: | |
411: | switch($statusCode) { |
412: | case 200: |
413: | if ('array<string,int>' === '\SplFileObject') { |
414: | $content = $response->getBody(); |
415: | } else { |
416: | $content = (string) $response->getBody(); |
417: | if ('array<string,int>' !== 'string') { |
418: | $content = json_decode($content); |
419: | } |
420: | } |
421: | |
422: | return [ |
423: | ObjectSerializer::deserialize($content, 'array<string,int>', []), |
424: | $response->getStatusCode(), |
425: | $response->getHeaders() |
426: | ]; |
427: | } |
428: | |
429: | $returnType = 'array<string,int>'; |
430: | if ($returnType === '\SplFileObject') { |
431: | $content = $response->getBody(); |
432: | } else { |
433: | $content = (string) $response->getBody(); |
434: | if ($returnType !== 'string') { |
435: | $content = json_decode($content); |
436: | } |
437: | } |
438: | |
439: | return [ |
440: | ObjectSerializer::deserialize($content, $returnType, []), |
441: | $response->getStatusCode(), |
442: | $response->getHeaders() |
443: | ]; |
444: | |
445: | } catch (ApiException $e) { |
446: | switch ($e->getCode()) { |
447: | case 200: |
448: | $data = ObjectSerializer::deserialize( |
449: | $e->getResponseBody(), |
450: | 'array<string,int>', |
451: | $e->getResponseHeaders() |
452: | ); |
453: | $e->setResponseObject($data); |
454: | break; |
455: | } |
456: | throw $e; |
457: | } |
458: | } |
459: | |
460: | |
461: | |
462: | |
463: | |
464: | |
465: | |
466: | |
467: | |
468: | |
469: | public function getInventoryAsync() |
470: | { |
471: | return $this->getInventoryAsyncWithHttpInfo() |
472: | ->then( |
473: | function ($response) { |
474: | return $response[0]; |
475: | } |
476: | ); |
477: | } |
478: | |
479: | |
480: | |
481: | |
482: | |
483: | |
484: | |
485: | |
486: | |
487: | |
488: | public function getInventoryAsyncWithHttpInfo() |
489: | { |
490: | $returnType = 'array<string,int>'; |
491: | $request = $this->getInventoryRequest(); |
492: | |
493: | return $this->client |
494: | ->sendAsync($request, $this->createHttpClientOption()) |
495: | ->then( |
496: | function ($response) use ($returnType) { |
497: | if ($returnType === '\SplFileObject') { |
498: | $content = $response->getBody(); |
499: | } else { |
500: | $content = (string) $response->getBody(); |
501: | if ($returnType !== 'string') { |
502: | $content = json_decode($content); |
503: | } |
504: | } |
505: | |
506: | return [ |
507: | ObjectSerializer::deserialize($content, $returnType, []), |
508: | $response->getStatusCode(), |
509: | $response->getHeaders() |
510: | ]; |
511: | }, |
512: | function ($exception) { |
513: | $response = $exception->getResponse(); |
514: | $statusCode = $response->getStatusCode(); |
515: | throw new ApiException( |
516: | sprintf( |
517: | '[%d] Error connecting to the API (%s)', |
518: | $statusCode, |
519: | $exception->getRequest()->getUri() |
520: | ), |
521: | $statusCode, |
522: | $response->getHeaders(), |
523: | (string) $response->getBody() |
524: | ); |
525: | } |
526: | ); |
527: | } |
528: | |
529: | |
530: | |
531: | |
532: | |
533: | |
534: | |
535: | |
536: | public function getInventoryRequest() |
537: | { |
538: | |
539: | $resourcePath = '/store/inventory'; |
540: | $formParams = []; |
541: | $queryParams = []; |
542: | $headerParams = []; |
543: | $httpBody = ''; |
544: | $multipart = false; |
545: | |
546: | |
547: | |
548: | |
549: | |
550: | if ($multipart) { |
551: | $headers = $this->headerSelector->selectHeadersForMultipart( |
552: | ['application/json'] |
553: | ); |
554: | } else { |
555: | $headers = $this->headerSelector->selectHeaders( |
556: | ['application/json'], |
557: | [] |
558: | ); |
559: | } |
560: | |
561: | |
562: | if (count($formParams) > 0) { |
563: | if ($multipart) { |
564: | $multipartContents = []; |
565: | foreach ($formParams as $formParamName => $formParamValue) { |
566: | $formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue]; |
567: | foreach ($formParamValueItems as $formParamValueItem) { |
568: | $multipartContents[] = [ |
569: | 'name' => $formParamName, |
570: | 'contents' => $formParamValueItem |
571: | ]; |
572: | } |
573: | } |
574: | |
575: | $httpBody = new MultipartStream($multipartContents); |
576: | |
577: | } elseif ($headers['Content-Type'] === 'application/json') { |
578: | $httpBody = \GuzzleHttp\json_encode($formParams); |
579: | |
580: | } else { |
581: | |
582: | $httpBody = ObjectSerializer::buildQuery($formParams); |
583: | } |
584: | } |
585: | |
586: | |
587: | $apiKey = $this->config->getApiKeyWithPrefix('api_key'); |
588: | if ($apiKey !== null) { |
589: | $headers['api_key'] = $apiKey; |
590: | } |
591: | |
592: | $defaultHeaders = []; |
593: | if ($this->config->getUserAgent()) { |
594: | $defaultHeaders['User-Agent'] = $this->config->getUserAgent(); |
595: | } |
596: | |
597: | $headers = array_merge( |
598: | $defaultHeaders, |
599: | $headerParams, |
600: | $headers |
601: | ); |
602: | |
603: | $operationHost = $this->config->getHost(); |
604: | $query = ObjectSerializer::buildQuery($queryParams); |
605: | return new Request( |
606: | 'GET', |
607: | $operationHost . $resourcePath . ($query ? "?{$query}" : ''), |
608: | $headers, |
609: | $httpBody |
610: | ); |
611: | } |
612: | |
613: | |
614: | |
615: | |
616: | |
617: | |
618: | |
619: | |
620: | |
621: | |
622: | |
623: | |
624: | public function getOrderById($order_id) |
625: | { |
626: | list($response) = $this->getOrderByIdWithHttpInfo($order_id); |
627: | return $response; |
628: | } |
629: | |
630: | |
631: | |
632: | |
633: | |
634: | |
635: | |
636: | |
637: | |
638: | |
639: | |
640: | |
641: | public function getOrderByIdWithHttpInfo($order_id) |
642: | { |
643: | $request = $this->getOrderByIdRequest($order_id); |
644: | |
645: | try { |
646: | $options = $this->createHttpClientOption(); |
647: | try { |
648: | $response = $this->client->send($request, $options); |
649: | } catch (RequestException $e) { |
650: | throw new ApiException( |
651: | "[{$e->getCode()}] {$e->getMessage()}", |
652: | (int) $e->getCode(), |
653: | $e->getResponse() ? $e->getResponse()->getHeaders() : null, |
654: | $e->getResponse() ? (string) $e->getResponse()->getBody() : null |
655: | ); |
656: | } catch (ConnectException $e) { |
657: | throw new ApiException( |
658: | "[{$e->getCode()}] {$e->getMessage()}", |
659: | (int) $e->getCode(), |
660: | null, |
661: | null |
662: | ); |
663: | } |
664: | |
665: | $statusCode = $response->getStatusCode(); |
666: | |
667: | if ($statusCode < 200 || $statusCode > 299) { |
668: | throw new ApiException( |
669: | sprintf( |
670: | '[%d] Error connecting to the API (%s)', |
671: | $statusCode, |
672: | (string) $request->getUri() |
673: | ), |
674: | $statusCode, |
675: | $response->getHeaders(), |
676: | (string) $response->getBody() |
677: | ); |
678: | } |
679: | |
680: | switch($statusCode) { |
681: | case 200: |
682: | if ('\Ally\PetStore\Schema\Order' === '\SplFileObject') { |
683: | $content = $response->getBody(); |
684: | } else { |
685: | $content = (string) $response->getBody(); |
686: | if ('\Ally\PetStore\Schema\Order' !== 'string') { |
687: | $content = json_decode($content); |
688: | } |
689: | } |
690: | |
691: | return [ |
692: | ObjectSerializer::deserialize($content, '\Ally\PetStore\Schema\Order', []), |
693: | $response->getStatusCode(), |
694: | $response->getHeaders() |
695: | ]; |
696: | } |
697: | |
698: | $returnType = '\Ally\PetStore\Schema\Order'; |
699: | if ($returnType === '\SplFileObject') { |
700: | $content = $response->getBody(); |
701: | } else { |
702: | $content = (string) $response->getBody(); |
703: | if ($returnType !== 'string') { |
704: | $content = json_decode($content); |
705: | } |
706: | } |
707: | |
708: | return [ |
709: | ObjectSerializer::deserialize($content, $returnType, []), |
710: | $response->getStatusCode(), |
711: | $response->getHeaders() |
712: | ]; |
713: | |
714: | } catch (ApiException $e) { |
715: | switch ($e->getCode()) { |
716: | case 200: |
717: | $data = ObjectSerializer::deserialize( |
718: | $e->getResponseBody(), |
719: | '\Ally\PetStore\Schema\Order', |
720: | $e->getResponseHeaders() |
721: | ); |
722: | $e->setResponseObject($data); |
723: | break; |
724: | } |
725: | throw $e; |
726: | } |
727: | } |
728: | |
729: | |
730: | |
731: | |
732: | |
733: | |
734: | |
735: | |
736: | |
737: | |
738: | |
739: | public function getOrderByIdAsync($order_id) |
740: | { |
741: | return $this->getOrderByIdAsyncWithHttpInfo($order_id) |
742: | ->then( |
743: | function ($response) { |
744: | return $response[0]; |
745: | } |
746: | ); |
747: | } |
748: | |
749: | |
750: | |
751: | |
752: | |
753: | |
754: | |
755: | |
756: | |
757: | |
758: | |
759: | public function getOrderByIdAsyncWithHttpInfo($order_id) |
760: | { |
761: | $returnType = '\Ally\PetStore\Schema\Order'; |
762: | $request = $this->getOrderByIdRequest($order_id); |
763: | |
764: | return $this->client |
765: | ->sendAsync($request, $this->createHttpClientOption()) |
766: | ->then( |
767: | function ($response) use ($returnType) { |
768: | if ($returnType === '\SplFileObject') { |
769: | $content = $response->getBody(); |
770: | } else { |
771: | $content = (string) $response->getBody(); |
772: | if ($returnType !== 'string') { |
773: | $content = json_decode($content); |
774: | } |
775: | } |
776: | |
777: | return [ |
778: | ObjectSerializer::deserialize($content, $returnType, []), |
779: | $response->getStatusCode(), |
780: | $response->getHeaders() |
781: | ]; |
782: | }, |
783: | function ($exception) { |
784: | $response = $exception->getResponse(); |
785: | $statusCode = $response->getStatusCode(); |
786: | throw new ApiException( |
787: | sprintf( |
788: | '[%d] Error connecting to the API (%s)', |
789: | $statusCode, |
790: | $exception->getRequest()->getUri() |
791: | ), |
792: | $statusCode, |
793: | $response->getHeaders(), |
794: | (string) $response->getBody() |
795: | ); |
796: | } |
797: | ); |
798: | } |
799: | |
800: | |
801: | |
802: | |
803: | |
804: | |
805: | |
806: | |
807: | |
808: | public function getOrderByIdRequest($order_id) |
809: | { |
810: | |
811: | |
812: | if ($order_id === null || (is_array($order_id) && count($order_id) === 0)) { |
813: | throw new \InvalidArgumentException( |
814: | 'Missing the required parameter $order_id when calling getOrderById' |
815: | ); |
816: | } |
817: | if ($order_id > 5) { |
818: | throw new \InvalidArgumentException('invalid value for "$order_id" when calling StoreApi.getOrderById, must be smaller than or equal to 5.'); |
819: | } |
820: | if ($order_id < 1) { |
821: | throw new \InvalidArgumentException('invalid value for "$order_id" when calling StoreApi.getOrderById, must be bigger than or equal to 1.'); |
822: | } |
823: | |
824: | |
825: | $resourcePath = '/store/order/{orderId}'; |
826: | $formParams = []; |
827: | $queryParams = []; |
828: | $headerParams = []; |
829: | $httpBody = ''; |
830: | $multipart = false; |
831: | |
832: | |
833: | |
834: | |
835: | if ($order_id !== null) { |
836: | $resourcePath = str_replace( |
837: | '{' . 'orderId' . '}', |
838: | ObjectSerializer::toPathValue($order_id), |
839: | $resourcePath |
840: | ); |
841: | } |
842: | |
843: | |
844: | if ($multipart) { |
845: | $headers = $this->headerSelector->selectHeadersForMultipart( |
846: | ['application/xml', 'application/json'] |
847: | ); |
848: | } else { |
849: | $headers = $this->headerSelector->selectHeaders( |
850: | ['application/xml', 'application/json'], |
851: | [] |
852: | ); |
853: | } |
854: | |
855: | |
856: | if (count($formParams) > 0) { |
857: | if ($multipart) { |
858: | $multipartContents = []; |
859: | foreach ($formParams as $formParamName => $formParamValue) { |
860: | $formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue]; |
861: | foreach ($formParamValueItems as $formParamValueItem) { |
862: | $multipartContents[] = [ |
863: | 'name' => $formParamName, |
864: | 'contents' => $formParamValueItem |
865: | ]; |
866: | } |
867: | } |
868: | |
869: | $httpBody = new MultipartStream($multipartContents); |
870: | |
871: | } elseif ($headers['Content-Type'] === 'application/json') { |
872: | $httpBody = \GuzzleHttp\json_encode($formParams); |
873: | |
874: | } else { |
875: | |
876: | $httpBody = ObjectSerializer::buildQuery($formParams); |
877: | } |
878: | } |
879: | |
880: | |
881: | $defaultHeaders = []; |
882: | if ($this->config->getUserAgent()) { |
883: | $defaultHeaders['User-Agent'] = $this->config->getUserAgent(); |
884: | } |
885: | |
886: | $headers = array_merge( |
887: | $defaultHeaders, |
888: | $headerParams, |
889: | $headers |
890: | ); |
891: | |
892: | $operationHost = $this->config->getHost(); |
893: | $query = ObjectSerializer::buildQuery($queryParams); |
894: | return new Request( |
895: | 'GET', |
896: | $operationHost . $resourcePath . ($query ? "?{$query}" : ''), |
897: | $headers, |
898: | $httpBody |
899: | ); |
900: | } |
901: | |
902: | |
903: | |
904: | |
905: | |
906: | |
907: | |
908: | |
909: | |
910: | |
911: | |
912: | |
913: | public function placeOrder($order) |
914: | { |
915: | list($response) = $this->placeOrderWithHttpInfo($order); |
916: | return $response; |
917: | } |
918: | |
919: | |
920: | |
921: | |
922: | |
923: | |
924: | |
925: | |
926: | |
927: | |
928: | |
929: | |
930: | public function placeOrderWithHttpInfo($order) |
931: | { |
932: | $request = $this->placeOrderRequest($order); |
933: | |
934: | try { |
935: | $options = $this->createHttpClientOption(); |
936: | try { |
937: | $response = $this->client->send($request, $options); |
938: | } catch (RequestException $e) { |
939: | throw new ApiException( |
940: | "[{$e->getCode()}] {$e->getMessage()}", |
941: | (int) $e->getCode(), |
942: | $e->getResponse() ? $e->getResponse()->getHeaders() : null, |
943: | $e->getResponse() ? (string) $e->getResponse()->getBody() : null |
944: | ); |
945: | } catch (ConnectException $e) { |
946: | throw new ApiException( |
947: | "[{$e->getCode()}] {$e->getMessage()}", |
948: | (int) $e->getCode(), |
949: | null, |
950: | null |
951: | ); |
952: | } |
953: | |
954: | $statusCode = $response->getStatusCode(); |
955: | |
956: | if ($statusCode < 200 || $statusCode > 299) { |
957: | throw new ApiException( |
958: | sprintf( |
959: | '[%d] Error connecting to the API (%s)', |
960: | $statusCode, |
961: | (string) $request->getUri() |
962: | ), |
963: | $statusCode, |
964: | $response->getHeaders(), |
965: | (string) $response->getBody() |
966: | ); |
967: | } |
968: | |
969: | switch($statusCode) { |
970: | case 200: |
971: | if ('\Ally\PetStore\Schema\Order' === '\SplFileObject') { |
972: | $content = $response->getBody(); |
973: | } else { |
974: | $content = (string) $response->getBody(); |
975: | if ('\Ally\PetStore\Schema\Order' !== 'string') { |
976: | $content = json_decode($content); |
977: | } |
978: | } |
979: | |
980: | return [ |
981: | ObjectSerializer::deserialize($content, '\Ally\PetStore\Schema\Order', []), |
982: | $response->getStatusCode(), |
983: | $response->getHeaders() |
984: | ]; |
985: | } |
986: | |
987: | $returnType = '\Ally\PetStore\Schema\Order'; |
988: | if ($returnType === '\SplFileObject') { |
989: | $content = $response->getBody(); |
990: | } else { |
991: | $content = (string) $response->getBody(); |
992: | if ($returnType !== 'string') { |
993: | $content = json_decode($content); |
994: | } |
995: | } |
996: | |
997: | return [ |
998: | ObjectSerializer::deserialize($content, $returnType, []), |
999: | $response->getStatusCode(), |
1000: | $response->getHeaders() |
1001: | ]; |
1002: | |
1003: | } catch (ApiException $e) { |
1004: | switch ($e->getCode()) { |
1005: | case 200: |
1006: | $data = ObjectSerializer::deserialize( |
1007: | $e->getResponseBody(), |
1008: | '\Ally\PetStore\Schema\Order', |
1009: | $e->getResponseHeaders() |
1010: | ); |
1011: | $e->setResponseObject($data); |
1012: | break; |
1013: | } |
1014: | throw $e; |
1015: | } |
1016: | } |
1017: | |
1018: | |
1019: | |
1020: | |
1021: | |
1022: | |
1023: | |
1024: | |
1025: | |
1026: | |
1027: | |
1028: | public function placeOrderAsync($order) |
1029: | { |
1030: | return $this->placeOrderAsyncWithHttpInfo($order) |
1031: | ->then( |
1032: | function ($response) { |
1033: | return $response[0]; |
1034: | } |
1035: | ); |
1036: | } |
1037: | |
1038: | |
1039: | |
1040: | |
1041: | |
1042: | |
1043: | |
1044: | |
1045: | |
1046: | |
1047: | |
1048: | public function placeOrderAsyncWithHttpInfo($order) |
1049: | { |
1050: | $returnType = '\Ally\PetStore\Schema\Order'; |
1051: | $request = $this->placeOrderRequest($order); |
1052: | |
1053: | return $this->client |
1054: | ->sendAsync($request, $this->createHttpClientOption()) |
1055: | ->then( |
1056: | function ($response) use ($returnType) { |
1057: | if ($returnType === '\SplFileObject') { |
1058: | $content = $response->getBody(); |
1059: | } else { |
1060: | $content = (string) $response->getBody(); |
1061: | if ($returnType !== 'string') { |
1062: | $content = json_decode($content); |
1063: | } |
1064: | } |
1065: | |
1066: | return [ |
1067: | ObjectSerializer::deserialize($content, $returnType, []), |
1068: | $response->getStatusCode(), |
1069: | $response->getHeaders() |
1070: | ]; |
1071: | }, |
1072: | function ($exception) { |
1073: | $response = $exception->getResponse(); |
1074: | $statusCode = $response->getStatusCode(); |
1075: | throw new ApiException( |
1076: | sprintf( |
1077: | '[%d] Error connecting to the API (%s)', |
1078: | $statusCode, |
1079: | $exception->getRequest()->getUri() |
1080: | ), |
1081: | $statusCode, |
1082: | $response->getHeaders(), |
1083: | (string) $response->getBody() |
1084: | ); |
1085: | } |
1086: | ); |
1087: | } |
1088: | |
1089: | |
1090: | |
1091: | |
1092: | |
1093: | |
1094: | |
1095: | |
1096: | |
1097: | public function placeOrderRequest($order) |
1098: | { |
1099: | |
1100: | |
1101: | if ($order === null || (is_array($order) && count($order) === 0)) { |
1102: | throw new \InvalidArgumentException( |
1103: | 'Missing the required parameter $order when calling placeOrder' |
1104: | ); |
1105: | } |
1106: | |
1107: | $resourcePath = '/store/order'; |
1108: | $formParams = []; |
1109: | $queryParams = []; |
1110: | $headerParams = []; |
1111: | $httpBody = ''; |
1112: | $multipart = false; |
1113: | |
1114: | |
1115: | |
1116: | |
1117: | |
1118: | if ($multipart) { |
1119: | $headers = $this->headerSelector->selectHeadersForMultipart( |
1120: | ['application/xml', 'application/json'] |
1121: | ); |
1122: | } else { |
1123: | $headers = $this->headerSelector->selectHeaders( |
1124: | ['application/xml', 'application/json'], |
1125: | ['application/json'] |
1126: | ); |
1127: | } |
1128: | |
1129: | |
1130: | if (isset($order)) { |
1131: | if ($headers['Content-Type'] === 'application/json') { |
1132: | $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($order)); |
1133: | } else { |
1134: | $httpBody = $order; |
1135: | } |
1136: | } elseif (count($formParams) > 0) { |
1137: | if ($multipart) { |
1138: | $multipartContents = []; |
1139: | foreach ($formParams as $formParamName => $formParamValue) { |
1140: | $formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue]; |
1141: | foreach ($formParamValueItems as $formParamValueItem) { |
1142: | $multipartContents[] = [ |
1143: | 'name' => $formParamName, |
1144: | 'contents' => $formParamValueItem |
1145: | ]; |
1146: | } |
1147: | } |
1148: | |
1149: | $httpBody = new MultipartStream($multipartContents); |
1150: | |
1151: | } elseif ($headers['Content-Type'] === 'application/json') { |
1152: | $httpBody = \GuzzleHttp\json_encode($formParams); |
1153: | |
1154: | } else { |
1155: | |
1156: | $httpBody = ObjectSerializer::buildQuery($formParams); |
1157: | } |
1158: | } |
1159: | |
1160: | |
1161: | $defaultHeaders = []; |
1162: | if ($this->config->getUserAgent()) { |
1163: | $defaultHeaders['User-Agent'] = $this->config->getUserAgent(); |
1164: | } |
1165: | |
1166: | $headers = array_merge( |
1167: | $defaultHeaders, |
1168: | $headerParams, |
1169: | $headers |
1170: | ); |
1171: | |
1172: | $operationHost = $this->config->getHost(); |
1173: | $query = ObjectSerializer::buildQuery($queryParams); |
1174: | return new Request( |
1175: | 'POST', |
1176: | $operationHost . $resourcePath . ($query ? "?{$query}" : ''), |
1177: | $headers, |
1178: | $httpBody |
1179: | ); |
1180: | } |
1181: | |
1182: | |
1183: | |
1184: | |
1185: | |
1186: | |
1187: | |
1188: | protected function createHttpClientOption() |
1189: | { |
1190: | $options = []; |
1191: | if ($this->config->getDebug()) { |
1192: | $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); |
1193: | if (!$options[RequestOptions::DEBUG]) { |
1194: | throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); |
1195: | } |
1196: | } |
1197: | |
1198: | return $options; |
1199: | } |
1200: | } |
1201: | |