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\UploadEmptyFilesException; |
12
|
|
|
use Yproximite\Api\Response; |
13
|
|
|
use Yproximite\Api\Util\UploadFile; |
14
|
|
|
|
15
|
|
|
class GraphQLClient extends AbstractClient |
16
|
|
|
{ |
17
|
|
|
private $graphqlEndpoint; |
18
|
|
|
private $authClient; |
19
|
|
|
|
20
|
|
|
public function __construct(AuthClient $authClient, string $graphqlEndpoint = null, HttpClient $httpClient = null, MessageFactory $messageFactory = null) |
21
|
|
|
{ |
22
|
|
|
parent::__construct($httpClient, $messageFactory); |
23
|
|
|
|
24
|
|
|
$this->authClient = $authClient; |
25
|
|
|
$this->graphqlEndpoint = $graphqlEndpoint; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @throws \Http\Client\Exception |
30
|
|
|
*/ |
31
|
|
|
public function query(string $query, array $variables = []): Response |
32
|
|
|
{ |
33
|
|
|
return $this->doGraphQLRequest($query, $variables); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @throws \Http\Client\Exception |
38
|
|
|
*/ |
39
|
|
|
public function mutation(string $query, array $variables = []): Response |
40
|
|
|
{ |
41
|
|
|
return $this->doGraphQLRequest($query, $variables); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @throws UploadEmptyFilesException |
46
|
|
|
*/ |
47
|
|
|
public function upload(int $siteId, array $files = []): Response |
48
|
|
|
{ |
49
|
|
|
$normalizedFiles = []; |
50
|
|
|
|
51
|
|
|
if (0 === \count($files)) { |
52
|
|
|
throw new UploadEmptyFilesException(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
foreach ($files as $k => $file) { |
56
|
|
|
$normalizedFiles[] = new UploadFile($file); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// @TODO: Implement upload mutation |
60
|
|
|
return $this->doGraphQLRequest('upload ...', ['siteId' => $siteId], $normalizedFiles); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $files UploadFile[] |
65
|
|
|
* |
66
|
|
|
* @throws \Http\Client\Exception |
67
|
|
|
*/ |
68
|
|
|
private function doGraphQLRequest(string $query, array $variables = [], array $files = [], string $filesParameterName = 'medias[]'): Response |
69
|
|
|
{ |
70
|
|
|
if (!$this->authClient->isAuthenticated()) { |
71
|
|
|
$this->authClient->auth(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$headers = $this->computeRequestHeaders(); |
75
|
|
|
$data = $this->computeRequestBody($query, $variables, $files, $filesParameterName); |
76
|
|
|
|
77
|
|
|
$request = $this->getMessageFactory()->createRequest('POST', $this->graphqlEndpoint, $headers, $data); |
78
|
|
|
$response = $this->getHttpClient()->sendRequest($request); |
79
|
|
|
$contents = json_decode((string) $response->getBody(), true); |
80
|
|
|
|
81
|
|
|
return new Response($contents); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function computeRequestHeaders(): array |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
|
|
'Accept' => '*/*', |
88
|
|
|
'Authorization' => sprintf('Bearer %s', $this->authClient->getApiToken()), |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function computeRequestBody(string $query, array $variables = [], array $files = [], string $filesParameterName = 'medias[]'): StreamInterface |
93
|
|
|
{ |
94
|
|
|
$data = [ |
95
|
|
|
['name' => 'query', 'contents' => $query], |
96
|
|
|
['name' => 'variables', 'contents' => json_encode($variables)], |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
foreach ($files as $file) { |
100
|
|
|
$data[] = [ |
101
|
|
|
'name' => $filesParameterName, |
102
|
|
|
'contents' => $file->getContent(), |
103
|
|
|
'filename' => $file->getName(), |
104
|
|
|
]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return new MultipartStream($data); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|