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
|
|
|
* @var Config |
14
|
|
|
*/ |
15
|
|
|
protected $config; |
16
|
|
|
|
17
|
|
|
protected $endpoint; |
18
|
|
|
protected $method; |
19
|
|
|
protected $storagePath; |
20
|
|
|
public $headers = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Psr7\Stream |
24
|
|
|
*/ |
25
|
|
|
protected $file; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
public function __construct(Config $config) { |
29
|
|
|
$this->config = $config; |
30
|
|
|
$this->endpoint = Config::$restApiEndPoint . '/' . $config->bucketName; |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function request($method, $storagePath) { |
34
|
|
|
$this->method = strtoupper($method); |
35
|
|
|
$this->storagePath = '/' . ltrim($storagePath, '/'); |
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string|resource $file |
42
|
|
|
* |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
|
|
public function withFile($file) { |
46
|
|
|
$stream = Psr7\stream_for($file); |
47
|
|
|
$this->file = $stream; |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return mixed|\Psr\Http\Message\ResponseInterface |
54
|
|
|
*/ |
55
|
|
|
public function send() { |
56
|
|
|
$client = new Client([ |
57
|
|
|
'timeout' => $this->config->timeout, |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
$url = ($this->config->useSsl ? 'https://' : 'http://') . $this->endpoint . $this->storagePath; |
61
|
|
|
$body = null; |
62
|
|
|
if($this->file && $this->method === 'PUT') { |
|
|
|
|
63
|
|
|
$body = $this->file; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$request = new Psr7\Request( |
67
|
|
|
$this->method, |
68
|
|
|
Util::encodeURI($url), |
69
|
|
|
$this->headers, |
70
|
|
|
$body |
71
|
|
|
); |
72
|
|
|
$authHeader = Signature::getHeaderSign($this->config, |
73
|
|
|
$this->method, |
74
|
|
|
$request->getUri()->getPath() |
75
|
|
|
); |
76
|
|
|
foreach($authHeader as $head => $value) { |
|
|
|
|
77
|
|
|
$request = $request->withHeader($head, $value); |
78
|
|
|
} |
79
|
|
|
$response = $client->send($request, [ |
80
|
|
|
'debug' => $this->config->debug |
81
|
|
|
]); |
82
|
|
|
|
83
|
|
|
return $response; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function withHeader($header, $value) { |
87
|
|
|
$header = strtolower(trim($header)); |
88
|
|
|
|
89
|
|
|
$this->headers[$header] = $value; |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function withHeaders($headers) { |
94
|
|
|
if(is_array($headers)) { |
|
|
|
|
95
|
|
|
foreach ($headers as $header => $value) { |
96
|
|
|
$this->withHeader($header, $value); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.