forked from shopwareArchive/AppTemplate-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.php
217 lines (167 loc) · 6.69 KB
/
Client.php
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php declare(strict_types=1);
namespace App\SwagAppsystem;
use App\SwagAppsystem\Exception\ApiException;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\HandlerStack;
class Client
{
/**
* @var int
*/
private const DEFAULT_API_VERSION = 2;
/**
* @var Credentials
*/
private $credentials;
/**
* @var string
*/
private $shopUrl;
/**
* @var array
*/
private $headers;
/**
* @var HandlerStack
*/
private $handlerStack;
/**
* @var HandlerStack
*/
private $authenticationHandlerStack;
/**
* @var int
*/
private $apiVersion;
/**
* @var HttpClient|null
*/
private $httpClient = null;
private function __construct(Credentials $credentials, array $headers = [], int $apiVersion = null, HandlerStack $handlerStack = null, HandlerStack $authenticationHandlerStack = null)
{
$this->credentials = $credentials;
$this->shopUrl = $credentials->getShopUrl();
$this->headers = $headers;
$this->apiVersion = $apiVersion;
$this->apiVersion = $apiVersion ? $apiVersion : self::DEFAULT_API_VERSION;
$this->handlerStack = $handlerStack;
$this->authenticationHandlerStack = $authenticationHandlerStack;
if ($credentials->getToken() !== null) {
$this->httpClient = $this->buildClient($credentials->getToken());
}
}
public static function fromCredentials(Credentials $credentials): Client
{
return new self($credentials);
}
public function withLanguage(string $languageId): Client
{
$this->headers['languageId'] = $languageId;
return new self($this->credentials, $this->headers, $this->apiVersion, $this->handlerStack, $this->authenticationHandlerStack);
}
public function withInheritance(bool $inheritance): Client
{
$this->headers['inheritance'] = $inheritance;
return new self($this->credentials, $this->headers, $this->apiVersion, $this->handlerStack, $this->authenticationHandlerStack);
}
public function withHeader(array $header): Client
{
$this->headers = array_merge($this->headers, $header);
return new self($this->credentials, $this->headers, $this->apiVersion, $this->handlerStack, $this->authenticationHandlerStack);
}
public function withHandlerStack(HandlerStack $handlerStack): Client
{
return new self($this->credentials, $this->headers, $this->apiVersion, $handlerStack, $this->authenticationHandlerStack);
}
public function withAuthenticationHandlerStack(HandlerStack $authenticationHandlerStack): Client
{
return new self($this->credentials, $this->headers, $this->apiVersion, $this->handlerStack, $authenticationHandlerStack);
}
public function withApiVersion(int $apiVersion): Client
{
return new self($this->credentials, $this->headers, $apiVersion, $this->handlerStack, $this->authenticationHandlerStack);
}
public function getHttpClient(): HttpClient
{
if ($this->httpClient !== null) {
return $this->httpClient;
}
if ($this->credentials->getToken() !== null) {
$this->httpClient = $this->buildClient($this->credentials->getToken());
return $this->httpClient;
}
$this->credentials = Authenticator::authenticate($this->credentials, $this->authenticationHandlerStack);
$this->httpClient = $this->buildClient($this->credentials->getToken());
return $this->httpClient;
}
public function fetchDetail(string $entityType, string $id): array
{
$client = $this->getHttpClient();
$requestPath = sprintf('/api/v%s/%s/%s', $this->apiVersion, $entityType, $id);
$response = $client->get($requestPath);
if ($response->getStatusCode() !== 200) {
throw new ApiException($this->shopUrl, $requestPath, $response);
}
return json_decode($response->getBody()->getContents(), true);
}
public function search(string $entityType, array $criteria): array
{
$client = $this->getHttpClient();
$requestPath = sprintf('/api/v%s/search/%s', $this->apiVersion, $entityType);
$response = $client->post($requestPath, ['body' => json_encode($criteria)]);
if ($response->getStatusCode() !== 200) {
throw new ApiException($this->shopUrl, $requestPath, $response);
}
return json_decode($response->getBody()->getContents(), true);
}
public function searchIds(string $entityType, array $criteria): array
{
$client = $this->getHttpClient();
$requestPath = sprintf('/api/v%s/search-ids/%s', $this->apiVersion, $entityType);
$response = $client->post($requestPath, ['body' => json_encode($criteria)]);
if ($response->getStatusCode() !== 200) {
throw new ApiException($this->shopUrl, $requestPath, $response);
}
return json_decode($response->getBody()->getContents(), true);
}
public function createEntity(string $entityType, array $entityData): void
{
$client = $this->getHttpClient();
$requestPath = sprintf('/api/v%s/%s', $this->apiVersion, $entityType);
$response = $client->post($requestPath, ['body' => json_encode($entityData)]);
if ($response->getStatusCode() !== 204) {
throw new ApiException($this->shopUrl, $requestPath, $response);
}
}
public function updateEntity(string $entityType, string $id, array $entityData): void
{
$client = $this->getHttpClient();
$requestPath = sprintf('/api/v%s/%s/%s', $this->apiVersion, $entityType, $id);
$response = $client->patch($requestPath, ['body' => json_encode($entityData)]);
if ($response->getStatusCode() !== 204) {
throw new ApiException($this->shopUrl, $requestPath, $response);
}
}
public function deleteEntity(string $entityType, string $id): void
{
$client = $this->getHttpClient();
$requestPath = sprintf('/api/v%s/%s/%s', $this->apiVersion, $entityType, $id);
$response = $client->delete($requestPath);
if ($response->getStatusCode() !== 204) {
throw new ApiException($this->shopUrl, $requestPath, $response);
}
}
private function buildClient($token): HttpClient
{
$baseHeaders = [
'Authorization' => 'Bearer ' . $token,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
];
return new HttpClient([
'base_uri' => $this->shopUrl,
'headers' => array_merge($baseHeaders, $this->headers),
'handler' => $this->handlerStack,
]);
}
}