Skip to content

Commit 1782a72

Browse files
authored
- Fixed ROA signature. (#220)
1 parent d476d16 commit 1782a72

File tree

9 files changed

+98
-48
lines changed

9 files changed

+98
-48
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 1.5.25 - 2020-07-04
4+
- Fixed ROA signature.
5+
- Deprecated `LogFormatter`.
6+
37
## 1.5.24 - 2020-06-04
48
- Fixed Resolve Host.
59

src/AlibabaCloud.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class AlibabaCloud
3030
/**
3131
* Version of the Client
3232
*/
33-
const VERSION = '1.5.24';
33+
const VERSION = '1.5.25';
3434

3535
/**
3636
* This static method can directly call the specific service.

src/Encode.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ public function toString()
4242
$string = '';
4343
foreach ($this->data as $key => $value) {
4444
$encode = rawurlencode($value);
45-
$string .= "$key=$encode&";
45+
if ($encode === '') {
46+
$string .= "$key&";
47+
} else {
48+
$string .= "$key=$encode&";
49+
}
4650
}
4751

4852
if (0 < count($this->data)) {

src/Log/LogFormatter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
use Psr\Http\Message\ResponseInterface;
1111

1212
/**
13+
* @deprecated Use GuzzleHttp\MessageFormatter.
1314
* Class LogFormatter
1415
*
1516
* @package AlibabaCloud\Client\Log
1617
*/
17-
class LogFormatter extends MessageFormatter
18+
class LogFormatter
1819
{
1920
/**
2021
* @var float

src/Request/Request.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@
22

33
namespace AlibabaCloud\Client\Request;
44

5-
use Exception;
6-
use ArrayAccess;
7-
use GuzzleHttp\Client;
8-
use GuzzleHttp\Psr7\Uri;
9-
use GuzzleHttp\Middleware;
10-
use AlibabaCloud\Client\SDK;
11-
use GuzzleHttp\HandlerStack;
12-
use AlibabaCloud\Client\Encode;
135
use AlibabaCloud\Client\AlibabaCloud;
14-
use AlibabaCloud\Client\Filter\Filter;
15-
use AlibabaCloud\Client\Result\Result;
16-
use Psr\Http\Message\ResponseInterface;
17-
use GuzzleHttp\Promise\PromiseInterface;
6+
use AlibabaCloud\Client\Credentials\Providers\CredentialsProvider;
7+
use AlibabaCloud\Client\Encode;
8+
use AlibabaCloud\Client\Exception\ClientException;
9+
use AlibabaCloud\Client\Exception\ServerException;
1810
use AlibabaCloud\Client\Filter\ApiFilter;
19-
use AlibabaCloud\Client\Log\LogFormatter;
20-
use AlibabaCloud\Client\Traits\HttpTrait;
21-
use GuzzleHttp\Exception\GuzzleException;
22-
use AlibabaCloud\Client\Filter\HttpFilter;
23-
use AlibabaCloud\Client\Traits\RegionTrait;
2411
use AlibabaCloud\Client\Filter\ClientFilter;
12+
use AlibabaCloud\Client\Filter\Filter;
13+
use AlibabaCloud\Client\Filter\HttpFilter;
2514
use AlibabaCloud\Client\Request\Traits\AcsTrait;
26-
use AlibabaCloud\Client\Traits\ArrayAccessTrait;
27-
use AlibabaCloud\Client\Traits\ObjectAccessTrait;
28-
use AlibabaCloud\Client\Request\Traits\RetryTrait;
29-
use AlibabaCloud\Client\Exception\ClientException;
30-
use AlibabaCloud\Client\Exception\ServerException;
3115
use AlibabaCloud\Client\Request\Traits\ClientTrait;
3216
use AlibabaCloud\Client\Request\Traits\DeprecatedTrait;
33-
use AlibabaCloud\Client\Credentials\Providers\CredentialsProvider;
17+
use AlibabaCloud\Client\Request\Traits\RetryTrait;
18+
use AlibabaCloud\Client\Result\Result;
19+
use AlibabaCloud\Client\SDK;
20+
use AlibabaCloud\Client\Traits\ArrayAccessTrait;
21+
use AlibabaCloud\Client\Traits\HttpTrait;
22+
use AlibabaCloud\Client\Traits\ObjectAccessTrait;
23+
use AlibabaCloud\Client\Traits\RegionTrait;
24+
use ArrayAccess;
25+
use Exception;
26+
use GuzzleHttp\Client;
27+
use GuzzleHttp\Exception\GuzzleException;
28+
use GuzzleHttp\HandlerStack;
29+
use GuzzleHttp\MessageFormatter;
30+
use GuzzleHttp\Middleware;
31+
use GuzzleHttp\Promise\PromiseInterface;
32+
use GuzzleHttp\Psr7\Uri;
33+
use Psr\Http\Message\ResponseInterface;
3434

3535
/**
3636
* Class Request
@@ -382,7 +382,7 @@ public static function createClient(Request $request = null)
382382
if (AlibabaCloud::getLogger()) {
383383
$stack->push(Middleware::log(
384384
AlibabaCloud::getLogger(),
385-
new LogFormatter(AlibabaCloud::getLogFormat())
385+
new MessageFormatter(AlibabaCloud::getLogFormat())
386386
));
387387
}
388388

src/Traits/LogTrait.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace AlibabaCloud\Client\Traits;
44

5+
use DateTime;
6+
use DateTimeZone;
57
use Exception;
68
use Psr\Log\LoggerInterface;
79

@@ -17,11 +19,21 @@ trait LogTrait
1719
*/
1820
private static $logger;
1921

22+
/**
23+
* @var float
24+
*/
25+
private static $logStartTime = 0;
26+
2027
/**
2128
* @var string
2229
*/
2330
private static $logFormat;
2431

32+
/**
33+
* @var DateTime
34+
*/
35+
private static $ts;
36+
2537
/**
2638
* @return LoggerInterface
2739
*/
@@ -37,16 +49,29 @@ public static function getLogger()
3749
*/
3850
public static function setLogger(LoggerInterface $logger)
3951
{
40-
self::$logger = $logger;
52+
self::$logger = $logger;
53+
self::$logStartTime = microtime(true);
54+
$timezone = new DateTimeZone(date_default_timezone_get() ?: 'UTC');
55+
if (PHP_VERSION_ID < 70100) {
56+
self::$ts = DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), $timezone);
57+
} else {
58+
self::$ts = new DateTime(null, $timezone);
59+
}
4160
}
4261

