@@ -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