-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicrosoftAzureApiHandler.php
122 lines (100 loc) · 3.35 KB
/
MicrosoftAzureApiHandler.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
<?php
namespace App\MicrosoftAzure;
use App\MicrosoftAzure\CognitiveServices\TextAnalytics\Operations\OperationInterface;
use App\MicrosoftAzure\Exceptions\MSAzureClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\GuzzleException;
use JMS\Serializer\SerializationContext;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
/**
* Class MicrosoftAzureApiHandler
* Project platform.
*
* @author Anton Prokhorov <[email protected]>
*/
class MicrosoftAzureApiHandler
{
private $logger;
/**
* MicrosoftAzureApiHandler constructor.
*
* @param $logger
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function request(OperationInterface $operation)
{
$client = new Client([
'timeout' => 10,
]);
$request = new Request(
$operation->getMethod(),
$this->decorateUri($operation),
$this->decorateHeaders($operation)
);
$options = $this->decorateRequest($operation);
try {
$this->logger->debug(json_encode($options), [
(string) $request->getUri(),
]);
$response = $client->send($request, $options);
$this->logger->debug($response->getBody()->getContents());
} catch (ConnectException $e) {
$this->logger->error(json_encode($e->getMessage()));
throw new MSAzureClientException($e);
} catch (\Exception $e) {
$this->logger->error(json_encode($e->getMessage()));
throw new MSAzureClientException($e);
} catch (GuzzleException $e) {
$this->logger->error(json_encode($e->getMessage()));
}
$response->getBody()->rewind();
return $this->handleResponse($response, $operation);
}
private function decorateRequest(OperationInterface $operation)
{
$options = [];
$options = array_merge_recursive($options, $operation->getOptions($this->getSerializer()));
return $options;
}
private function decorateHeaders(OperationInterface $operation)
{
$headers = [
'Content-Type' => 'application/json',
'Ocp-Apim-Subscription-Key' => $operation->getSubscriptionKey(),
];
$headers = array_merge_recursive($headers, $operation->getHeaders());
return $headers;
}
private function decorateUri(OperationInterface $operation)
{
return $operation->getUri().(\count($operation->getQuery()) > 0 ? '?'.http_build_query($operation->getQuery()) : '');
}
/**
* @param ResponseInterface $response
* @param OperationInterface $operation
*
* @return mixed
*/
private function handleResponse(ResponseInterface $response, OperationInterface $operation)
{
$serializer = $this->getSerializer();
return $operation->getResponse($response, $serializer);
}
final public static function getSerializer()
{
return \JMS\Serializer\SerializerBuilder::create()
->setSerializationContextFactory(function () {
return SerializationContext::create()
->setSerializeNull(true)
;
})
->build()
;
}
}