4362
/**
4463
* @return string
4564
*/
4665
public static function getLogFormat()
4766
{
48-
return self::$logFormat
67+
$template = self::$logFormat
4968
?: '"{method} {uri} HTTP/{version}" {code} {cost} {hostname} {pid}';
69+
70+
return str_replace(
71+
['{pid}', '{cost}', '{start_time}'],
72+
[getmypid(), self::getCost(), self::$ts->format('Y-m-d H:i:s.u')],
73+
$template
74+
);
5075
}
5176

5277
/**
@@ -61,4 +86,12 @@ public static function setLogFormat($formatter)
6186
{
6287
self::$logFormat = $formatter;
6388
}
89+
90+
/**
91+
* @return float|mixed
92+
*/
93+
private static function getCost()
94+
{
95+
return microtime(true) - self::$logStartTime;
96+
}
6497
}

tests/Feature/Request/RequestAsyncTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace AlibabaCloud\Client\Tests\Feature\Request;
44

5-
use Exception;
6-
use Stringy\Stringy;
7-
use PHPUnit\Framework\TestCase;
85
use AlibabaCloud\Client\AlibabaCloud;
6+
use AlibabaCloud\Client\Exception\ClientException;
97
use AlibabaCloud\Client\Result\Result;
10-
use Psr\Http\Message\ResponseInterface;
8+
use Exception;
119
use GuzzleHttp\Exception\RequestException;
12-
use AlibabaCloud\Client\Exception\ClientException;
10+
use PHPUnit\Framework\TestCase;
11+
use Psr\Http\Message\ResponseInterface;
12+
use Stringy\Stringy;
1313

1414
/**
1515
* Class RequestAsyncTest
@@ -86,7 +86,7 @@ static function (ResponseInterface $res) {
8686

8787
return $res;
8888
},
89-
static function (RequestException $e) {
89+
static function (Exception $e) {
9090
self::assertTrue(Stringy::create($e->getMessage())->contains('cURL error'));
9191
}
9292
)->wait();

tests/Unit/Clients/RamRoleArnClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testGetSessionCredential(RamRoleArnClient $client)
5353
try {
5454
$client->getSessionCredential();
5555
} catch (ServerException $exception) {
56-
self::assertEquals('Specified access key is not found.', $exception->getErrorMessage());
56+
self::assertEquals('Specified access key is not found or invalid.', $exception->getErrorMessage());
5757
} catch (ClientException $exception) {
5858
self::assertStringStartsWith('cURL error', $exception->getErrorMessage());
5959
}

tests/Unit/Request/RoaRequestTest.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22

33
namespace AlibabaCloud\Client\Tests\Unit\Request;
44

5-
use Stringy\Stringy;
6-
use ReflectionObject;
7-
use RuntimeException;
8-
use ReflectionMethod;
9-
use ReflectionException;
10-
use GuzzleHttp\Psr7\Request;
11-
use AlibabaCloud\Client\Encode;
12-
use PHPUnit\Framework\TestCase;
135
use AlibabaCloud\Client\Accept;
14-
use AlibabaCloud\Client\Support\Path;
15-
use AlibabaCloud\Client\Support\Sign;
166
use AlibabaCloud\Client\AlibabaCloud;
17-
use AlibabaCloud\Client\Request\RoaRequest;
7+
use AlibabaCloud\Client\Credentials\BearerTokenCredential;
188
use AlibabaCloud\Client\Credentials\StsCredential;
9+
use AlibabaCloud\Client\Encode;
1910
use AlibabaCloud\Client\Exception\ClientException;
20-
use AlibabaCloud\Client\Credentials\BearerTokenCredential;
11+
use AlibabaCloud\Client\Request\RoaRequest;
12+
use AlibabaCloud\Client\Support\Path;
13+
use AlibabaCloud\Client\Support\Sign;
2114
use AlibabaCloud\Client\Tests\Mock\Services\CS\DescribeClusterServicesRequest;
15+
use GuzzleHttp\Psr7\Request;
16+
use PHPUnit\Framework\TestCase;
17+
use ReflectionException;
18+
use ReflectionMethod;
19+
use ReflectionObject;
20+
use RuntimeException;
21+
use Stringy\Stringy;
2222

2323
/**
2424
* Class RoaRequestTest
@@ -154,6 +154,14 @@ public function buildQueryString()
154154
],
155155
'Version=2015-12-15&b=b&c=c',
156156
],
157+
[
158+
[
159+
'b' => 'b',
160+
'c' => 'c',
161+
'd' => '',
162+
],
163+
'Version=2015-12-15&b=b&c=c&d',
164+
],
157165
];
158166
}
159167

0 commit comments

Comments
 (0)