Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-micro
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
without authentication
meibuyu-micro
Commits
971d56ae
Commit
971d56ae
authored
Feb 20, 2021
by
王源
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
调整shopify请求类为guzzleHttp客户端,以便协程化处理
parent
0e6998cf
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
131 additions
and
109 deletions
+131
-109
AbstractShopify.php
src/Shopify/lib/AbstractShopify.php
+39
-108
Graphql.php
src/Shopify/lib/Graphql.php
+1
-1
HttpRequestJson.php
src/Shopify/tools/HttpRequestJson.php
+91
-0
No files found.
src/Shopify/lib/AbstractShopify.php
View file @
971d56ae
...
...
@@ -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
processResponse
Old
(
$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
;
...
...
src/Shopify/lib/Graphql.php
View file @
971d56ae
...
...
@@ -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
->
processResponse
Old
(
$response
);
}
public
function
get
(
$urlParams
=
array
(),
$url
=
null
,
$dataKey
=
null
)
...
...
src/Shopify/tools/HttpRequestJson.php
0 → 100644
View file @
971d56ae
<?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
]);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment