-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sender.php
141 lines (124 loc) · 4.55 KB
/
Sender.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
<?php
namespace Wearesho\Phonet;
use GuzzleHttp;
use Psr\Http\Message\ResponseInterface;
use Wearesho\Phonet\Authorization\CacheProviderInterface;
/**
* Class Sender
* @package Wearesho\Phonet
*/
class Sender implements RestInterface
{
protected const STATUS_FORBIDDEN = 403;
protected const COOKIE = 'Cookie';
/** @var GuzzleHttp\ClientInterface */
protected $client;
/** @var ConfigInterface */
protected $config;
/** @var Authorization\ProviderInterface|CacheProviderInterface */
protected $provider;
public function __construct(
GuzzleHttp\ClientInterface $client,
ConfigInterface $config,
Authorization\ProviderInterface $provider
) {
$this->client = $client;
$this->config = $config;
$this->provider = $provider;
}
/**
* @param string $api
* @param array $params
*
* @return array
* @throws Exception
*/
public function get(string $api, array $params = []): array
{
return $this->send('GET', $api, null, [
GuzzleHttp\RequestOptions::QUERY => $params
]);
}
/**
* @param string $api
* @param string|null $body
*
* @return array
* @throws Exception
*/
public function post(string $api, string $body = null): array
{
return $this->send('POST', $api, $body);
}
/**
* @param string $method
* @param string $api
* @param string|null $body
* @param array $options
*
* @return array
* @throws Exception
*/
public function send(string $method, string $api, ?string $body, array $options = []): array
{
$options = \array_merge([
GuzzleHttp\RequestOptions::HEADERS => [
'Content-Type' => 'application/json',
],
GuzzleHttp\RequestOptions::BODY => $body
], $options);
$uri = "https://{$this->config->getDomain()}/{$api}";
try {
// Provider can throw ProviderException or CacheException (if it instance of it) so no reasons to catch them
$sessionId = $this->provider->provide($this->config);
$response = $this->client->request($method, $uri, \array_merge_recursive([
GuzzleHttp\RequestOptions::HEADERS => [
Sender::COOKIE => $sessionId,
],
], $options));
} catch (GuzzleHttp\Exception\GuzzleException $exception) {
// Checking exception with hasResponse() is optional, but for better logic execution it must be here
// If service return status code 403 and provider can cache response, sender will try auth with force option
if ($exception instanceof GuzzleHttp\Exception\ClientException
&& $exception->hasResponse()
&& $exception->getResponse()->getStatusCode() === static::STATUS_FORBIDDEN
&& $this->provider instanceof Authorization\CacheProviderInterface
) {
try {
// CacheProvider can throw ProviderException or CacheException so no reasons to catch them
$sessionId = $this->provider->forceProvide($this->config);
$response = $this->client->request($method, $uri, \array_merge_recursive([
GuzzleHttp\RequestOptions::HEADERS => [
Sender::COOKIE => $sessionId
],
], $options));
} catch (GuzzleHttp\Exception\GuzzleException $exception) {
throw new Exception("Api [$api] with force auth failed", $exception->getCode(), $exception);
}
} else {
throw new Exception("Api [{$api}] failed", $exception->getCode(), $exception);
}
}
return $this->parseResponse($response, $api);
}
/**
* @param ResponseInterface $response
* @param string $rest
*
* @return array
* @throws Exception
*/
private function parseResponse(ResponseInterface $response, string $rest): array
{
// In Phonet documentation only `hangup` api (get-method) contain empty body in response
// So no reason to parse it
if (\preg_match('/\/' . RestInterface::HANGUP_CALL . '/', $rest)) {
return [];
}
$json = \json_decode((string)$response->getBody(), true);
if (\json_last_error() !== JSON_ERROR_NONE) {
throw new Exception("[$rest] return response with body that have content not json");
}
return $json;
}
}