Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
feat: add page streaming for php (#3318)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Dec 24, 2020
1 parent 3ff8ca9 commit 6de594b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,10 @@ static PageStreamingConfig createPageStreamingConfig(
if (pageSizeField == null) {
// TODO: Conform to design doc spec, once approved, for using non-standard paging fields
// (such as max_results for page_size)
if (language == TargetLanguage.JAVA && transportProtocol == TransportProtocol.HTTP) {
pageSizeField = methodModel.getInputField(pagingParams.getNameForMaxResults());
if (transportProtocol == TransportProtocol.HTTP) {
if (language == TargetLanguage.JAVA || language == TargetLanguage.PHP) {
pageSizeField = methodModel.getInputField(pagingParams.getNameForMaxResults());
}
}
}
ProtoField responseTokenField =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import com.google.protobuf.Descriptors.FieldDescriptor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -206,7 +207,7 @@ private ViewModel buildGapicClientViewModel(GapicInterfaceContext context) {
} else {
// PHP generates a client that only supports REST
apiImplClass.grpcClientTypeName("");
apiImplClass.stubs(new ArrayList<>());
apiImplClass.stubs(Collections.emptyList());
}

apiImplClass.apiMethods(methods.stream().collect(Collectors.toList()));
Expand Down
19 changes: 18 additions & 1 deletion src/main/resources/com/google/api/codegen/php/test.snip
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,11 @@
@join pageStreamingResponseView : test.pageStreamingResponseViews
${@pageStreamingResponseView.resourcesVarName} = iterator_to_array($response->{@pageStreamingResponseView.resourcesIterateMethod}());
$this->assertSame(1, count(${@pageStreamingResponseView.resourcesVarName}));
$this->assertEquals($expectedResponse->{@pageStreamingResponseView.resourcesFieldGetterName}()[0], ${@pageStreamingResponseView.resourcesVarName}[0]);
@if pageStreamingResponseView.resourcesFieldIsMap
{@mapFieldPageStreamingAsserts(pageStreamingResponseView, test.mockResponse.rpcResponseInitCode)}
@else
$this->assertEquals($expectedResponse->{@pageStreamingResponseView.resourcesFieldGetterName}()[0], ${@pageStreamingResponseView.resourcesVarName}[0]);
@end
@end

{@singleCallSuccessAsserts(test)}
Expand Down Expand Up @@ -553,3 +557,16 @@
@end
@end
@end

@private mapFieldPageStreamingAsserts(view, initCode)
@join line : initCode.lines
@if line.lineType == "MapInitLine"
@join mapEntry : line.initEntries on ","
$this->assertArrayHasKey({@mapEntry.key}, $expectedResponse->{@view.resourcesFieldGetterName}());
$this->assertArrayHasKey({@mapEntry.key}, ${@view.resourcesVarName});
$this->assertEquals($expectedResponse->{@view.resourcesFieldGetterName}()[{@mapEntry.key}], ${@view.resourcesVarName}[{@mapEntry.key}]);
@end
@end
@end
@end

Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,22 @@ class LibraryServiceGapicClient
* ```
* $libraryServiceClient = new LibraryServiceClient();
* try {
* $response = $libraryServiceClient->listAggregatedShelves();
* // Iterate over pages of elements
* $pagedResponse = $libraryServiceClient->listAggregatedShelves();
* foreach ($pagedResponse->iteratePages() as $page) {
* foreach ($page as $element) {
* // doSomethingWith($element);
* }
* }
*
*
* // Alternatively:
*
* // Iterate through all elements
* $pagedResponse = $libraryServiceClient->listAggregatedShelves();
* foreach ($pagedResponse->iterateAllElements() as $element) {
* // doSomethingWith($element);
* }
* } finally {
* $libraryServiceClient->close();
* }
Expand All @@ -1025,17 +1040,22 @@ class LibraryServiceGapicClient
* @param array $optionalArgs {
* Optional.
* @type int $maxResults
* Requested page size.
* The maximum number of resources contained in the underlying API
* response. The API may return fewer values in a page, even if
* there are additional values to be retrieved.
* @type string $pageToken
* A token identifying a page of results the server should return.
* A page token is used to specify a page of values to be returned.
* If no page token is specified (the default), the first page
* of values will be returned. Any page token used here must have
* been generated by a previous call to the API.
* @type RetrySettings|array $retrySettings
* Retry settings to use for this call. Can be a
* {@see Google\ApiCore\RetrySettings} object, or an associative array
* of retry settings parameters. See the documentation on
* {@see Google\ApiCore\RetrySettings} for example usage.
* }
*
* @return \Google\Example\Library\V1\ListAggregatedShelvesResponse
* @return \Google\ApiCore\PagedListResponse
*
* @throws ApiException if the remote call fails
* @experimental
Expand All @@ -1050,12 +1070,12 @@ class LibraryServiceGapicClient
$request->setPageToken($optionalArgs['pageToken']);
}

return $this->startCall(
return $this->getPagedListResponse(
'ListAggregatedShelves',
ListAggregatedShelvesResponse::class,
$optionalArgs,
ListAggregatedShelvesResponse::class,
$request
)->wait();
);
}

/**
Expand Down Expand Up @@ -3570,6 +3590,16 @@ return [
'totalPollTimeoutMillis' => '300000',
]
],
'ListAggregatedShelves' => [
'pageStreaming' => [
'requestPageTokenGetMethod' => 'getPageToken',
'requestPageTokenSetMethod' => 'setPageToken',
'requestPageSizeGetMethod' => 'getMaxResults',
'requestPageSizeSetMethod' => 'setMaxResults',
'responsePageTokenGetMethod' => 'getNextPageToken',
'resourcesGetMethod' => 'getShelves'
]
],
'ListBooks' => [
'pageStreaming' => [
'requestPageTokenGetMethod' => 'getPageToken',
Expand Down Expand Up @@ -4052,6 +4082,7 @@ use Google\Example\Library\V1\GetShelfRequest;
use Google\Example\Library\V1\Inventory;
use Google\Example\Library\V1\ListAggregatedShelvesRequest;
use Google\Example\Library\V1\ListAggregatedShelvesResponse;
use Google\Example\Library\V1\ListAggregatedShelvesResponse\ShelvesEntry;
use Google\Example\Library\V1\ListBooksRequest;
use Google\Example\Library\V1\ListBooksResponse;
use Google\Example\Library\V1\ListShelvesRequest;
Expand Down Expand Up @@ -4365,20 +4396,31 @@ class LibraryServiceClientTest extends GeneratedTest
$this->assertTrue($transport->isExhausted());

// Mock response
$nextPageToken = 'nextPageToken-1530815211';
$nextPageToken = '';
$shelvesItem = new Shelf();
$shelves = ['shelves' => $shelvesItem];
$expectedResponse = new ListAggregatedShelvesResponse();
$expectedResponse->setNextPageToken($nextPageToken);
$expectedResponse->setShelves($shelves);
$transport->addResponse($expectedResponse);

$response = $client->listAggregatedShelves();
$this->assertEquals($expectedResponse, $response);
$this->assertEquals($expectedResponse, $response->getPage()->getResponseObject());
$resources = iterator_to_array($response->iterateAllElements());
$this->assertSame(1, count($resources));


$this->assertArrayHasKey('shelves', $expectedResponse->getShelves());
$this->assertArrayHasKey('shelves', $resources);
$this->assertEquals($expectedResponse->getShelves()['shelves'], $resources['shelves']);


$actualRequests = $transport->popReceivedCalls();
$this->assertSame(1, count($actualRequests));
$actualFuncCall = $actualRequests[0]->getFuncCall();
$actualRequestObject = $actualRequests[0]->getRequestObject();
$this->assertSame('/google.example.library.v1.LibraryService/ListAggregatedShelves', $actualFuncCall);


$this->assertTrue($transport->isExhausted());
}

Expand Down

0 comments on commit 6de594b

Please sign in to comment.