Skip to content

Commit

Permalink
- Fixed ROA signature. (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
aliguyong authored Jul 4, 2020
1 parent d476d16 commit 1782a72
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 48 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 1.5.25 - 2020-07-04
- Fixed ROA signature.
- Deprecated `LogFormatter`.

## 1.5.24 - 2020-06-04
- Fixed Resolve Host.

Expand Down
2 changes: 1 addition & 1 deletion src/AlibabaCloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AlibabaCloud
/**
* Version of the Client
*/
const VERSION = '1.5.24';
const VERSION = '1.5.25';

/**
* This static method can directly call the specific service.
Expand Down
6 changes: 5 additions & 1 deletion src/Encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public function toString()
$string = '';
foreach ($this->data as $key => $value) {
$encode = rawurlencode($value);
$string .= "$key=$encode&";
if ($encode === '') {
$string .= "$key&";
} else {
$string .= "$key=$encode&";
}
}

if (0 < count($this->data)) {
Expand Down
3 changes: 2 additions & 1 deletion src/Log/LogFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
use Psr\Http\Message\ResponseInterface;

/**
* @deprecated Use GuzzleHttp\MessageFormatter.
* Class LogFormatter
*
* @package AlibabaCloud\Client\Log
*/
class LogFormatter extends MessageFormatter
class LogFormatter
{
/**
* @var float
Expand Down
48 changes: 24 additions & 24 deletions src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@

namespace AlibabaCloud\Client\Request;

use Exception;
use ArrayAccess;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\Middleware;
use AlibabaCloud\Client\SDK;
use GuzzleHttp\HandlerStack;
use AlibabaCloud\Client\Encode;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Filter\Filter;
use AlibabaCloud\Client\Result\Result;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Promise\PromiseInterface;
use AlibabaCloud\Client\Credentials\Providers\CredentialsProvider;
use AlibabaCloud\Client\Encode;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Client\Filter\ApiFilter;
use AlibabaCloud\Client\Log\LogFormatter;
use AlibabaCloud\Client\Traits\HttpTrait;
use GuzzleHttp\Exception\GuzzleException;
use AlibabaCloud\Client\Filter\HttpFilter;
use AlibabaCloud\Client\Traits\RegionTrait;
use AlibabaCloud\Client\Filter\ClientFilter;
use AlibabaCloud\Client\Filter\Filter;
use AlibabaCloud\Client\Filter\HttpFilter;
use AlibabaCloud\Client\Request\Traits\AcsTrait;
use AlibabaCloud\Client\Traits\ArrayAccessTrait;
use AlibabaCloud\Client\Traits\ObjectAccessTrait;
use AlibabaCloud\Client\Request\Traits\RetryTrait;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Client\Request\Traits\ClientTrait;
use AlibabaCloud\Client\Request\Traits\DeprecatedTrait;
use AlibabaCloud\Client\Credentials\Providers\CredentialsProvider;
use AlibabaCloud\Client\Request\Traits\RetryTrait;
use AlibabaCloud\Client\Result\Result;
use AlibabaCloud\Client\SDK;
use AlibabaCloud\Client\Traits\ArrayAccessTrait;
use AlibabaCloud\Client\Traits\HttpTrait;
use AlibabaCloud\Client\Traits\ObjectAccessTrait;
use AlibabaCloud\Client\Traits\RegionTrait;
use ArrayAccess;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\MessageFormatter;
use GuzzleHttp\Middleware;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\ResponseInterface;

/**
* Class Request
Expand Down Expand Up @@ -382,7 +382,7 @@ public static function createClient(Request $request = null)
if (AlibabaCloud::getLogger()) {
$stack->push(Middleware::log(
AlibabaCloud::getLogger(),
new LogFormatter(AlibabaCloud::getLogFormat())
new MessageFormatter(AlibabaCloud::getLogFormat())
));
}

Expand Down
37 changes: 35 additions & 2 deletions src/Traits/LogTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace AlibabaCloud\Client\Traits;

use DateTime;
use DateTimeZone;
use Exception;
use Psr\Log\LoggerInterface;

Expand All @@ -17,11 +19,21 @@ trait LogTrait
*/
private static $logger;

/**
* @var float
*/
private static $logStartTime = 0;

/**
* @var string
*/
private static $logFormat;

/**
* @var DateTime
*/
private static $ts;

/**
* @return LoggerInterface
*/
Expand All @@ -37,16 +49,29 @@ public static function getLogger()
*/
public static function setLogger(LoggerInterface $logger)
{
self::$logger = $logger;
self::$logger = $logger;
self::$logStartTime = microtime(true);
$timezone = new DateTimeZone(date_default_timezone_get() ?: 'UTC');
if (PHP_VERSION_ID < 70100) {
self::$ts = DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), $timezone);
} else {
self::$ts = new DateTime(null, $timezone);
}
}

