Skip to content

Commit 10c0f55

Browse files
lewzylulewzylu
andauthored
feature-custom-header (#158)
Co-authored-by: lewzylu <[email protected]>
1 parent f6e13d8 commit 10c0f55

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

src/Qcloud/Cos/Client.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function __construct($cosConfig) {
4949
$this->cosConfig['endpoint'] = isset($cosConfig['endpoint']) ? $cosConfig['endpoint'] : 'myqcloud.com';
5050
$this->cosConfig['domain'] = isset($cosConfig['domain']) ? $cosConfig['domain'] : null;
5151
$this->cosConfig['proxy'] = isset($cosConfig['proxy']) ? $cosConfig['proxy'] : null;
52+
$this->cosConfig['retry'] = isset($cosConfig['retry']) ? $cosConfig['retry'] : 1;
5253
$this->cosConfig['userAgent'] = isset($cosConfig['userAgent']) ? $cosConfig['userAgent'] : 'cos-php-sdk-v5.'. Client::VERSION;
5354
$this->cosConfig['pathStyle'] = isset($cosConfig['pathStyle']) ? $cosConfig['pathStyle'] : false;
5455

@@ -113,14 +114,20 @@ public function __destruct() {
113114
}
114115

115116
public function __call($method, array $args) {
116-
try {
117-
return parent::__call(ucfirst($method), $args);
118-
} catch (CommandException $e) {
119-
$previous = $e->getPrevious();
120-
if ($previous !== null) {
121-
throw $previous;
122-
} else {
123-
throw $e;
117+
for ($i = 1; $i <= $this->cosConfig['retry']; $i++) {
118+
try {
119+
return parent::__call(ucfirst($method), $args);
120+
} catch (CommandException $e) {
121+
if ($i != $this->cosConfig['retry']) {
122+
sleep(1 << ($i-1));
123+
continue;
124+
}
125+
$previous = $e->getPrevious();
126+
if ($previous !== null) {
127+
throw $previous;
128+
} else {
129+
throw $e;
130+
}
124131
}
125132
}
126133
}

src/Qcloud/Cos/CommandToRequestTransformer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public function metadataTransformer(CommandInterface $command, $request) {
130130
$request = $request->withHeader('x-cos-meta-' . $key, $value);
131131
}
132132
}
133+
$request = headersMap($command, $request);
133134
return $request;
134135
}
135136

src/Qcloud/Cos/Common.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,16 @@ function endWith($haystack, $needle) {
3333
}
3434
return (substr($haystack, -$length) === $needle);
3535
}
36+
37+
function headersMap($command, $request) {
38+
$headermap = array(
39+
'TransferEncoding'=>'Transfer-Encoding',
40+
'ChannelId'=>'x-cos-channel-id'
41+
);
42+
foreach ($headermap as $key => $value) {
43+
if (isset($command[$key])) {
44+
$request = $request->withHeader($value, $command[$key]);
45+
}
46+
}
47+
return $request;
48+
}

src/Qcloud/Cos/Tests/Test.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public function testValidRegionBucket()
6868
$regionlist = array('cn-east','ap-shanghai',
6969
'cn-south','ap-guangzhou',
7070
'cn-north','ap-beijing-1',
71-
'cn-south-2','ap-guangzhou-2',
7271
'cn-southwest','ap-chengdu',
7372
'sg','ap-singapore',
7473
'tj','ap-beijing-1',

0 commit comments

Comments
 (0)