1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Upyun\Api; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Psr7; |
7
|
|
|
use Upyun\Config; |
8
|
|
|
use Upyun\Signature; |
9
|
|
|
use Upyun\Util; |
10
|
|
|
|
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
|
20 |
|
public function __construct(Config $config) |
30
|
|
|
{ |
31
|
20 |
|
$this->config = $config; |
32
|
20 |
|
$this->endpoint = $config->getProtocol() . Config::$restApiEndPoint . '/' . $config->serviceName; |
33
|
20 |
|
} |
34
|
|
|
|
35
|
19 |
|
public function request($method, $storagePath) |
36
|
|
|
{ |
37
|
19 |
|
$this->method = strtoupper($method); |
38
|
19 |
|
$this->storagePath = '/' . ltrim($storagePath, '/'); |
39
|
19 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string|resource $file |
45
|
|
|
* |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
14 |
|
public function withFile($file) |
49
|
|
|
{ |
50
|
14 |
|
$stream = Psr7\stream_for($file); |
51
|
14 |
|
$this->file = $stream; |
52
|
|
|
|
53
|
14 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return mixed|\Psr\Http\Message\ResponseInterface |
58
|
|
|
*/ |
59
|
19 |
|
public function send() |
60
|
|
|
{ |
61
|
19 |
|
$client = new Client([ |
62
|
19 |
|
'timeout' => $this->config->timeout, |
63
|
19 |
|
]); |
64
|
|
|
|
65
|
19 |
|
$url = $this->endpoint . $this->storagePath; |
66
|
19 |
|
$body = null; |
67
|
19 |
|
if ($this->file && $this->method === 'PUT') { |
68
|
14 |
|
$body = $this->file; |
69
|
14 |
|
} |
70
|
|
|
|
71
|
19 |
|
$request = new Psr7\Request( |
72
|
19 |
|
$this->method, |
73
|
19 |
|
Util::encodeURI($url), |
74
|
19 |
|
$this->headers, |
75
|
|
|
$body |
76
|
19 |
|
); |
77
|
19 |
|
$authHeader = Signature::getHeaderSign($this->config, |
78
|
19 |
|
$this->method, |
79
|
19 |
|
$request->getUri()->getPath() |
80
|
19 |
|
); |
81
|
19 |
|
foreach ($authHeader as $head => $value) { |
82
|
19 |
|
$request = $request->withHeader($head, $value); |
83
|
19 |
|
} |
84
|
19 |
|
$response = $client->send($request, [ |
85
|
19 |
|
'debug' => $this->config->debug |
86
|
19 |
|
]); |
87
|
|
|
|
88
|
17 |
|
return $response; |
89
|
|
|
} |
90
|
|
|
|
91
|
5 |
|
public function withHeader($header, $value) |
92
|
|
|
{ |
93
|
5 |
|
$header = strtolower(trim($header)); |
94
|
|
|
|
95
|
5 |
|
$this->headers[$header] = $value; |
96
|
5 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
14 |
|
public function withHeaders($headers) |
100
|
|
|
{ |
101
|
14 |
|
if (is_array($headers)) { |
102
|
14 |
|
foreach ($headers as $header => $value) { |
103
|
1 |
|
$this->withHeader($header, $value); |
104
|
14 |
|
} |
105
|
14 |
|
} |
106
|
14 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
public function toRequest() |
110
|
|
|
{ |
111
|
1 |
|
$url = $this->endpoint . $this->storagePath; |
112
|
1 |
|
$body = null; |
113
|
1 |
|
if ($this->file && $this->method === 'PUT') { |
114
|
1 |
|
$body = $this->file; |
115
|
1 |
|
} |
116
|
|
|
|
117
|
1 |
|
$request = new Psr7\Request( |
118
|
1 |
|
$this->method, |
119
|
1 |
|
Util::encodeURI($url), |
120
|
1 |
|
$this->headers, |
121
|
|
|
$body |
122
|
1 |
|
); |
123
|
1 |
|
$authHeader = Signature::getHeaderSign($this->config, |
124
|
1 |
|
$this->method, |
125
|
1 |
|
$request->getUri()->getPath() |
126
|
1 |
|
); |
127
|
1 |
|
foreach ($authHeader as $head => $value) { |
128
|
1 |
|
$request = $request->withHeader($head, $value); |
129
|
2 |
|
} |
130
|
1 |
|
return $request; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|