-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathRest.php
More file actions
86 lines (73 loc) · 2.31 KB
/
Rest.php
File metadata and controls
86 lines (73 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
declare(strict_types=1);
namespace Shopify\Clients;
use Shopify\Context;
use Shopify\Exception\MissingArgumentException;
class Rest extends Http
{
/** @var string */
private $accessToken;
/**
* Rest Client constructor.
*
* @param string $domain
* @param string|null $accessToken
*
* @throws \Shopify\Exception\MissingArgumentException
*/
public function __construct(string $domain, ?string $accessToken = null)
{
parent::__construct($domain);
$this->accessToken = $accessToken;
if (!Context::$IS_CUSTOM_APP && !$this->accessToken) {
throw new MissingArgumentException('Missing access token when creating REST client');
}
}
/**
* {@inheritDoc}
*/
protected function request(
string $path,
string $method,
$body = null,
array $headers = [],
array $query = [],
?int $tries = null,
string $dataType = self::DATA_TYPE_JSON
): RestResponse {
$headers[HttpHeaders::X_SHOPIFY_ACCESS_TOKEN] =
Context::$IS_CUSTOM_APP ? Context::$API_SECRET_KEY : $this->accessToken;
$response = parent::request($path, $method, $body, $headers, $query, $tries, $dataType);
return new RestResponse(
$response->getStatusCode(),
$response->getHeaders(),
$response->getBody(),
$response->getProtocolVersion(),
$response->getReasonPhrase(),
$this->getPageInfo($response)
);
}
protected function getRequestPath(string $path): string
{
$path = parent::getRequestPath($path);
$path = preg_replace("/\.json$/", "", $path) . ".json";
$apiVersion = Context::$API_VERSION;
if (strpos($path, "/admin") !== 0) {
$path = "/admin/api/$apiVersion$path";
}
return $path;
}
/**
* @param \Shopify\Clients\HttpResponse $response
*
* @return \Shopify\Clients\PageInfo|null
*/
private function getPageInfo(HttpResponse $response): ?PageInfo
{
$pageInfo = null;
if ($response->hasHeader(HttpHeaders::PAGINATION_HEADER)) {
$pageInfo = PageInfo::fromLinkHeader($response->getHeaderLine(HttpHeaders::PAGINATION_HEADER));
}
return $pageInfo;
}
}