/**
* @return string
*/
public static function getLogFormat()
{
return self::$logFormat
$template = self::$logFormat
?: '"{method} {uri} HTTP/{version}" {code} {cost} {hostname} {pid}';

return str_replace(
['{pid}', '{cost}', '{start_time}'],
[getmypid(), self::getCost(), self::$ts->format('Y-m-d H:i:s.u')],
$template
);
}

/**
Expand All @@ -61,4 +86,12 @@ public static function setLogFormat($formatter)
{
self::$logFormat = $formatter;
}

/**
* @return float|mixed
*/
private static function getCost()
{
return microtime(true) - self::$logStartTime;
}
}
12 changes: 6 additions & 6 deletions tests/Feature/Request/RequestAsyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace AlibabaCloud\Client\Tests\Feature\Request;

use Exception;
use Stringy\Stringy;
use PHPUnit\Framework\TestCase;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Result\Result;
use Psr\Http\Message\ResponseInterface;
use Exception;
use GuzzleHttp\Exception\RequestException;
use AlibabaCloud\Client\Exception\ClientException;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Stringy\Stringy;

/**
* Class RequestAsyncTest
Expand Down Expand Up @@ -86,7 +86,7 @@ static function (ResponseInterface $res) {

return $res;
},
static function (RequestException $e) {
static function (Exception $e) {
self::assertTrue(Stringy::create($e->getMessage())->contains('cURL error'));
}
)->wait();
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Clients/RamRoleArnClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testGetSessionCredential(RamRoleArnClient $client)
try {
$client->getSessionCredential();
} catch (ServerException $exception) {
self::assertEquals('Specified access key is not found.', $exception->getErrorMessage());
self::assertEquals('Specified access key is not found or invalid.', $exception->getErrorMessage());
} catch (ClientException $exception) {
self::assertStringStartsWith('cURL error', $exception->getErrorMessage());
}
Expand Down
32 changes: 20 additions & 12 deletions tests/Unit/Request/RoaRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

namespace AlibabaCloud\Client\Tests\Unit\Request;

use Stringy\Stringy;
use ReflectionObject;
use RuntimeException;
use ReflectionMethod;
use ReflectionException;
use GuzzleHttp\Psr7\Request;
use AlibabaCloud\Client\Encode;
use PHPUnit\Framework\TestCase;
use AlibabaCloud\Client\Accept;
use AlibabaCloud\Client\Support\Path;
use AlibabaCloud\Client\Support\Sign;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Request\RoaRequest;
use AlibabaCloud\Client\Credentials\BearerTokenCredential;
use AlibabaCloud\Client\Credentials\StsCredential;
use AlibabaCloud\Client\Encode;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Credentials\BearerTokenCredential;
use AlibabaCloud\Client\Request\RoaRequest;
use AlibabaCloud\Client\Support\Path;
use AlibabaCloud\Client\Support\Sign;
use AlibabaCloud\Client\Tests\Mock\Services\CS\DescribeClusterServicesRequest;
use GuzzleHttp\Psr7\Request;
use PHPUnit\Framework\TestCase;
use ReflectionException;
use ReflectionMethod;
use ReflectionObject;
use RuntimeException;
use Stringy\Stringy;

/**
* Class RoaRequestTest
Expand Down Expand Up @@ -154,6 +154,14 @@ public function buildQueryString()
],
'Version=2015-12-15&b=b&c=c',
],
[
[
'b' => 'b',
'c' => 'c',
'd' => '',
],
'Version=2015-12-15&b=b&c=c&d',
],
];
}

Expand Down

0 comments on commit 1782a72

Please sign in to comment.