1 | <?php |
||
11 | class Rest |
||
12 | { |
||
13 | /** |
||
14 | * @var Config |
||
15 | */ |
||
16 | protected $config; |
||
17 | |||
18 | protected $endpoint; |
||
19 | protected $method; |
||
20 | protected $storagePath; |
||
21 | public $headers = []; |
||
22 | |||
23 | /** |
||
24 | * @var Psr7\Stream |
||
25 | */ |
||
26 | protected $file; |
||
27 | |||
28 | |||
29 | 15 | public function __construct(Config $config) |
|
34 | |||
35 | 14 | public function request($method, $storagePath) |
|
41 | |||
42 | |||
43 | /** |
||
44 | * @param string|resource $file |
||
45 | * |
||
46 | * @return $this |
||
47 | */ |
||
48 | 9 | public function withFile($file) |
|
55 | |||
56 | /** |
||
57 | * @return mixed|\Psr\Http\Message\ResponseInterface |
||
58 | */ |
||
59 | 14 | public function send() |
|
60 | { |
||
61 | 14 | $client = new Client([ |
|
62 | 14 | 'timeout' => $this->config->timeout, |
|
63 | 14 | ]); |
|
64 | |||
65 | 14 | $url = $this->endpoint . $this->storagePath; |
|
66 | 14 | $body = null; |
|
67 | 14 | if ($this->file && $this->method === 'PUT') { |
|
68 | 9 | $body = $this->file; |
|
69 | 9 | } |
|
70 | |||
71 | 14 | $request = new Psr7\Request( |
|
72 | 14 | $this->method, |
|
73 | 14 | Util::encodeURI($url), |
|
74 | 14 | $this->headers, |
|
75 | $body |
||
76 | 14 | ); |
|
77 | 14 | $authHeader = Signature::getHeaderSign($this->config, |
|
78 | 14 | $this->method, |
|
79 | 14 | $request->getUri()->getPath() |
|
80 | 14 | ); |
|
81 | 14 | foreach ($authHeader as $head => $value) { |
|
82 | 14 | $request = $request->withHeader($head, $value); |
|
83 | 14 | } |
|
84 | 14 | $response = $client->send($request, [ |
|
85 | 14 | 'debug' => $this->config->debug |
|
86 | 14 | ]); |
|
87 | |||
88 | 12 | return $response; |
|
89 | } |
||
90 | |||
91 | 2 | public function withHeader($header, $value) |
|
98 | |||
99 | 9 | public function withHeaders($headers) |
|
108 | } |
||
109 |