Commit 6ec80c96 authored by 王源's avatar 王源 🎧

调整shopify底层为curl请求

parent d1f5920a
...@@ -9,7 +9,8 @@ ...@@ -9,7 +9,8 @@
namespace Meibuyu\Micro\Shopify\lib; namespace Meibuyu\Micro\Shopify\lib;
use Exception; use Exception;
use Meibuyu\Micro\Shopify\Tools\HttpRequestJson; use Meibuyu\Micro\Shopify\tools\CurlHttpRequestJson;
use Meibuyu\Micro\Shopify\tools\HttpRequestJson;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
abstract class AbstractShopify abstract class AbstractShopify
...@@ -45,7 +46,7 @@ abstract class AbstractShopify ...@@ -45,7 +46,7 @@ abstract class AbstractShopify
private $config; private $config;
/** /**
* @var HttpRequestJson * @var HttpRequestJson|CurlHttpRequestJson
*/ */
private $httpRequestJson; private $httpRequestJson;
...@@ -62,7 +63,7 @@ abstract class AbstractShopify ...@@ -62,7 +63,7 @@ abstract class AbstractShopify
$this->id = $id; $this->id = $id;
$this->pluralizeKey = $this->pluralizeKey ?: $this->resourceKey . 's'; $this->pluralizeKey = $this->pluralizeKey ?: $this->resourceKey . 's';
$this->resourceUrl = ($parentResourceUrl ? "$parentResourceUrl/" : $config['api_url']) . $this->pluralizeKey . ($this->id ? "/{$this->id}" : ''); $this->resourceUrl = ($parentResourceUrl ? "$parentResourceUrl/" : $config['api_url']) . $this->pluralizeKey . ($this->id ? "/{$this->id}" : '');
$this->httpRequestJson = make(HttpRequestJson::class); $this->httpRequestJson = make(CurlHttpRequestJson::class);
if (isset($config['api_password'])) { if (isset($config['api_password'])) {
$this->httpHeaders['X-Shopify-Access-Token'] = $config['api_password']; $this->httpHeaders['X-Shopify-Access-Token'] = $config['api_password'];
} elseif (!isset($config['api_password'])) { } elseif (!isset($config['api_password'])) {
...@@ -158,20 +159,20 @@ abstract class AbstractShopify ...@@ -158,20 +159,20 @@ abstract class AbstractShopify
return $resourceUrl . ($customAction ? "/$customAction" : '') . '.json' . (!empty($urlParams) ? '?' . http_build_query($urlParams) : ''); return $resourceUrl . ($customAction ? "/$customAction" : '') . '.json' . (!empty($urlParams) ? '?' . http_build_query($urlParams) : '');
} }
// /** /**
// * 获取数量 * 获取数量
// * @param array $urlParams * @param array $urlParams
// * @return mixed * @return mixed
// * @throws Exception * @throws Exception
// */ */
// public function count($urlParams = []) public function count($urlParams = [])
// { {
// if (!$this->countEnabled) { if (!$this->countEnabled) {
// throw new Exception("当前类{$this->getResourceName()}不支持count()方法"); throw new Exception("当前类{$this->getResourceName()}不支持count()方法");
// } }
// $url = $this->generateUrl($urlParams, null, 'count'); $url = $this->generateUrl($urlParams, null, 'count');
// return $this->get([], $url, 'count'); return $this->get([], $url, 'count');
// } }
/** /**
* @param array $urlParams * @param array $urlParams
...@@ -282,15 +283,14 @@ abstract class AbstractShopify ...@@ -282,15 +283,14 @@ abstract class AbstractShopify
/** /**
* 处理响应 * 处理响应
* @param ResponseInterface $response * @param array $response
* @param null $dataKey * @param null $dataKey
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function processResponse($response, $dataKey = null) public function processResponse($response, $dataKey = null)
{ {
$code = $response->getStatusCode(); [$code, , $content] = $response;
$content = $response->getBody()->getContents();
$content = json_decode($content, true); $content = json_decode($content, true);
if (isset($content['errors'])) { if (isset($content['errors'])) {
throw new Exception($this->castString($content['errors']), $code); throw new Exception($this->castString($content['errors']), $code);
...@@ -302,28 +302,6 @@ abstract class AbstractShopify ...@@ -302,28 +302,6 @@ abstract class AbstractShopify
} }
} }
/**
* 处理响应
* @param array $response
* @param null $dataKey
* @return mixed
* @throws Exception
*/
public function processResponseOld($response, $dataKey = null)
{
[$code, $header, $body] = $response;
$content = json_decode($body, true);
if (isset($content['errors'])) {
throw new Exception($this->castString($content['errors']), $code);
}
if ($dataKey && isset($content[$dataKey])) {
return $content[$dataKey];
} else {
return $content;
}
}
/** /**
* 处理响应 * 处理响应
* @param ResponseInterface $response * @param ResponseInterface $response
...@@ -333,9 +311,7 @@ abstract class AbstractShopify ...@@ -333,9 +311,7 @@ abstract class AbstractShopify
*/ */
public function processPageResponse($response, $dataKey = null) public function processPageResponse($response, $dataKey = null)
{ {
$code = $response->getStatusCode(); [$code, $headers, $content] = $response;
$headers = $response->getHeaders();
$content = $response->getBody()->getContents();
$content = json_decode($content, true); $content = json_decode($content, true);
$link = $this->getLink($headers); $link = $this->getLink($headers);
if (isset($content['errors'])) { if (isset($content['errors'])) {
...@@ -351,11 +327,11 @@ abstract class AbstractShopify ...@@ -351,11 +327,11 @@ abstract class AbstractShopify
public function getLink($header, $type = 'next') public function getLink($header, $type = 'next')
{ {
if (!empty($header['X-Shopify-API-Version'][0]) && $header['X-Shopify-API-Version'][0] < '2019-07') { if (!empty($header['x-shopify-api-version'][0]) && $header['x-shopify-api-version'][0] < '2019-07') {
return null; return null;
} }
if (!empty($header['Link'][0])) { if (!empty($header['link'][0])) {
$headerLinks = $header['Link'][0]; $headerLinks = $header['link'][0];
if (stristr($headerLinks, '; rel="' . $type . '"') > -1) { if (stristr($headerLinks, '; rel="' . $type . '"') > -1) {
$headerLinks = explode(',', $headerLinks); $headerLinks = explode(',', $headerLinks);
foreach ($headerLinks as $headerLink) { foreach ($headerLinks as $headerLink) {
......
...@@ -6,20 +6,19 @@ ...@@ -6,20 +6,19 @@
* Time: 9:06 * Time: 9:06
*/ */
namespace Meibuyu\Micro\Tools; namespace Meibuyu\Micro\Shopify\tools;
//use GuzzleHttp\Client; use Exception;
//use Psr\Http\Message\ResponseInterface;
/** /**
* json格式请求(非协程) * Curl的json格式请求
* Class HttpRequestJson * Class HttpRequestJson
* @package Meibuyu\Micro\Tools * @package Meibuyu\Micro\Tools
*/ */
class HttpRequestJson class CurlHttpRequestJson
{ {
protected static function prepareRequest($headers, $data = []) protected function prepareRequest($headers, $data = [])
{ {
$data = json_encode($data); $data = json_encode($data);
if (!empty($data)) { if (!empty($data)) {
...@@ -29,63 +28,14 @@ class HttpRequestJson ...@@ -29,63 +28,14 @@ class HttpRequestJson
return [$headers, $data]; return [$headers, $data];
} }
// /**
// * get请求
// * @param $url
// * @param array $httpHeaders
// * @return ResponseInterface
// */
// public static function get($url, $httpHeaders = [])
// {
// $client = new Client(['timeout' => 30]);
// return $client->get($url, ['headers' => $httpHeaders]);
// }
//
// /**
// * post请求
// * @param $url
// * @param $data
// * @param array $httpHeaders
// * @return ResponseInterface
// */
// public static function post($url, $data, $httpHeaders = [])
// {
// $client = new Client(['timeout' => 30]);
// return $client->post($url, ['headers' => $httpHeaders, 'json' => $data]);
// }
//
// /**
// * put请求
// * @param $url
// * @param $data
// * @param array $httpHeaders
// * @return ResponseInterface
// */
// public static function put($url, $data, $httpHeaders = [])
// {
// $client = new Client(['timeout' => 30]);
// return $client->put($url, ['headers' => $httpHeaders, 'json' => $data]);
// }
//
// /**
// * delete请求
// * @param $url
// * @param array $httpHeaders
// * @return ResponseInterface
// */
// public static function delete($url, $httpHeaders = [])
// {
// $client = new Client(['timeout' => 30]);
// return $client->delete($url, ['headers' => $httpHeaders]);
// }
/** /**
* get请求
* @param $url * @param $url
* @param array $headers * @param array $headers
* @return array * @return array
* @throws Exception
* @author zero
*/ */
public static function get($url, $headers = []) public function get($url, $headers = [])
{ {
return CurlRequest::get($url, $headers); return CurlRequest::get($url, $headers);
} }
...@@ -96,8 +46,10 @@ class HttpRequestJson ...@@ -96,8 +46,10 @@ class HttpRequestJson
* @param $data * @param $data
* @param array $headers * @param array $headers
* @return array * @return array
* @throws Exception
* @author zero
*/ */
public static function post($url, $data, $headers = []) public function post($url, $data, $headers = [])
{ {
[$headers, $data] = self::prepareRequest($headers, $data); [$headers, $data] = self::prepareRequest($headers, $data);
return CurlRequest::post($url, $data, $headers); return CurlRequest::post($url, $data, $headers);
...@@ -109,8 +61,10 @@ class HttpRequestJson ...@@ -109,8 +61,10 @@ class HttpRequestJson
* @param $data * @param $data
* @param array $headers * @param array $headers
* @return array * @return array
* @throws Exception
* @author zero
*/ */
public static function put($url, $data, $headers = []) public function put($url, $data, $headers = [])
{ {
[$headers, $data] = self::prepareRequest($headers, $data); [$headers, $data] = self::prepareRequest($headers, $data);
return CurlRequest::put($url, $data, $headers); return CurlRequest::put($url, $data, $headers);
...@@ -121,8 +75,10 @@ class HttpRequestJson ...@@ -121,8 +75,10 @@ class HttpRequestJson
* @param $url * @param $url
* @param array $headers * @param array $headers
* @return array * @return array
* @throws Exception
* @author zero
*/ */
public static function delete($url, $headers = []) public function delete($url, $headers = [])
{ {
return CurlRequest::delete($url, $headers); return CurlRequest::delete($url, $headers);
} }
......
<?php <?php
namespace Meibuyu\Micro\Tools; namespace Meibuyu\Micro\Shopify\tools;
use Exception; use Exception;
...@@ -22,12 +22,27 @@ class CurlRequest ...@@ -22,12 +22,27 @@ class CurlRequest
return $ch; return $ch;
} }
/**
* @param $url
* @param array $httpHeaders
* @return array
* @throws Exception
* @author zero
*/
public static function get($url, $httpHeaders = []) public static function get($url, $httpHeaders = [])
{ {
$ch = self::init($url, $httpHeaders); $ch = self::init($url, $httpHeaders);
return self::processRequest($ch); return self::processRequest($ch);
} }
/**
* @param $url
* @param $data
* @param array $httpHeaders
* @return array
* @throws Exception
* @author zero
*/
public static function post($url, $data, $httpHeaders = []) public static function post($url, $data, $httpHeaders = [])
{ {
$ch = self::init($url, $httpHeaders); $ch = self::init($url, $httpHeaders);
...@@ -36,6 +51,14 @@ class CurlRequest ...@@ -36,6 +51,14 @@ class CurlRequest
return self::processRequest($ch); return self::processRequest($ch);
} }
/**
* @param $url
* @param $data
* @param array $httpHeaders
* @return array
* @throws Exception
* @author zero
*/
public static function put($url, $data, $httpHeaders = []) public static function put($url, $data, $httpHeaders = [])
{ {
$ch = self::init($url, $httpHeaders); $ch = self::init($url, $httpHeaders);
...@@ -44,6 +67,13 @@ class CurlRequest ...@@ -44,6 +67,13 @@ class CurlRequest
return self::processRequest($ch); return self::processRequest($ch);
} }
/**
* @param $url
* @param array $httpHeaders
* @return array
* @throws Exception
* @author zero
*/
public static function delete($url, $httpHeaders = []) public static function delete($url, $httpHeaders = [])
{ {
$ch = self::init($url, $httpHeaders); $ch = self::init($url, $httpHeaders);
...@@ -51,17 +81,23 @@ class CurlRequest ...@@ -51,17 +81,23 @@ class CurlRequest
return self::processRequest($ch); return self::processRequest($ch);
} }
/**
* @param $ch
* @return array
* @throws Exception
* @author zero
*/
protected static function processRequest($ch) protected static function processRequest($ch)
{ {
$output = curl_exec($ch); $output = curl_exec($ch);
$response = new CurlResponse($output); $response = new CurlResponse($output);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode == 429) { // if ($httpCode == 429) {
$limitHeader = explode('/', $response->getHeader('X-Shopify-Shop-Api-Call-Limit'), 2); // $limitHeader = explode('/', $response->getHeader('X-Shopify-Shop-Api-Call-Limit')[0], 2);
if (isset($limitHeader[1]) && $limitHeader[0] < $limitHeader[1]) { // if (isset($limitHeader[1]) && $limitHeader[0] < $limitHeader[1]) {
throw new Exception($response->getBody()); // throw new Exception($response->getBody());
} // }
} // }
if (curl_errno($ch)) { if (curl_errno($ch)) {
throw new Exception(curl_errno($ch) . ' : ' . curl_error($ch)); throw new Exception(curl_errno($ch) . ' : ' . curl_error($ch));
} }
......
<?php <?php
namespace Meibuyu\Micro\Tools; namespace Meibuyu\Micro\Shopify\tools;
class CurlResponse class CurlResponse
{ {
...@@ -21,16 +21,16 @@ class CurlResponse ...@@ -21,16 +21,16 @@ class CurlResponse
*/ */
private function parse($response) private function parse($response)
{ {
$response = \explode("\r\n\r\n", $response); $response = explode("\r\n\r\n", $response);
if (\count($response) > 1) { if (count($response) > 1) {
// We want the last two parts // We want the last two parts
$response = \array_slice($response, -2, 2); $response = array_slice($response, -2, 2);
list($headers, $body) = $response; list($headers, $body) = $response;
foreach (\explode("\r\n", $headers) as $header) { foreach (explode("\r\n", $headers) as $header) {
$pair = \explode(': ', $header, 2); $pair = explode(': ', $header, 2);
if (isset($pair[1])) { if (isset($pair[1])) {
$headerKey = strtolower($pair[0]); $headerKey = strtolower($pair[0]);
$this->headers[$headerKey] = $pair[1]; $this->headers[$headerKey][] = $pair[1];
} }
} }
} else { } else {
......
...@@ -7,18 +7,25 @@ ...@@ -7,18 +7,25 @@
namespace Meibuyu\Micro\Shopify\tools; namespace Meibuyu\Micro\Shopify\tools;
use Meibuyu\Micro\Tools\CurlRequest; use Exception;
use Meibuyu\Micro\Tools\HttpRequestJson;
class HttpRequestGraphQL extends HttpRequestJson class HttpRequestGraphQL
{ {
/**
* @param array $headers
* @param array $data
* @param null $variables
* @return array
* @throws Exception
* @author zero
*/
protected static function prepareRequest($headers = [], $data = [], $variables = null) protected static function prepareRequest($headers = [], $data = [], $variables = null)
{ {
if (is_string($data)) { if (is_string($data)) {
$postDataGraphQL = $data; $postDataGraphQL = $data;
} else { } else {
throw new \Exception("Only GraphQL string is allowed!"); throw new Exception("Only GraphQL string is allowed!");
} }
if (is_array($variables)) { if (is_array($variables)) {
$postDataGraphQL = json_encode(['query' => $data, 'variables' => $variables]); $postDataGraphQL = json_encode(['query' => $data, 'variables' => $variables]);
...@@ -29,6 +36,15 @@ class HttpRequestGraphQL extends HttpRequestJson ...@@ -29,6 +36,15 @@ class HttpRequestGraphQL extends HttpRequestJson
return [$headers, $postDataGraphQL]; return [$headers, $postDataGraphQL];
} }
/**
* @param $url
* @param $data
* @param array $headers
* @param null $variables
* @return array
* @throws Exception
* @author zero
*/
public static function post($url, $data, $headers = [], $variables = null) public static function post($url, $data, $headers = [], $variables = null)
{ {
[$headers, $postDataGraphQL] = self::prepareRequest($headers, $data, $variables); [$headers, $postDataGraphQL] = self::prepareRequest($headers, $data, $variables);
......
...@@ -47,7 +47,8 @@ class HttpRequestJson ...@@ -47,7 +47,8 @@ class HttpRequestJson
public function get($url, $httpHeaders = []) public function get($url, $httpHeaders = [])
{ {
$client = $this->clientFactory->create(['timeout' => 60, 'handler' => $this->stack,]); $client = $this->clientFactory->create(['timeout' => 60, 'handler' => $this->stack,]);
return $client->get($url, ['headers' => $httpHeaders]); $res = $client->get($url, ['headers' => $httpHeaders]);
return $this->processResponse($res);
} }
/** /**
...@@ -60,7 +61,8 @@ class HttpRequestJson ...@@ -60,7 +61,8 @@ class HttpRequestJson
public function post($url, $data, $httpHeaders = []) public function post($url, $data, $httpHeaders = [])
{ {
$client = $this->clientFactory->create(['timeout' => 60, 'handler' => $this->stack,]); $client = $this->clientFactory->create(['timeout' => 60, 'handler' => $this->stack,]);
return $client->post($url, ['headers' => $httpHeaders, 'json' => $data]); $res = $client->post($url, ['headers' => $httpHeaders, 'json' => $data]);
return $this->processResponse($res);
} }
/** /**
...@@ -73,7 +75,8 @@ class HttpRequestJson ...@@ -73,7 +75,8 @@ class HttpRequestJson
public function put($url, $data, $httpHeaders = []) public function put($url, $data, $httpHeaders = [])
{ {
$client = $this->clientFactory->create(['timeout' => 60, 'handler' => $this->stack,]); $client = $this->clientFactory->create(['timeout' => 60, 'handler' => $this->stack,]);
return $client->put($url, ['headers' => $httpHeaders, 'json' => $data]); $res = $client->put($url, ['headers' => $httpHeaders, 'json' => $data]);
return $this->processResponse($res);
} }
/** /**
...@@ -85,7 +88,21 @@ class HttpRequestJson ...@@ -85,7 +88,21 @@ class HttpRequestJson
public function delete($url, $httpHeaders = []) public function delete($url, $httpHeaders = [])
{ {
$client = $this->clientFactory->create(['timeout' => 60, 'handler' => $this->stack,]); $client = $this->clientFactory->create(['timeout' => 60, 'handler' => $this->stack,]);
return $client->delete($url, ['headers' => $httpHeaders]); $res = $client->delete($url, ['headers' => $httpHeaders]);
return $this->processResponse($res);
}
/**
* 处理响应
* @param ResponseInterface $response
* @return mixed
*/
public function processResponse($response)
{
$code = $response->getStatusCode();
$headers = $response->getHeaders();
$content = $response->getBody()->getContents();
return [$code, $headers, $content];
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment