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

Commit

Permalink
Python VGen update for bundling
Browse files Browse the repository at this point in the history
Introduces bundling into Python VGen. To accomodate bundling, the
defaults/options system has been reworked; ApiCallDefaults is removed.
Instead, a dictionary is maintained which associates defaults to each
method; the values in the dictionary are then merged with any
CallOptions passed in while a call is being made.

A corresponding change to GAX is here:
  googleapis/gax-python#28

Note that it is not currently possible to override the bundling
defaults when calling the API constructor; a later change will add
the ability to pass in a dictionary mapping method names to bundlers
in order to provide this functionality.

Change-Id: I434a6026882fa57ec0d4672bff8be4f4102c4a82
  • Loading branch information
geigerj committed Feb 24, 2016
1 parent 6394b1f commit 000146b
Show file tree
Hide file tree
Showing 8 changed files with 401 additions and 111 deletions.
127 changes: 79 additions & 48 deletions vgen/src/main/resources/io/gapi/vgen/py/main.snip
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,14 @@

@private serviceClass(service)
@let path_templates = {@pathTemplateSection(service)}, \
documentation = {@comments(service)}, \
page_descriptors = {@pageDescriptorSection(service)}
documentation = {@comments(service)}
class {@context.getVeneerName(service)}(object):
@if documentation
{@documentation}

@end
{@constantSection(service)}

@if page_descriptors
{@page_descriptors}

@end
@if path_templates
{@path_templates}

Expand Down Expand Up @@ -90,6 +85,12 @@
)
@end

