Commit 971d56ae authored by 王源's avatar 王源 🎧

调整shopify请求类为guzzleHttp客户端,以便协程化处理

parent 0e6998cf
......@@ -9,7 +9,7 @@
namespace Meibuyu\Micro\Shopify\lib;
use Exception;
use Meibuyu\Micro\Tools\HttpRequestJson;
use Meibuyu\Micro\Shopify\Tools\HttpRequestJson;
use Psr\Http\Message\ResponseInterface;
abstract class AbstractShopify
......@@ -44,6 +44,11 @@ abstract class AbstractShopify
private $config;
/**
* @var HttpRequestJson
*/
private $httpRequestJson;
/**
* AbstractShopify constructor.
* @param $config
......@@ -57,7 +62,7 @@ abstract class AbstractShopify
$this->id = $id;
$this->pluralizeKey = $this->pluralizeKey ?: $this->resourceKey . 's';
$this->resourceUrl = ($parentResourceUrl ? "$parentResourceUrl/" : $config['api_url']) . $this->pluralizeKey . ($this->id ? "/{$this->id}" : '');
$this->httpRequestJson = make(HttpRequestJson::class);
if (isset($config['api_password'])) {
$this->httpHeaders['X-Shopify-Access-Token'] = $config['api_password'];
} elseif (!isset($config['api_password'])) {
......@@ -153,35 +158,6 @@ abstract class AbstractShopify
return $resourceUrl . ($customAction ? "/$customAction" : '') . '.json' . (!empty($urlParams) ? '?' . http_build_query($urlParams) : '');
}
// /**
// * @param array $urlParams
// * @param null $url
// * @param null $dataKey
// * @return mixed
// * @throws Exception
// */
// public function get($urlParams = [], $url = null, $dataKey = null)
// {
// if (!$url) $url = $this->generateUrl($urlParams);
// $response = HttpRequestJson::get($url, $this->httpHeaders);
// if (!$dataKey) $dataKey = $this->id ? $this->resourceKey : $this->pluralizeKey;
// return $this->processResponse($response, $dataKey);
// }
//
// /**
// * 根据id获取一条数据
// * @param $id
// * @param array $urlParams
// * @return mixed
// * @throws Exception
// */
// public function show($id, $urlParams = [])
// {
// $url = $this->generateUrl($urlParams, $id);
// $response = HttpRequestJson::get($url, $this->httpHeaders);
// return $this->processResponse($response, $this->resourceKey);
// }
//
// /**
// * 获取数量
// * @param array $urlParams
......@@ -195,51 +171,6 @@ abstract class AbstractShopify
// }
// $url = $this->generateUrl($urlParams, null, 'count');
// return $this->get([], $url, 'count');
// }
//
// /**
// * @param $dataArray
// * @param null $url
// * @param bool $wrapData
// * @return mixed
// * @throws Exception
// */
// public function post($dataArray, $url = null, $wrapData = true)
// {
// if (!$url) $url = $this->generateUrl();
// if ($wrapData && !empty($dataArray)) $dataArray = $this->wrapData($dataArray);
// $response = HttpRequestJson::post($url, $dataArray, $this->httpHeaders);
// return $this->processResponse($response, $this->resourceKey);
// }
//
// /**
// * @param int|string $id
// * @param $dataArray
// * @param null $url
// * @param bool $wrapData
// * @return mixed
// * @throws Exception
// */
// public function put($id, $dataArray, $url = null, $wrapData = true)
// {
// if (!$url) $url = $this->generateUrl([], $id);
// if ($wrapData && !empty($dataArray)) $dataArray = $this->wrapData($dataArray);
// $response = HttpRequestJson::put($url, $dataArray, $this->httpHeaders);
// return $this->processResponse($response, $this->resourceKey);
// }
//
// /**
// * @param int|string $id
// * @param array $urlParams
// * @param null $url
// * @return mixed
// * @throws Exception
// */
// public function delete($id = null, $urlParams = [], $url = null)
// {
// if (!$url) $url = $this->generateUrl($urlParams, $id);
// $response = HttpRequestJson::delete($url, $this->httpHeaders);
// return $this->processResponse($response);
// }
/**
......@@ -252,7 +183,7 @@ abstract class AbstractShopify
public function get($urlParams = [], $url = null, $dataKey = null)
{
if (!$url) $url = $this->generateUrl($urlParams);
$response = HttpRequestJson::get($url, $this->httpHeaders);
$response = $this->httpRequestJson->get($url, $this->httpHeaders);
if (!$dataKey) $dataKey = $this->id ? $this->resourceKey : $this->pluralizeKey;
return $this->processResponse($response, $dataKey);
}
......@@ -267,7 +198,7 @@ abstract class AbstractShopify
public function page($url = null, $urlParams = [])
{
if (!$url) $url = $this->generateUrl($urlParams);
$response = HttpRequestJson::get($url, $this->httpHeaders);
$response = $this->httpRequestJson->get($url, $this->httpHeaders);
return $this->processPageResponse($response, $this->pluralizeKey);
}
......@@ -281,7 +212,7 @@ abstract class AbstractShopify
public function show($id, $urlParams = [])
{
$url = $this->generateUrl($urlParams, $id);
$response = HttpRequestJson::get($url, $this->httpHeaders);
$response = $this->httpRequestJson->get($url, $this->httpHeaders);
return $this->processResponse($response, $this->resourceKey);
}
......@@ -296,7 +227,7 @@ abstract class AbstractShopify
{
if (!$url) $url = $this->generateUrl();
if ($wrapData && !empty($dataArray)) $dataArray = $this->wrapData($dataArray);
$response = HttpRequestJson::post($url, $dataArray, $this->httpHeaders);
$response = $this->httpRequestJson->post($url, $dataArray, $this->httpHeaders);
return $this->processResponse($response, $this->resourceKey);
}
......@@ -312,7 +243,7 @@ abstract class AbstractShopify
{
if (!$url) $url = $this->generateUrl([], $id);
if ($wrapData && !empty($dataArray)) $dataArray = $this->wrapData($dataArray);
$response = HttpRequestJson::put($url, $dataArray, $this->httpHeaders);
$response = $this->httpRequestJson->put($url, $dataArray, $this->httpHeaders);
return $this->processResponse($response, $this->resourceKey);
}
......@@ -326,7 +257,7 @@ abstract class AbstractShopify
public function delete($id = null, $urlParams = [], $url = null)
{
if (!$url) $url = $this->generateUrl($urlParams, $id);
$response = HttpRequestJson::delete($url, $this->httpHeaders);
$response = $this->httpRequestJson->delete($url, $this->httpHeaders);
return $this->processResponse($response);
}
......@@ -356,21 +287,20 @@ abstract class AbstractShopify
* @return mixed
* @throws Exception
*/
// public function processResponse($response, $dataKey = null)
// {
// $httpCode = $response->getStatusCode();
// $content = $response->getBody()->getContents();
// $content = json_decode($content, true);
//
// if (isset($content['errors'])) {
// throw new Exception($this->castString($content['errors']), $httpCode);
// }
// if ($dataKey && isset($content[$dataKey])) {
// return $content[$dataKey];
// } else {
// return $content;
// }
// }
public function processResponse($response, $dataKey = null)
{
$code = $response->getStatusCode();
$content = $response->getBody()->getContents();
$content = json_decode($content, true);
if (isset($content['errors'])) {
throw new Exception($this->castString($content['errors']), $code);
}
if ($dataKey && isset($content[$dataKey])) {
return $content[$dataKey];
} else {
return $content;
}
}
/**
* 处理响应
......@@ -379,7 +309,7 @@ abstract class AbstractShopify
* @return mixed
* @throws Exception
*/
public function processResponse($response, $dataKey = null)
public function processResponseOld($response, $dataKey = null)
{
[$code, $header, $body] = $response;
$content = json_decode($body, true);
......@@ -396,18 +326,18 @@ abstract class AbstractShopify
/**
* 处理响应
* @param array $response
* @param ResponseInterface $response
* @param null $dataKey
* @return mixed
* @throws Exception
*/
public function processPageResponse($response, $dataKey = null)
{
[$code, $header, $body] = $response;
$content = json_decode($body, true);
$link = $this->getLink($header);
$code = $response->getStatusCode();
$headers = $response->getHeaders();
$content = $response->getBody()->getContents();
$content = json_decode($content, true);
$link = $this->getLink($headers);
if (isset($content['errors'])) {
throw new Exception($this->castString($content['errors']), $code);
}
......@@ -421,12 +351,13 @@ abstract class AbstractShopify
public function getLink($header, $type = 'next')
{
if (array_key_exists('x-shopify-api-version', $header) && $header['x-shopify-api-version'] < '2019-07') {
if (!empty($header['X-Shopify-API-Version'][0]) && $header['X-Shopify-API-Version'][0] < '2019-07') {
return null;
}
if (!empty($header['link'])) {
if (stristr($header['link'], '; rel="' . $type . '"') > -1) {
$headerLinks = explode(',', $header['link']);
if (!empty($header['Link'][0])) {
$headerLinks = $header['Link'][0];
if (stristr($headerLinks, '; rel="' . $type . '"') > -1) {
$headerLinks = explode(',', $headerLinks);
foreach ($headerLinks as $headerLink) {
if (stristr($headerLink, '; rel="' . $type . '"') === -1) {
continue;
......
......@@ -26,7 +26,7 @@ class Graphql extends AbstractShopify
{
if (!$url) $url = $this->generateUrl();
$response = HttpRequestGraphQL::post($url, $graphQL, $this->httpHeaders, $variables);
return $this->processResponse($response);
return $this->processResponseOld($response);
}
public function get($urlParams = array(), $url = null, $dataKey = null)
......
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2020/8/19
* Time: 9:06
*/
namespace Meibuyu\Micro\Shopify\tools;
use GuzzleHttp\HandlerStack;
use Hyperf\Guzzle\ClientFactory;
use Hyperf\Guzzle\HandlerStackFactory;
use Psr\Http\Message\ResponseInterface;
/**
* json格式请求
* Class HttpRequestJson
* @package Meibuyu\Micro\Tools
*/
class HttpRequestJson
{
/**
* @var ClientFactory
*/
private $clientFactory;
/**
* @var HandlerStack
*/
private $stack;
public function __construct(ClientFactory $clientFactory)
{
$this->clientFactory = $clientFactory;
$factory = new HandlerStackFactory();
$this->stack = $factory->create();
}
/**
* get请求
* @param $url
* @param array $httpHeaders
* @return ResponseInterface
*/
public function get($url, $httpHeaders = [])
{
$client = $this->clientFactory->create(['timeout' => 60, 'handler' => $this->stack,]);
return $client->get($url, ['headers' => $httpHeaders]);
}
/**
* post请求
* @param $url
* @param $data
* @param array $httpHeaders
* @return ResponseInterface
*/
public function post($url, $data, $httpHeaders = [])
{
$client = $this->clientFactory->create(['timeout' => 60, 'handler' => $this->stack,]);
return $client->post($url, ['headers' => $httpHeaders, 'json' => $data]);
}
/**
* put请求
* @param $url
* @param $data
* @param array $httpHeaders
* @return ResponseInterface
*/
public function put($url, $data, $httpHeaders = [])
{
$client = $this->clientFactory->create(['timeout' => 60, 'handler' => $this->stack,]);
return $client->put($url, ['headers' => $httpHeaders, 'json' => $data]);
}
/**
* delete请求
* @param $url
* @param array $httpHeaders
* @return ResponseInterface
*/
public function delete($url, $httpHeaders = [])
{
$client = $this->clientFactory->create(['timeout' => 60, 'handler' => $this->stack,]);
return $client->delete($url, ['headers' => $httpHeaders]);
}
}
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