Skip to content

Commit

Permalink
Paging v2 (#101)
Browse files Browse the repository at this point in the history
* Paging v2

* Docstring update

* Save some lines

* Missing import for operation mixin

* Testserver 2.6.4
  • Loading branch information
lmazuel authored Jul 25, 2019
1 parent ca4e74e commit 8203ce8
Show file tree
Hide file tree
Showing 27 changed files with 459 additions and 719 deletions.
11 changes: 0 additions & 11 deletions src/azure/CodeGeneratorPya.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,6 @@ public override async Task Generate(CodeModel cm)
await Write(enumTemplate, Path.Combine(folderName, "models", "_" + codeModel.Name.ToPythonCase() + "_enums.py"));
}

// Page class
if (codeModel.PageModels.Any()) {
List<PagePya> pagedModels = codeModel.PageModels as List<PagePya>;
var pageTemplate = new PageTemplate
{
Model = pagedModels
};
await Write(pageTemplate, Path.Combine(folderName, "models", "_paged_models.py"));

}

// Async
if (noAsync != true)
{
Expand Down
6 changes: 4 additions & 2 deletions src/azure/Model/CodeModelPya.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ public class CodeModelPya : CodeModelPy
internal IDictionary<string, IDictionary<int, string>> PageClasses { get; } =
new Dictionary<string, IDictionary<int, string>>();

public IEnumerable<string> PagedClasses => PageModels.Select(t => t.TypeDefinitionName);

public override bool HasAnyModel =>
ModelTypes.Any(model =>
!model.Extensions.ContainsKey(AzureExtensions.ExternalExtension) ||
!(bool) model.Extensions[AzureExtensions.ExternalExtension]);

public bool HasAnyPagingOperation =>
MethodTemplateModels.Any(m =>
m.Extensions.ContainsKey(AzureExtensions.PageableExtension));

public bool HasAnyLongRunOperation =>
MethodTemplateModels.Any(m =>
m.Extensions.ContainsKey(AzureExtensions.LongRunningExtension) &&
Expand Down
2 changes: 2 additions & 0 deletions src/azure/Model/MethodGroupPya.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ public MethodGroupPya(string name) : base(name)
public bool HasAnyHttpResponseErrors => MethodTemplateModels.Any(item => !(CodeModel as CodeModelPya).AzureArm && item.DefaultResponse.Body == null);

public bool HasAnyLongRunOperation => MethodTemplateModels.Any(m => m.Extensions.ContainsKey(AzureExtensions.LongRunningExtension));

public bool HasAnyPagingOperation => MethodTemplateModels.Any(m => m.Extensions.ContainsKey(AzureExtensions.PageableExtension));
}
}
2 changes: 1 addition & 1 deletion src/azure/Model/MethodPya.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public IModelType PagedResponseClass
}
}

public IModelType PagedResponseContentClass { get; set; }
public PagePya PagedMetadata { get; set; }

public string ClientRequestIdString => AzureExtensions.GetClientRequestIdString(this);

Expand Down
24 changes: 6 additions & 18 deletions src/azure/Model/PagePya.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@

namespace AutoRest.Python.Azure.Model
{
public class PagePya : IEquatable<PagePya>
public class PagePya
{
private readonly string _typeDefinitionName;

public PagePya(string className, string nextLinkName, string itemName, IModelType itemType)
public PagePya(string className, Property nextLinkProp, Property itemProp, IModelType itemType)
{
this._typeDefinitionName = className;
this.NextLinkName = nextLinkName;
this.ItemName = itemName;
this.NextLinkProp = nextLinkProp;
this.ItemProp = itemProp;
this.ItemType = itemType;
}

public string NextLinkName { get; private set; }
public Property NextLinkProp { get; private set; }

public string ItemName { get; private set; }
public Property ItemProp { get; private set; }

public string TypeDefinitionName => CodeNamer.Instance.GetTypeName(_typeDefinitionName);

Expand All @@ -37,17 +37,5 @@ public string GetReturnTypeDocumentation()
}
return $":class:`{ItemType.Name} <{((CodeModelPy)ItemType.CodeModel)?.Namespace}.models.{ItemType.Name}>`";
}

