1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Rutube PHP API Client package. |
5
|
|
|
* |
6
|
|
|
* (c) Rutube |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Rutube\Transports; |
13
|
|
|
|
14
|
|
|
use Rutube\Exceptions\Exception; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Низкоуровневое обращение к API |
18
|
|
|
* |
19
|
|
|
* @package Rutube\Transports |
20
|
|
|
*/ |
21
|
|
|
class Transport |
22
|
|
|
{ |
23
|
|
|
protected $client; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var boolean |
27
|
|
|
*/ |
28
|
|
|
protected $secure; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $rutube; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $token = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $exceptions = array( |
44
|
|
|
400 => 'Rutube\Exceptions\BadRequestException', |
45
|
|
|
401 => 'Rutube\Exceptions\UnauthorizedException', |
46
|
|
|
403 => 'Rutube\Exceptions\ForbiddenException', |
47
|
|
|
404 => 'Rutube\Exceptions\NotFoundException', |
48
|
|
|
405 => 'Rutube\Exceptions\MethodNotAllowedException', |
49
|
|
|
500 => 'Rutube\Exceptions\ServerErrorException' |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $transport |
54
|
|
|
* @param bool $secure |
55
|
|
|
* @param string $rutube |
56
|
|
|
* @throws Exception |
57
|
|
|
*/ |
58
|
|
|
public function __construct($transport, $secure, $rutube) |
59
|
|
|
{ |
60
|
|
|
$this->secure = $secure; |
61
|
|
|
$this->rutube = $rutube; |
62
|
|
|
$trs = $this->transports; |
63
|
|
|
|
64
|
|
|
if (!isset($trs[$transport]) || !class_exists($trs[$transport])) { |
65
|
|
|
throw new Exception("Unknown " . $transport . " transport"); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->client = new $trs[$transport](); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var array |
73
|
|
|
*/ |
74
|
|
|
protected $transports = array( |
75
|
|
|
'httpful' => '\Rutube\Clients\ClientHttpful', |
76
|
|
|
'mock' => '\Rutube\Clients\ClientMock', |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
protected function getProtocol() |
83
|
|
|
{ |
84
|
|
|
$protocol = 'http'; |
85
|
|
|
|
86
|
|
|
if ($this->isSecure()) { |
87
|
|
|
$protocol .= 's'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $protocol . "://"; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $url |
95
|
|
|
* @param array $query |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
protected function getUrl($url, $query = array()) |
99
|
|
|
{ |
100
|
|
|
$url = $this->getProtocol() . $this->rutube . '/' . $url; |
101
|
|
|
|
102
|
|
|
if (!empty($query)) { |
103
|
|
|
$url .= '?' . http_build_query($query); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $url; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public function isSecure() |
113
|
|
|
{ |
114
|
|
|
return (bool)$this->secure; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function hasToken() |
121
|
|
|
{ |
122
|
|
|
return (bool)$this->token; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getToken() |
129
|
|
|
{ |
130
|
|
|
return $this->token; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return \Httpful\Request |
135
|
|
|
*/ |
136
|
|
|
public function getClient() |
137
|
|
|
{ |
138
|
|
|
return $this->client; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $method Метод: GET, POST, PUT, PATCH, DELETE |
143
|
|
|
* @param string $url URL метода API, например: api/video/person/ |
144
|
|
|
* @param array $params Параметры зпроса |
145
|
|
|
* @param array $query Запрос |
146
|
|
|
* @param array $file Путь к файлу |
147
|
|
|
* @param bool $return_code Если true - возвращается HTTP-код ответа |
148
|
|
|
* @return mixed |
149
|
|
|
* |
150
|
|
|
* @throws \Rutube\Exceptions\ConnectionErrorException |
151
|
|
|
*/ |
152
|
|
|
public function call($method, $url, $params = array(), $query = array(), $file = array(), $return_code = false) |
153
|
|
|
{ |
154
|
|
|
try { |
155
|
|
|
/** @var \Httpful\Request $request */ |
156
|
|
|
$request = $this->client |
157
|
|
|
->{strtolower($method)}($this->getUrl($url, $query)) |
158
|
|
|
->asJson() |
159
|
|
|
->setHeaders($this->getToken()); |
160
|
|
|
|
161
|
|
|
if (!empty($params)) { |
162
|
|
|
$request = $request->setBody($params); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (!empty($file)) { |
166
|
|
|
$request = $request->attach($file); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$response = $request->send(); |
170
|
|
|
} catch (\Exception $e) { |
171
|
|
|
throw new \Rutube\Exceptions\ConnectionErrorException($e->getMessage(), $e->getCode()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if (isset($this->exceptions[$response->code])) { |
175
|
|
|
throw new $this->exceptions[$response->code](); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $return_code ? $response->code : $response->body; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|