|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yproximite\Api\Client; |
|
6
|
|
|
|
|
7
|
|
|
use GuzzleHttp\Psr7\MultipartStream; |
|
8
|
|
|
use Http\Client\HttpClient; |
|
9
|
|
|
use Http\Message\MessageFactory; |
|
10
|
|
|
use Psr\Http\Message\StreamInterface; |
|
11
|
|
|
use Yproximite\Api\Exception\AuthenticationException; |
|
12
|
|
|
use Yproximite\Api\Exception\UploadEmptyFilesException; |
|
13
|
|
|
use Yproximite\Api\Response; |
|
14
|
|
|
use Yproximite\Api\Util\UploadFile; |
|
15
|
|
|
|
|
16
|
|
|
class GraphQLClient extends AbstractClient |
|
17
|
|
|
{ |
|
18
|
|
|
private $graphqlEndpoint; |
|
19
|
|
|
private $authClient; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(AuthClient $authClient, string $graphqlEndpoint = null, HttpClient $httpClient = null, MessageFactory $messageFactory = null) |
|
22
|
|
|
{ |
|
23
|
|
|
parent::__construct($httpClient, $messageFactory); |
|
24
|
|
|
|
|
25
|
|
|
$this->authClient = $authClient; |
|
26
|
|
|
$this->graphqlEndpoint = $graphqlEndpoint; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @throws \Http\Client\Exception |
|
31
|
|
|
*/ |
|
32
|
|
|
public function query(string $query, array $variables = []): Response |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->doGraphQLRequest($query, $variables); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @throws \Http\Client\Exception |
|
39
|
|
|
*/ |
|
40
|
|
|
public function mutation(string $query, array $variables = []): Response |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->doGraphQLRequest($query, $variables); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @throws UploadEmptyFilesException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function upload(int $siteId, array $files = []): Response |
|
49
|
|
|
{ |
|
50
|
|
|
$normalizedFiles = []; |
|
51
|
|
|
|
|
52
|
|
|
if (0 === \count($files)) { |
|
53
|
|
|
throw new UploadEmptyFilesException(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
foreach ($files as $k => $file) { |
|
57
|
|
|
$normalizedFiles[] = new UploadFile($file); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// @TODO: Implement upload mutation |
|
61
|
|
|
return $this->doGraphQLRequest('upload ...', ['siteId' => $siteId], $normalizedFiles); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param $files UploadFile[] |
|
66
|
|
|
* |
|
67
|
|
|
* @throws \Http\Client\Exception |
|
68
|
|
|
*/ |
|
69
|
|
|
private function doGraphQLRequest(string $query, array $variables = [], array $files = [], string $filesParameterName = 'medias[]'): Response |
|
70
|
|
|
{ |
|
71
|
|
|
if (!$this->authClient->isAuthenticated()) { |
|
72
|
|
|
$this->authClient->auth(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$headers = $this->computeRequestHeaders(); |
|
76
|
|
|
$body = $this->computeRequestBody($query, $variables, $files, $filesParameterName); |
|
77
|
|
|
$request = $this->createRequest('POST', $this->graphqlEndpoint, $headers, $body); |
|
78
|
|
|
|
|
79
|
|
|
try { |
|
80
|
|
|
$response = $this->sendRequest($request); |
|
81
|
|
|
$contents = $this->extractJson($request, $response); |
|
82
|
|
|
} catch (AuthenticationException $e) { |
|
83
|
|
|
// Maybe API Token has expired? Clear it, then try to fetch a new API Token |
|
84
|
|
|
$this->authClient->clearApiToken(); |
|
85
|
|
|
$this->authClient->auth(); |
|
86
|
|
|
try { |
|
87
|
|
|
$response = $this->sendRequest($request); |
|
88
|
|
|
$contents = $this->extractJson($request, $response); |
|
89
|
|
|
} catch (AuthenticationException $e) { |
|
90
|
|
|
throw $e; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return new Response($contents); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private function computeRequestHeaders(): array |
|
98
|
|
|
{ |
|
99
|
|
|
$headers = ['Accept' => '*/*']; |
|
100
|
|
|
|
|
101
|
|
|
if ($this->authClient->isAuthenticated()) { |
|
102
|
|
|
$headers['Authorization'] = sprintf('Bearer %s', $this->authClient->getApiToken()); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $headers; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private function computeRequestBody(string $query, array $variables = [], array $files = [], string $filesParameterName = 'medias[]'): StreamInterface |
|
109
|
|
|
{ |
|
110
|
|
|
$data = [ |
|
111
|
|
|
['name' => 'query', 'contents' => $query], |
|
112
|
|
|
['name' => 'variables', 'contents' => json_encode($variables)], |
|
113
|
|
|
]; |
|
114
|
|
|
|
|
115
|
|
|
foreach ($files as $file) { |
|
116
|
|
|
$data[] = [ |
|
117
|
|
|
'name' => $filesParameterName, |
|
118
|
|
|
'contents' => $file->getContent(), |
|
119
|
|
|
'filename' => $file->getName(), |
|
120
|
|
|
]; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return new MultipartStream($data); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|