public bool Equals(PagePya other)
{
if (other != null &&
this.NextLinkName.EqualsIgnoreCase(other.NextLinkName) &&
this.TypeDefinitionName.EqualsIgnoreCase(other.TypeDefinitionName) &&
this.ItemName.EqualsIgnoreCase(other.ItemName))
{
return true;
}
return false;
}
}
}
4 changes: 4 additions & 0 deletions src/azure/Templates/AzureMethodGroupTemplate.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ else
{
@:from azure.core.exceptions import map_error
}
@if (Model.HasAnyPagingOperation)
{
@:from azure.core.paging import ItemPaged
}
@if (Model.HasAnyLongRunOperation)
{
@:from azure.core.polling import LROPoller, NoPolling
Expand Down
4 changes: 4 additions & 0 deletions src/azure/Templates/AzureMethodGroupTemplateAsync.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ else
{
@:from azure.core.exceptions import map_error
}
@if (Model.HasAnyPagingOperation)
{
@:from azure.core.async_paging import AsyncItemPaged, AsyncList
}
@if (Model.HasAnyLongRunOperation)
{
@:from azure.core.polling.async_poller import async_poller, AsyncNoPolling
Expand Down
8 changes: 0 additions & 8 deletions src/azure/Templates/AzureModelInitTemplate.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ except (SyntaxError, ImportError):
}
@: from ._models import @modelType.Name@Model.GetExceptionNameIfExist(modelType, false)
}
@foreach (var pagedName in Model.PagedClasses.OrderBy(o => o))
{
@:from ._paged_models import @pagedName
}
@if (Model.EnumTypes.Any())
{
@:from ._@(Model.Name.ToPythonCase())_enums import (
Expand All @@ -52,10 +48,6 @@ __all__ = [
}
@: '@modelType.Name'@Model.GetExceptionNameIfExist(modelType, true),
}
@foreach (var pagedName in Model.PagedClasses)
{
@: '@pagedName',
}
@foreach (var enumType in @Model.EnumTypes)
{
@: '@(enumType.Name)',
Expand Down
69 changes: 38 additions & 31 deletions src/azure/Templates/AzurePagingMethodTemplate.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ sync_trace_decorator = "@distributed_trace";
@: @ParameterWrapComment(string.Empty, MethodPy.GetParameterDocumentationString(parameter))
@: @ParameterWrapComment(string.Empty, ":type " + parameter.Name + ": " + Model.GetDocumentationType(parameter.ModelType))
}
@ParameterWrapComment(string.Empty, ":return: An iterator like instance of " + Model.GetDocumentationTypeName(Model.PagedResponseContentClass))
@ParameterWrapComment(string.Empty, string.Format(":rtype: {0}[{1}]", Model.GetReturnTypeDocumentation(Model.ReturnType.Body), Model.GetReturnTypeDocumentation(Model.PagedResponseContentClass)))
@ParameterWrapComment(string.Empty, ":return: An iterator like instance of " + Model.GetDocumentationTypeName(Model.PagedMetadata.ItemType))
@if (!AsyncMode)
{
@: @ParameterWrapComment(string.Empty, string.Format(":rtype: ~azure.core.paging.ItemPaged[{0}]", Model.GetReturnTypeDocumentation(Model.PagedMetadata.ItemType)))
}
else
{
@: @ParameterWrapComment(string.Empty, string.Format(":rtype: ~azure.core.async_paging.AsyncItemPaged[{0}]", Model.GetReturnTypeDocumentation(Model.PagedMetadata.ItemType)))
}
@ParameterWrapComment(string.Empty, string.Format(":raises: {0}", Model.ExceptionDocumentation))
"""
@if (Model.Deprecated)
Expand All @@ -53,13 +60,11 @@ sync_trace_decorator = "@distributed_trace";
@EmptyLine
}
def prepare_request(next_link=None):
query_parameters = {}
if not next_link:
# Construct URL
url = self.@(((string)Model.Name).ToPythonCase()).metadata['url']
@(Model.BuildUrlPath("url", Model.LogicalParameters))
@EmptyLine
# Construct parameters
query_parameters = {}
@(Model.BuildUrlQuery("query_parameters", Model.LogicalParameters))
@EmptyLine
else:
Expand All @@ -72,7 +77,6 @@ else
@:url = next_link
}
@(Model.BuildUrlPath("url", Model.PagingParameters))
query_parameters = {}
@(Model.BuildUrlQuery("query_parameters", Model.PagingParameters))
@EmptyLine
# Construct headers
Expand Down Expand Up @@ -115,43 +119,46 @@ else
}
return request
@EmptyLine
def internal_paging(next_link=None):
error_map = kwargs.pop('error_map', None)
request = prepare_request(next_link)
@EmptyLine
pipeline_response = self._client._pipeline.run(request)
response = pipeline_response.http_response
@EmptyLine
if @Model.FailureStatusCodePredicate:
@Model.RaisedException
@if(!AsyncMode)
{
@: def extract_data(response):
@: deserialized = self._deserialize('@(Model.PagedResponseClass.Name)', response)
@: return deserialized.@(Model.PagedMetadata.NextLinkProp.Name), iter(deserialized.@(Model.PagedMetadata.ItemProp.Name))
@EmptyLine
return response
@: def get_next(next_link=None):
@: request = prepare_request(next_link)
@EmptyLine
@if(AsyncMode)
@: pipeline_response = self._client._pipeline.run(request)
}
else
{
@: async def internal_paging_async(next_link=None):
@: error_map = kwargs.pop('error_map', None)
@: async def extract_data_async(response):
@: deserialized = self._deserialize('@(Model.PagedResponseClass.Name)', response)
@: return deserialized.@(Model.PagedMetadata.NextLinkProp.Name), AsyncList(deserialized.@(Model.PagedMetadata.ItemProp.Name))
@EmptyLine
@: async def get_next_async(next_link=None):
@: request = prepare_request(next_link)
@EmptyLine
@:pipeline_response = await self._client._pipeline.run(request)
@:response = pipeline_response.http_response
@EmptyLine
@: if @Model.FailureStatusCodePredicate:
@: @Model.RaisedException
}
response = pipeline_response.http_response
@EmptyLine
@: return response
error_map = kwargs.pop('error_map', None)
if @Model.FailureStatusCodePredicate:
@Model.RaisedException
return response
@EmptyLine
}
# Deserialize response
@if(AsyncMode)
@if(!AsyncMode)
{
@:deserialized = models.@(Model.PagedResponseClass.Name)(
@:internal_paging, self._deserialize, async_command=internal_paging_async)
@:return ItemPaged(
@:get_next, extract_data
@:)
}
else
{
@:deserialized = models.@(Model.PagedResponseClass.Name)(internal_paging, self._deserialize)
@:return AsyncItemPaged(
@:get_next_async, extract_data_async
@:)
}
@EmptyLine
return deserialized
@(methodName).metadata = {'url': '@(Model.Url)'}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ else
{
@:from azure.core.exceptions import map_error
}
@if (Model.HasAnyPagingOperation)
{
@:from azure.core.paging import ItemPaged
}
@if (Model.HasAnyLongRunOperation)
{
@:from azure.core.polling import LROPoller, NoPolling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ else
{
@:from azure.core.exceptions import map_error
}
@if (Model.HasAnyPagingOperation)
{
@:from azure.core.async_paging import AsyncItemPaged, AsyncList
}
@if (Model.HasAnyLongRunOperation)
{
@:from azure.core.polling.async_poller import async_poller, AsyncNoPolling
Expand Down
30 changes: 0 additions & 30 deletions src/azure/Templates/PageTemplate.cshtml

This file was deleted.

Loading

0 comments on commit 8203ce8

Please sign in to comment.