1: <?php
2: namespace Inventorus;
3:
4: use Inventorus\APIException;
5: use Inventorus\APIHelper;
6: use Inventorus\Configuration;
7: use Inventorus\Models;
8: use Inventorus\Http\HttpRequest;
9: use Inventorus\Http\HttpResponse;
10: use Inventorus\Http\HttpMethod;
11: use Inventorus\Http\HttpContext;
12: use Inventorus\Http\HttpCallBack;
13: use Unirest\Request;
14:
15: 16: 17:
18: class InventorusClient {
19:
20: 21: 22: 23:
24: private $httpCallBack = null;
25:
26: 27: 28:
29: public function __construct($apiKey = NULL)
30: {
31: Configuration::$apiKey = $apiKey ? $apiKey : Configuration::$apiKey;
32: }
33:
34: 35: 36: 37:
38: public function setHttpCallBack(HttpCallBack $httpCallBack)
39: {
40: $this->httpCallBack = $httpCallBack;
41: }
42:
43: 44: 45: 46:
47: public function getHttpCallBack()
48: {
49: return $this->httpCallBack;
50: }
51:
52: 53: 54: 55: 56:
57: public function getExportHistory ()
58: {
59:
60: $_queryBuilder = Configuration::$BASEURI;
61:
62:
63: $_queryBuilder = $_queryBuilder.'/warehouse/exports/history';
64:
65:
66: APIHelper::appendUrlWithQueryParameters($_queryBuilder, array (
67: 'apiKey' => Configuration::$apiKey,
68: ));
69:
70:
71: $_queryUrl = APIHelper::cleanUrl($_queryBuilder);
72:
73:
74: $_headers = array (
75: 'user-agent' => 'APIMATIC 2.0',
76: 'Accept' => 'application/json'
77: );
78:
79:
80: $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl);
81: if($this->getHttpCallBack() != null) {
82: $this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
83: }
84:
85:
86: $response = Request::get($_queryUrl, $_headers);
87:
88:
89: if($this->getHttpCallBack() != null) {
90: $_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
91: $_httpContext = new HttpContext($_httpRequest, $_httpResponse);
92:
93: $this->getHttpCallBack()->callOnAfterRequest($_httpContext);
94: }
95:
96: if (!isset($response->body->status)) {
97: throw new APIException('Unexpected error in API call. See HTTP response body for details.', $response->code, $response->body);
98: }
99: else if ($response->body->status == 'error') {
100: throw new APIException('API responded with error status.', $response->code, $response->body);
101: }
102:
103: return $response->body->response->data->ids;
104: }
105:
106: 107: 108: 109: 110: 111: 112: 113:
114: public function getUsersItems ($steamid, $appid, $contextids = NULL)
115: {
116:
117: $_queryBuilder = Configuration::$BASEURI;
118:
119:
120: $_queryBuilder = $_queryBuilder.'/steam/items/{steamid}/{appid}';
121:
122:
123: APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array (
124: 'steamid' => $steamid,
125: 'appid' => $appid,
126: ));
127:
128:
129: APIHelper::appendUrlWithQueryParameters($_queryBuilder, array (
130: 'contextids' => $contextids,
131: 'apiKey' => Configuration::$apiKey,
132: ));
133:
134:
135: $_queryUrl = APIHelper::cleanUrl($_queryBuilder);
136:
137:
138: $_headers = array (
139: 'user-agent' => 'APIMATIC 2.0',
140: 'Accept' => 'application/json'
141: );
142:
143:
144: $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl);
145: if ($this->getHttpCallBack() != null) {
146: $this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
147: }
148:
149:
150: $response = Request::get($_queryUrl, $_headers);
151:
152:
153: if($this->getHttpCallBack() != null) {
154: $_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
155: $_httpContext = new HttpContext($_httpRequest, $_httpResponse);
156:
157: $this->getHttpCallBack()->callOnAfterRequest($_httpContext);
158: }
159:
160: if (!isset($response->body->status)) {
161: throw new APIException('Unexpected error in API call. See HTTP response body for details.', $response->code, $response->body);
162: }
163: else if ($response->body->status == 'error') {
164: throw new APIException('API responded with error status.', $response->code, $response->body);
165: }
166:
167: return array(
168: $response->body->response->data->inventory,
169: $response->body->response->data->currency
170: );
171: }
172:
173: 174: 175: 176: 177: 178: 179:
180: public function getUsersApps ($steamid, $includeEmpty = NULL)
181: {
182:
183: $_queryBuilder = Configuration::$BASEURI;
184:
185:
186: $_queryBuilder = $_queryBuilder.'/steam/apps/{steamid}';
187:
188:
189: APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array (
190: 'steamid' => $steamid,
191: ));
192:
193:
194: APIHelper::appendUrlWithQueryParameters($_queryBuilder, array (
195: 'includeEmpty' => var_export($includeEmpty, true),
196: 'apiKey' => Configuration::$apiKey,
197: ));
198:
199:
200: $_queryUrl = APIHelper::cleanUrl($_queryBuilder);
201:
202:
203: $_headers = array (
204: 'user-agent' => 'APIMATIC 2.0',
205: 'Accept' => 'application/json'
206: );
207:
208:
209: $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl);
210: if($this->getHttpCallBack() != null) {
211: $this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
212: }
213:
214:
215: $response = Request::get($_queryUrl, $_headers);
216:
217:
218: if($this->getHttpCallBack() != null) {
219: $_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
220: $_httpContext = new HttpContext($_httpRequest, $_httpResponse);
221:
222: $this->getHttpCallBack()->callOnAfterRequest($_httpContext);
223: }
224:
225: if (!isset($response->body->status)) {
226: throw new APIException('Unexpected error in API call. See HTTP response body for details.', $response->code, $response->body);
227: }
228: else if ($response->body->status == 'error') {
229: throw new APIException('API responded with error status.', $response->code, $response->body);
230: }
231:
232: return $response->body->response->data->apps;
233: }
234:
235: 236: 237: 238: 239: 240:
241: public function getExport ($id)
242: {
243:
244: $_queryBuilder = Configuration::$BASEURI;
245:
246:
247: $_queryBuilder = $_queryBuilder.'/warehouse/exports/{id}';
248:
249:
250: APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array (
251: 'id' => $id,
252: ));
253:
254:
255: APIHelper::appendUrlWithQueryParameters($_queryBuilder, array (
256: 'apiKey' => Configuration::$apiKey,
257: ));
258:
259:
260: $_queryUrl = APIHelper::cleanUrl($_queryBuilder);
261:
262:
263: $_headers = array (
264: 'user-agent' => 'APIMATIC 2.0',
265: 'Accept' => 'application/json'
266: );
267:
268:
269: $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl);
270: if($this->getHttpCallBack() != null) {
271: $this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
272: }
273:
274:
275: $response = Request::get($_queryUrl, $_headers);
276:
277:
278: if($this->getHttpCallBack() != null) {
279: $_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
280: $_httpContext = new HttpContext($_httpRequest, $_httpResponse);
281:
282: $this->getHttpCallBack()->callOnAfterRequest($_httpContext);
283: }
284:
285: if (!isset($response->body->status)) {
286: throw new APIException('Unexpected error in API call. See HTTP response body for details.', $response->code, $response->body);
287: }
288: else if ($response->body->status == 'error') {
289: throw new APIException('API responded with error status.', $response->code, $response->body);
290: }
291:
292: return $response->body->response->data->export;
293: }
294:
295: 296: 297: 298: 299: 300:
301: public function createExport ($body)
302: {
303:
304: $_queryBuilder = Configuration::$BASEURI;
305:
306:
307: $_queryBuilder = $_queryBuilder.'/warehouse/exports';
308:
309:
310: APIHelper::appendUrlWithQueryParameters($_queryBuilder, array (
311: 'apiKey' => Configuration::$apiKey,
312: ));
313:
314:
315: $_queryUrl = APIHelper::cleanUrl($_queryBuilder);
316:
317:
318: $_headers = array (
319: 'user-agent' => 'APIMATIC 2.0',
320: 'Accept' => 'application/json',
321: 'content-type' => 'application/json; charset=utf-8'
322: );
323:
324:
325: $_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl);
326: if($this->getHttpCallBack() != null) {
327: $this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
328: }
329:
330:
331: $response = Request::post($_queryUrl, $_headers, Request\Body::Json($body));
332:
333:
334: if($this->getHttpCallBack() != null) {
335: $_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
336: $_httpContext = new HttpContext($_httpRequest, $_httpResponse);
337:
338: $this->getHttpCallBack()->callOnAfterRequest($_httpContext);
339: }
340:
341: if (!isset($response->body->status)) {
342: throw new APIException('Unexpected error in API call. See HTTP response body for details.', $response->code, $response->body);
343: }
344: else if ($response->body->status == 'error') {
345: throw new APIException('API responded with error status.', $response->code, $response->body);
346: }
347:
348: return $response->body->response->data->id;
349: }
350:
351: 352: 353: 354: 355:
356: public function getImportHistory ()
357: {
358:
359: $_queryBuilder = Configuration::$BASEURI;
360:
361:
362: $_queryBuilder = $_queryBuilder.'/warehouse/imports/history';
363:
364:
365: APIHelper::appendUrlWithQueryParameters($_queryBuilder, array (
366: 'apiKey' => Configuration::$apiKey,
367: ));
368:
369:
370: $_queryUrl = APIHelper::cleanUrl($_queryBuilder);
371:
372:
373: $_headers = array (
374: 'user-agent' => 'APIMATIC 2.0',
375: 'Accept' => 'application/json'
376: );
377:
378:
379: $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl);
380: if($this->getHttpCallBack() != null) {
381: $this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
382: }
383:
384:
385: $response = Request::get($_queryUrl, $_headers);
386:
387:
388: if($this->getHttpCallBack() != null) {
389: $_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
390: $_httpContext = new HttpContext($_httpRequest, $_httpResponse);
391:
392: $this->getHttpCallBack()->callOnAfterRequest($_httpContext);
393: }
394:
395: if (!isset($response->body->status)) {
396: throw new APIException('Unexpected error in API call. See HTTP response body for details.', $response->code, $response->body);
397: }
398: else if ($response->body->status == 'error') {
399: throw new APIException('API responded with error status.', $response->code, $response->body);
400: }
401:
402: return $response->body->response->data->ids;
403: }
404:
405: 406: 407: 408: 409: 410:
411: public function getImport ($id)
412: {
413:
414: $_queryBuilder = Configuration::$BASEURI;
415:
416:
417: $_queryBuilder = $_queryBuilder.'/warehouse/imports/{id}';
418:
419:
420: APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array (
421: 'id' => $id,
422: ));
423:
424:
425: APIHelper::appendUrlWithQueryParameters($_queryBuilder, array (
426: 'apiKey' => Configuration::$apiKey,
427: ));
428:
429:
430: $_queryUrl = APIHelper::cleanUrl($_queryBuilder);
431:
432:
433: $_headers = array (
434: 'user-agent' => 'APIMATIC 2.0',
435: 'Accept' => 'application/json'
436: );
437:
438:
439: $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl);
440: if($this->getHttpCallBack() != null) {
441: $this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
442: }
443:
444:
445: $response = Request::get($_queryUrl, $_headers);
446:
447:
448: if($this->getHttpCallBack() != null) {
449: $_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
450: $_httpContext = new HttpContext($_httpRequest, $_httpResponse);
451:
452: $this->getHttpCallBack()->callOnAfterRequest($_httpContext);
453: }
454:
455: if (!isset($response->body->status)) {
456: throw new APIException('Unexpected error in API call. See HTTP response body for details.', $response->code, $response->body);
457: }
458: else if ($response->body->status == 'error') {
459: throw new APIException('API responded with error status.', $response->code, $response->body);
460: }
461:
462: return $response->body->response->data->import;
463: }
464:
465: 466: 467: 468: 469:
470: public function listWarehouse ()
471: {
472:
473: $_queryBuilder = Configuration::$BASEURI;
474:
475:
476: $_queryBuilder = $_queryBuilder.'/warehouse';
477:
478:
479: APIHelper::appendUrlWithQueryParameters($_queryBuilder, array (
480: 'apiKey' => Configuration::$apiKey,
481: ));
482:
483:
484: $_queryUrl = APIHelper::cleanUrl($_queryBuilder);
485:
486:
487: $_headers = array (
488: 'user-agent' => 'APIMATIC 2.0',
489: 'Accept' => 'application/json'
490: );
491:
492:
493: $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl);
494: if($this->getHttpCallBack() != null) {
495: $this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
496: }
497:
498:
499: $response = Request::get($_queryUrl, $_headers);
500:
501:
502: if($this->getHttpCallBack() != null) {
503: $_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
504: $_httpContext = new HttpContext($_httpRequest, $_httpResponse);
505:
506: $this->getHttpCallBack()->callOnAfterRequest($_httpContext);
507: }
508:
509: if (!isset($response->body->status)) {
510: throw new APIException('Unexpected error in API call. See HTTP response body for details.', $response->code, $response->body);
511: }
512: else if ($response->body->status == 'error') {
513: throw new APIException('API responded with error status.', $response->code, $response->body);
514: }
515:
516: return $response->body->response->data->items;
517: }
518:
519: 520: 521: 522: 523: 524:
525: public function createImport ($body)
526: {
527:
528: $_queryBuilder = Configuration::$BASEURI;
529:
530:
531: $_queryBuilder = $_queryBuilder.'/warehouse/imports';
532:
533:
534: APIHelper::appendUrlWithQueryParameters($_queryBuilder, array (
535: 'apiKey' => Configuration::$apiKey,
536: ));
537:
538:
539: $_queryUrl = APIHelper::cleanUrl($_queryBuilder);
540:
541:
542: $_headers = array (
543: 'user-agent' => 'APIMATIC 2.0',
544: 'Accept' => 'application/json',
545: 'content-type' => 'application/json; charset=utf-8'
546: );
547:
548:
549: $_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl);
550: if($this->getHttpCallBack() != null) {
551: $this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
552: }
553:
554:
555: $response = Request::post($_queryUrl, $_headers, Request\Body::Json($body));
556:
557:
558: if($this->getHttpCallBack() != null) {
559: $_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
560: $_httpContext = new HttpContext($_httpRequest, $_httpResponse);
561:
562: $this->getHttpCallBack()->callOnAfterRequest($_httpContext);
563: }
564:
565: if (!isset($response->body->status)) {
566: throw new APIException('Unexpected error in API call. See HTTP response body for details.', $response->code, $response->body);
567: }
568: else if ($response->body->status == 'error') {
569: throw new APIException('API responded with error status.', $response->code, $response->body);
570: }
571:
572: return $response->body->response->data->id;
573: }
574:
575: 576: 577: 578: 579: 580:
581: public function cancelImport ($id)
582: {
583:
584: $_queryBuilder = Configuration::$BASEURI;
585:
586:
587: $_queryBuilder = $_queryBuilder.'/warehouse/imports/{id}/cancel/';
588:
589:
590: APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array (
591: 'id' => $id,
592: ));
593:
594:
595: APIHelper::appendUrlWithQueryParameters($_queryBuilder, array (
596: 'apiKey' => Configuration::$apiKey,
597: ));
598:
599:
600: $_queryUrl = APIHelper::cleanUrl($_queryBuilder);
601:
602:
603: $_headers = array (
604: 'user-agent' => 'APIMATIC 2.0',
605: 'Accept' => 'application/json',
606: 'content-type' => 'application/json; charset=utf-8'
607: );
608:
609:
610: $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl);
611: if($this->getHttpCallBack() != null) {
612: $this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
613: }
614:
615:
616: $response = Request::post($_queryUrl, $_headers);
617:
618:
619: if($this->getHttpCallBack() != null) {
620: $_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
621: $_httpContext = new HttpContext($_httpRequest, $_httpResponse);
622:
623: $this->getHttpCallBack()->callOnAfterRequest($_httpContext);
624: }
625:
626: if (!isset($response->body->status)) {
627: throw new APIException('Unexpected error in API call. See HTTP response body for details.', $response->code, $response->body);
628: }
629: else if ($response->body->status == 'error') {
630: throw new APIException('API responded with error status.', $response->code, $response->body);
631: }
632:
633: return true;
634: }
635:
636: 637: 638: 639: 640: 641:
642: public function getItemDetails ($id)
643: {
644:
645: $_queryBuilder = Configuration::$BASEURI;
646:
647:
648: $_queryBuilder = $_queryBuilder.'/warehouse/items/{id}';
649:
650:
651: APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array (
652: 'id' => $id,
653: ));
654:
655:
656: APIHelper::appendUrlWithQueryParameters($_queryBuilder, array (
657: 'apiKey' => Configuration::$apiKey,
658: ));
659:
660:
661: $_queryUrl = APIHelper::cleanUrl($_queryBuilder);
662:
663:
664: $_headers = array (
665: 'user-agent' => 'APIMATIC 2.0',
666: 'Accept' => 'application/json'
667: );
668:
669:
670: $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl);
671: if($this->getHttpCallBack() != null) {
672: $this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
673: }
674:
675:
676: $response = Request::get($_queryUrl, $_headers);
677:
678:
679: if($this->getHttpCallBack() != null) {
680: $_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
681: $_httpContext = new HttpContext($_httpRequest, $_httpResponse);
682:
683: $this->getHttpCallBack()->callOnAfterRequest($_httpContext);
684: }
685:
686: if (!isset($response->body->status)) {
687: throw new APIException('Unexpected error in API call. See HTTP response body for details.', $response->code, $response->body);
688: }
689: else if ($response->body->status == 'error') {
690: throw new APIException('API responded with error status.', $response->code, $response->body);
691: }
692:
693: return $response->body->response->data->item;
694: }
695: }
696: