Skip to content

Commit a04ac59

Browse files
committed
Different Class for Handling JSON calls
1 parent 097424c commit a04ac59

File tree

2 files changed

+166
-56
lines changed

2 files changed

+166
-56
lines changed

lib/HttpRequestJson.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* @author Tareq Mahmood <tareqtms@yahoo.com>
5+
* Created at: 8/24/16 9:03 AM UTC+06:00
6+
*/
7+
8+
namespace PHPShopify;
9+
10+
/**
11+
* Class HttpRequestJson
12+
*
13+
* Prepare the data / headers for JSON requests, make the call and decode the response
14+
* Accepts data in array format returns data in array format
15+
*
16+
* @uses CurlRequest
17+
*
18+
* @package PHPShopify
19+
*/
20+
class HttpRequestJson
21+
{
22+
23+
/**
24+
* HTTP request headers
25+
*
26+
* @var array
27+
*/
28+
private static $httpHeaders;
29+
30+
/**
31+
* Prepared JSON string to be posted with request
32+
*
33+
* @var string
34+
*/
35+
private static $postDataJSON;
36+
37+
38+
/**
39+
* Prepare the data and request headers before making the call
40+
*
41+
* @param array $dataArray
42+
* @param array $httpHeaders
43+
*
44+
* @return void
45+
*/
46+
protected static function prepareRequest($httpHeaders = array(), $dataArray = array())
47+
{
48+
49+
self::$postDataJSON = json_encode($dataArray);
50+
51+
self::$httpHeaders = $httpHeaders;
52+
53+
if (!empty($dataArray)) {
54+
self::$httpHeaders['Content-type'] = 'application/json';
55+
self::$httpHeaders['Content-Length'] = strlen(self::$postDataJSON);
56+
}
57+
}
58+
59+
/**
60+
* Implement a GET request and return json decoded output
61+
*
62+
* @param string $url
63+
* @param array $httpHeaders
64+
*
65+
* @return string
66+
*/
67+
public static function get($url, $httpHeaders = array())
68+
{
69+
self::prepareRequest($httpHeaders);
70+
71+
$response = CurlRequest::get($url, self::$httpHeaders);
72+
73+
return self::processResponse($response);
74+
}
75+
76+
/**
77+
* Implement a POST request and return json decoded output
78+
*
79+
* @param string $url
80+
* @param array $dataArray
81+
* @param array $httpHeaders
82+
*
83+
* @return string
84+
*/
85+
public static function post($url, $dataArray, $httpHeaders = array())
86+
{
87+
self::prepareRequest($httpHeaders, $dataArray);
88+
89+
$response = CurlRequest::post($url, self::$postDataJSON, self::$httpHeaders);
90+
91+
return self::processResponse($response);
92+
}
93+
94+
/**
95+
* Implement a PUT request and return json decoded output
96+
*
97+
* @param string $url
98+
* @param array $dataArray
99+
* @param array $httpHeaders
100+
*
101+
* @return string
102+
*/
103+
public static function put($url, $dataArray, $httpHeaders = array())
104+
{
105+
self::prepareRequest($httpHeaders, $dataArray);
106+
107+
$response = CurlRequest::put($url, self::$postDataJSON, self::$httpHeaders);
108+
109+
return self::processResponse($response);
110+
}
111+
112+
/**
113+
* Implement a DELETE request and return json decoded output
114+
*
115+
* @param string $url
116+
* @param array $httpHeaders
117+
*
118+
* @return string
119+
*/
120+
public static function delete($url, $httpHeaders = array())
121+
{
122+
self::prepareRequest($httpHeaders);
123+
124+
$response = CurlRequest::delete($url, self::$httpHeaders);
125+
126+
return self::processResponse($response);
127+
}
128+
129+
/**
130+
* Decode JSON response
131+
*
132+
* @param string $response
133+
*
134+
* @return array
135+
*/
136+
protected static function processResponse($response)
137+
{
138+
139+
return json_decode($response, true);
140+
}
141+
142+
}

lib/ShopifyAPI.php

Lines changed: 24 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,7 @@ abstract class ShopifyAPI
3535
*
3636
* @var array
3737
*/
38-
protected $httpHeaders;
39-
40-
/**
41-
* Prepared JSON string to be posted with request
42-
*
43-
* @var string
44-
*/
45-
protected $postDataJSON;
38+
protected $httpHeaders = array();
4639

4740
/**
4841
* The base URL of the API Resource (excluding the '.json' extension).
@@ -129,6 +122,10 @@ public function __construct($config, $id = null)
129122
$parentResource = isset($config['ParentResource']) ? $config['ParentResource'] : '';
130123

131124
$this->resourceUrl = $config['ApiUrl'] . $parentResource . $this->getResourcePath() . ($this->id ? '/' . $this->id : '');
125+
126+
if (isset($config['AccessToken'])) {
127+
$this->httpHeaders['X-Shopify-Access-Token'] = $config['AccessToken'];
128+
}
132129
}
133130

134131
/**
@@ -171,7 +168,7 @@ public function __call($name, $arguments)
171168
$childKey = array_search($name, $this->childResource);
172169

173170
if ($childKey === false) {
174-
throw new \SdkException("Child Resource $name is not available for " . $this->getResourceName());
171+
throw new SdkException("Child Resource $name is not available for " . $this->getResourceName());
175172
}
176173

177174
//If any associative key is given to the childname, then it will be considered as the class name,
@@ -303,18 +300,17 @@ public function generateUrl($urlParams = array(), $customAction = null)
303300
*
304301
* @param array $urlParams Check Shopify API reference of the specific resource for the list of URL parameters
305302
* @param string $url
303+
* @param string $dataKey Keyname to fetch data from response array
306304
*
307-
* @uses CurlRequest::get() to send the HTTP request
305+
* @uses HttpRequestJson::get() to send the HTTP request
308306
*
309307
* @return array
310308
*/
311309
public function get($urlParams = array(), $url = null, $dataKey = null)
312310
{
313311
if (!$url) $url = $this->generateUrl($urlParams);
314312

315-
$this->prepareRequest();
316-
317-
$response = CurlRequest::get($url, $this->httpHeaders);
313+
$response = HttpRequestJson::get($url, $this->httpHeaders);
318314

319315
if (!$dataKey) $dataKey = $this->id ? $this->resourceKey : $this->pluralizeKey();
320316

@@ -364,17 +360,17 @@ public function search($query)
364360
* @param array $dataArray Check Shopify API reference of the specific resource for the list of required and optional data elements to be provided
365361
* @param string $url
366362
*
367-
* @uses CurlRequest::post() to send the HTTP request
363+
* @uses HttpRequestJson::post() to send the HTTP request
368364
*
369365
* @return array
370366
*/
371367
public function post($dataArray, $url = null)
372368
{
373369
if (!$url) $url = $this->generateUrl();
374-
375-
$this->prepareRequest($dataArray);
376370

377-
$response = CurlRequest::post($url, $this->postDataJSON, $this->httpHeaders);
371+
if (!empty($dataArray)) $dataArray = $this->wrapData($dataArray);
372+
373+
$response = HttpRequestJson::post($url, $dataArray, $this->httpHeaders);
378374

379375
return $this->processResponse($response, $this->resourceKey);
380376
}
@@ -385,7 +381,7 @@ public function post($dataArray, $url = null)
385381
* @param array $dataArray Check Shopify API reference of the specific resource for the list of required and optional data elements to be provided
386382
* @param string $url
387383
*
388-
* @uses CurlRequest::put() to send the HTTP request
384+
* @uses HttpRequestJson::put() to send the HTTP request
389385
*
390386
* @return array
391387
*/
@@ -394,9 +390,9 @@ public function put($dataArray, $url = null)
394390

395391
if (!$url) $url = $this->generateUrl();
396392

397-
$this->prepareRequest($dataArray);
393+
if (!empty($dataArray)) $dataArray = $this->wrapData($dataArray);
398394

399-
$response = CurlRequest::put($url, $this->postDataJSON, $this->httpHeaders);
395+
$response = HttpRequestJson::put($url, $dataArray, $this->httpHeaders);
400396

401397
return $this->processResponse($response, $this->resourceKey);
402398
}
@@ -407,47 +403,19 @@ public function put($dataArray, $url = null)
407403
* @param array $urlParams Check Shopify API reference of the specific resource for the list of URL parameters
408404
* @param string $url
409405
*
410-
* @uses CurlRequest::delete() to send the HTTP request
406+
* @uses HttpRequestJson::delete() to send the HTTP request
411407
*
412408
* @return array an empty array will be returned if the request is successfully completed
413409
*/
414410
public function delete($urlParams = array(), $url = null)
415411
{
416412
if (!$url) $url = $this->generateUrl($urlParams);
417413

418-
$this->prepareRequest();
419-
420-
$response = CurlRequest::delete($url, $this->httpHeaders);
414+
$response = HttpRequestJson::delete($url, $this->httpHeaders);
421415

422416
return $this->processResponse($response);
423417
}
424418

425-
/**
426-
* Prepare the data and request headers before making the call
427-
*
428-
* @param array $dataArray
429-
*
430-
* @return void
431-
*/
432-
public function prepareRequest($dataArray = array())
433-
{
434-
if (!empty($dataArray)) $dataArray = $this->wrapData($dataArray);
435-
436-
$this->postDataJSON = json_encode($dataArray);
437-
438-
$this->httpHeaders = array();
439-
440-
if (isset($this->config['AccessToken'])) {
441-
$this->httpHeaders['X-Shopify-Access-Token'] = $this->config['AccessToken'];
442-
}
443-
444-
if (!empty($dataArray)) {
445-
$this->httpHeaders['Content-type'] = 'application/json';
446-
$this->httpHeaders['Content-Length'] = strlen($this->postDataJSON);
447-
}
448-
449-
}
450-
451419
/**
452420
* Wrap data array with resource key
453421
*
@@ -497,25 +465,25 @@ protected function castString($array)
497465
/**
498466
* Process the request response
499467
*
500-
* @param string $response JSON response from the API
501-
* @param string $dataKey key to be looked for in the data
468+
* @param array $responseArray Request response in array format
469+
* @param string $dataKey Keyname to fetch data from response array
502470
*
503471
* @throws ApiException if the response has an error specified
504472
* @throws CurlException if response received with unexpected HTTP code.
505473
*
506474
* @return array
507475
*/
508-
public function processResponse($response, $dataKey = false)
476+
public function processResponse($responseArray, $dataKey = null)
509477
{
510-
$responseArray = json_decode($response, true);
511-
512478
if ($responseArray === null) {
513479
//Something went wrong, Checking HTTP Codes
514480
$httpOK = 200; //Request Successful, OK.
515481
$httpCreated = 201; //Create Successful.
482+
483+
//should be null if any other library used for http calls
516484
$httpCode = CurlRequest::$lastHttpCode;
517485

518-
if ($httpCode != $httpOK && $httpCode != $httpCreated) {
486+
if ($httpCode != null && $httpCode != $httpOK && $httpCode != $httpCreated) {
519487
throw new Exception\CurlException("Request failed with HTTP Code $httpCode.");
520488
}
521489
}

0 commit comments

Comments
 (0)