Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code #152

Merged
merged 12 commits into from
May 27, 2022
25 changes: 13 additions & 12 deletions src/AblyRest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ class AblyRest {

static function ablyAgentHeader()
{
$sdk_identifier = 'ably-php/'.Defaults::LIB_VERSION;
$runtime_identifier = 'php/'.Miscellaneous::getNumeric(phpversion());
$agent_header = $sdk_identifier.' '.$runtime_identifier;
foreach(self::$agents as $agent_identifier => $agent_version) {
$agent_header.= ' '.$agent_identifier;
if (!empty($agent_version)) {
$agent_header.= '/'.$agent_version;
$sdkIdentifier = 'ably-php/'.Defaults::LIB_VERSION;
$runtimeIdentifier = 'php/'.Miscellaneous::getNumeric(phpversion());
$agentHeader = $sdkIdentifier.' '.$runtimeIdentifier;
foreach(self::$agents as $agentIdentifier => $agentVersion) {
$agentHeader.= ' '.$agentIdentifier;
if (!empty($agentVersion)) {
$agentHeader.= '/'.$agentVersion;
}
}
return $agent_header;
return $agentHeader;
}
/**
* @var \Ably\Http $http object for making HTTP requests
Expand Down Expand Up @@ -222,9 +222,10 @@ public function requestInternal( $method, $path, $headers = [], $params = [], $r
}

/**
* Does an HTTP request with automatic pagination, automatically injected
* auth headers and automatic server failure handling using fallbackHosts.
*
* RSC19 - This function is provided as a convenience for customers who wish to use REST API functionality that is
* either not documented or is not included in the API for our client libraries.
* The REST client library provides a function to issue HTTP requests to the Ably endpoints with all the built in
* functionality of the library such as authentication, paging, fallback hosts, MsgPack and JSON support
* @param string $method HTTP method (GET, POST, PUT, DELETE, PATCH, ...)
* @param string $path root-relative path, e.g. /channels/example/messages
* @param array $params GET parameters to append to $path
Expand All @@ -246,7 +247,7 @@ public function request( $method, $path, $params = [], $body = '', $headers = []
$body = json_encode( $body );
}

return new HttpPaginatedResponse( $this, 'Ably\Models\Untyped', null, $method, $path, $body, $headers );
return new HttpPaginatedResponse( $this, 'Ably\Models\Untyped', null, $method, $path, $body, $headers ); // RSC19d
}

// RTN17c
Expand Down
2 changes: 1 addition & 1 deletion src/Models/HttpPaginatedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Ably\Exceptions\AblyRequestException;

/**
* This class is used as a container for response data from AblyRest::request
* RSC19d, HP1 - This class is used as a container for response data from AblyRest::request
* It provides automatic pagination.
*/
class HttpPaginatedResponse extends PaginatedResult {
Expand Down
15 changes: 8 additions & 7 deletions tests/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ public function testAblyAgentHeader() {
$ably->time(); // make a request
$curlParams = $ably->http->getCurlLastParams();

$expected_agent_header = 'ably-php/'.Defaults::LIB_VERSION.' '.'php/'.Miscellaneous::getNumeric(phpversion());
$this->assertContains( 'Ably-Agent: '. $expected_agent_header, $curlParams[CURLOPT_HTTPHEADER],
$expectedAgentHeader = 'ably-php/'.Defaults::LIB_VERSION.' '.'php/'.Miscellaneous::getNumeric(phpversion());
$this->assertContains( 'Ably-Agent: '. $expectedAgentHeader, $curlParams[CURLOPT_HTTPHEADER],
'Expected Ably agent header in HTTP request' );

$ably = new AblyRest( $opts );
$ably->time(); // make a request

$curlParams = $ably->http->getCurlLastParams();

$this->assertContains( 'Ably-Agent: '. $expected_agent_header, $curlParams[CURLOPT_HTTPHEADER],
$this->assertContains( 'Ably-Agent: '. $expectedAgentHeader, $curlParams[CURLOPT_HTTPHEADER],
'Expected Ably agent header in HTTP request' );

AblyRest::setLibraryFlavourString( 'laravel');
Expand All @@ -77,8 +77,8 @@ public function testAblyAgentHeader() {

$curlParams = $ably->http->getCurlLastParams();

$expected_agent_header = 'ably-php/'.Defaults::LIB_VERSION.' '.'php/'.Miscellaneous::getNumeric(phpversion()).' laravel'.' customLib/2.3.5';
$this->assertContains( 'Ably-Agent: '. $expected_agent_header, $curlParams[CURLOPT_HTTPHEADER],
$expectedAgentHeader = 'ably-php/'.Defaults::LIB_VERSION.' '.'php/'.Miscellaneous::getNumeric(phpversion()).' laravel'.' customLib/2.3.5';
$this->assertContains( 'Ably-Agent: '. $expectedAgentHeader, $curlParams[CURLOPT_HTTPHEADER],
'Expected Ably agent header in HTTP request' );

AblyRest::setLibraryFlavourString();
Expand Down Expand Up @@ -147,7 +147,7 @@ public function testPOST() {
}

/**
* Test basic AblyRest::request functionality
* RSC19 Test basic AblyRest::request functionality
*/
public function testRequestBasic() {
$ably = self::$ably;
Expand Down Expand Up @@ -186,7 +186,7 @@ public function testRequestBasic() {
}

/**
* Test that Response handles various returned structures properly
* RSC19 - Test that Response handles various returned structures properly
*/
public function testRequestReturnValues() {
$ably = new AblyRest( [
Expand Down Expand Up @@ -290,3 +290,4 @@ private static function endsWith($haystack, $needle) {
return substr($haystack, -strlen($needle)) == $needle;
}
}