@private methodConfigDict(service)
@join method : service.getMethods
method: MethodConfig(
@end
@end

@private initMethodSection(service)
@let stubModule = pyproto.getPbFileName(service), \
stubService = service.getSimpleName()
Expand Down Expand Up @@ -127,10 +128,8 @@
through this client
:type timeout: int
"""
self.defaults = api_callable.ApiCallDefaults(
timeout=timeout,
max_attempts=max_attempts,
is_idempotent_retrying=is_idempotent_retrying)
self._defaults = dict()
{@defaultsSection(service)}

self.stub = config.create_stub(
{@stubModule}.beta_create_{@stubService}_stub,
Expand All @@ -142,23 +141,74 @@
@end
@end

@private pageDescriptorName(method)
_{@context.upperCamelToUpperUnderscore(method.getSimpleName())}_DESCRIPTOR
@private defaultsSection(service)
@join method : service.getMethods
@let methodConfig = context.getApiConfig.getInterfaceConfig(service).getMethodConfig(method), \
methodName = context.upperCamelToLowerUnderscore(method.getSimpleName), \
isPageStreaming = methodConfig.isPageStreaming(), \
isBundling = methodConfig.isBundling()
self._defaults['{@methodName}'] = CallSettings(
timeout=timeout,
@if context.isIdempotent(method)
is_retrying=is_idempotent_retrying,
@end
max_attempts=max_attempts,
@if isPageStreaming
@let pageStreaming = methodConfig.getPageStreaming(), \
requestToken = pageStreaming.getRequestTokenField().getSimpleName(), \
responseToken = pageStreaming.getResponseTokenField().getSimpleName(), \
resources = pageStreaming.getResourcesField().getSimpleName()
page_descriptor=PageDescriptor(
'{@requestToken}',
'{@responseToken}',
'{@resources}',
),
@end
@end
@if isBundling
@let bundling = methodConfig.getBundling(), \
count = bundling.getElementCountThreshold(), \
bytes = bundling.getRequestByteThreshold(), \
delay = bundling.getDelayThresholdMillis()
bundler=bundling.Executor(BundleOptions(
@if count
element_count_threshold={@count},
@end
@if bytes
request_bytesize_threshold={@bytes},
@end
@if delay
delay_threshold={@delay}
@end
)
bundle_descriptor=BundleDescriptor(
{@bundleDescriptorBody(bundling, method)}
),
@end
@end
)
@end
@end
@end

@private pageDescriptorSection(service)
@let interfaceConfig = context.getApiConfig.getInterfaceConfig(service)
@join method : context.messages.filterPageStreamingMethods(interfaceConfig, service.getMethods)
@let pageStreaming = interfaceConfig.getMethodConfig(method).getPageStreaming(), \
requestToken = pageStreaming.getRequestTokenField().getSimpleName(), \
responseToken = pageStreaming.getResponseTokenField().getSimpleName(), \
resources = pageStreaming.getResourcesField().getSimpleName()
{@pageDescriptorName(method)} = PageDescriptor(
'{@requestToken}',
'{@responseToken}',
'{@resources}',
)
@end
@private bundleDescriptorBody(bundling, method)
@let bundledField = bundling.getBundledField().getSimpleName(), \
discriminatorFields = bundling.getDiscriminatorFields()
@if {@bundling.hasSubresponseField}
'{@bundledField}',
[
@join fieldSelector : bundling.getDiscriminatorFields() on {@", "}.add(BREAK)
'{@fieldSelector.getParamName}'
@end
],
'subresponse_field={@bundling.getSubresponseField().getSimpleName()}',
@else
'{@bundledField}',
[
@join fieldSelector : bundling.getDiscriminatorFields() on {@", "}.add(BREAK)
'{@fieldSelector.getParamName}'
@end
]
@end
@end
@end
Expand Down Expand Up @@ -227,14 +277,8 @@
@end

@private callableConstructor(method)
@if context.isIdempotent(method)
return api_callable.ApiCallable(
self.stub.{@method.getSimpleName()}, options=options, defaults=self.defaults,
is_idempotent=True)(req)
@else
return api_callable.ApiCallable(
self.stub.{@method.getSimpleName()}, options=options, defaults=self.defaults)(req)
@end
return api_callable.ApiCallable(
self.stub.{@method.getSimpleName()}, settings=settings)(req)
@end

@private flattenedMethod(service, method)
Expand All @@ -256,15 +300,9 @@
@if {@defaultMutableValues(flattenedParams)}
{@defaultMutableValues(flattenedParams)}
@end
@if {@isPageStreamingRequest}
standard_options = api_callable.CallOptions(
page_streaming={@pageDescriptorName(method)})
@else
standard_options = api_callable.CallOptions()
@end
standard_options.update(options)
req = {@methodInputModule}.{@inputTypeName}(
{@namedParameters(flattenedParams)})
settings = self._defaults['{@methodName}'].merge(options)
{@callableConstructor(method)}
@else
def {@methodName}(
Expand All @@ -273,15 +311,8 @@
@if documentation
{@documentation}
@end
@if {@isPageStreamingRequest}
standard_options = api_callable.CallOptions(
page_streaming={@pageDescriptorName(method)})
@else
standard_options = api_callable.CallOptions()
@end
standard_options.update(options)
standard_options.normalize()
req = {@methodInputModule}.{@inputTypeName}()
settings = self._defaults['{@methodName}'].merge(options)
{@callableConstructor(method)}
@end
@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,57 @@ namespace Google.Example.Library.V1
return @this.CreateBookAsync(request, cancellationToken: cancellationToken).ResponseAsync;
}

/// <summary>
/// Creates a series of books.
/// </summary>
/// <paramref name="shelf">
/// The shelf in which the series is created.
/// </paramref>
/// <paramref name="books">
/// The books to publish in the series.
/// </paramref>
/// <paramref name="edition">
/// The edition of the series
/// </paramref>
public static PublishSeriesResponse PublishSeries(this LibraryService.ILibraryServiceClient @this, Shelf shelf, IEnumerable<Book> books, uint edition)
{
PublishSeriesRequest request = new PublishSeriesRequest
{
Shelf = shelf,
Books = { books },
Edition = edition,
};

return @this.PublishSeries(request);
}
/// <summary>
/// Creates a series of books.
/// </summary>
/// <paramref name="shelf">
/// The shelf in which the series is created.
/// </paramref>
/// <paramref name="books">
/// The books to publish in the series.
/// </paramref>
/// <paramref name="edition">
/// The edition of the series
/// </paramref>
/// <paramref name="cancellationToken">
/// The cancellation token for the asynchronous operation. This defaults to the default
/// cancellation token, equivalent to <see cref="CancellationToken.None" />.
/// </paramref>
public static Task<PublishSeriesResponse> PublishSeriesAsync(this LibraryService.ILibraryServiceClient @this, Shelf shelf, IEnumerable<Book> books, uint edition, CancellationToken cancellationToken = default(CancellationToken))
{
PublishSeriesRequest request = new PublishSeriesRequest
{
Shelf = shelf,
Books = { books },
Edition = edition,
};

return @this.PublishSeriesAsync(request, cancellationToken: cancellationToken).ResponseAsync;
}

/// <summary>
/// Gets a book.
/// </summary>
Expand Down
38 changes: 38 additions & 0 deletions vgen/src/test/java/io/gapi/vgen/testdata/go_library.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,44 @@ func (api *LibraryServiceApi) createBook(ctx context.Context, req interface{}) (

// AUTO-GENERATED DOCUMENTATION AND METHOD -- see instructions at the top of the file for editing.

// Creates a series of books.
//
//
func (api *LibraryServiceApi) PublishSeries(ctx context.Context, req *google_example_library_v1.PublishSeriesRequest, opts ...gax.CallOption) (*google_example_library_v1.PublishSeriesResponse, error) {
opts = append(api.CallOptions, opts...)
resp, err := gax.Invoke(ctx, req, api.publishSeries, opts...)
if err != nil {
return nil, err
}
return resp.(*google_example_library_v1.PublishSeriesResponse), nil
}

// AUTO-GENERATED DOCUMENTATION AND METHOD -- see instructions at the top of the file for editing.

// Creates a series of books.
//
// shelf: The shelf in which the series is created.
//
// books: The books to publish in the series.
//
// edition: The edition of the series
//
//
//
func (api *LibraryServiceApi) PublishSeriesWith(ctx context.Context, shelf *google_example_library_v1.Shelf, books []*google_example_library_v1.Book, edition uint32, opts ...gax.CallOption) (*google_example_library_v1.PublishSeriesResponse, error) {
return api.PublishSeries(ctx, &google_example_library_v1.PublishSeriesRequest{
Shelf: shelf,
Books: books,
Edition: edition,
}, opts...)
}

func (api *LibraryServiceApi) publishSeries(ctx context.Context, req interface{}) (interface{}, error) {
return api.client.PublishSeries(ctx, req.(*google_example_library_v1.PublishSeriesRequest))
}

// AUTO-GENERATED DOCUMENTATION AND METHOD -- see instructions at the top of the file for editing.

// Gets a book.
//
//
Expand Down
Loading

0 comments on commit 000146b

Please sign in to comment.