Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions autorest/codegen/models/code_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,11 @@ def generate_single_parameter_from_multiple_media_types(self) -> None:
raise ValueError("You are missing a parameter that has multiple media types")
chosen_parameter.multiple_media_types_type_annot = f"Union[{type_annot}]"
chosen_parameter.multiple_media_types_docstring_type = docstring_type

@property
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally would've had this code in jinja2, but spent a long time and don't think I can get the jinja2 template to correctly filter by property 'has_lro'. Always defaults to what an Operation model has for this value: False

def has_lro_operations(self) -> bool:
return any([
operation.is_lro
for operation_group in self.operation_groups
for operation in operation_group.operations
])
4 changes: 4 additions & 0 deletions autorest/codegen/models/lro_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,7 @@ def imports(self, code_model, async_mode: bool) -> FileImport:
else:
file_import.add_from_import("azure.core.polling.base_polling", "LROBasePolling", ImportType.AZURECORE)
return file_import

@property
def is_lro(self):
return True
9 changes: 8 additions & 1 deletion autorest/codegen/models/lro_paging_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,11 @@ def imports(self, code_model, async_mode: bool) -> FileImport:
file_import = lro_imports
file_import.merge(paging_imports)
return file_import


@property
def is_lro(self):
return True

@property
def is_paging(self):
return True
8 changes: 8 additions & 0 deletions autorest/codegen/models/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@ def imports(self, code_model, async_mode: bool) -> FileImport:

return file_import

@property
def is_lro(self):
return False

@property
def is_paging(self):
return False

@classmethod
def from_yaml(cls, yaml_data: Dict[str, Any]) -> "Operation":
name = yaml_data["language"]["python"]["name"]
Expand Down
4 changes: 4 additions & 0 deletions autorest/codegen/models/paging_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ def success_status_code(self) -> List[Union[str, int]]:
return [200]
return super(PagingOperation, self).success_status_code

@property
def is_paging(self):
return True

def imports(self, code_model, async_mode: bool) -> FileImport:
file_import = super(PagingOperation, self).imports(code_model, async_mode)

Expand Down
10 changes: 0 additions & 10 deletions autorest/codegen/serializers/metadata_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
CodeModel,
Operation,
OperationGroup,
LROOperation,
PagingOperation,
CredentialSchema,
ParameterList,
TypingSection,
Expand Down Expand Up @@ -83,12 +81,6 @@ def _make_async_copy_of_global_parameters(self) -> ParameterList:
return global_parameters

def serialize(self) -> str:
def _is_lro(operation):
return isinstance(operation, LROOperation)

def _is_paging(operation):
return isinstance(operation, PagingOperation)

mixin_operation_group: Optional[OperationGroup] = next(
(operation_group
for operation_group in self.code_model.operation_groups if operation_group.is_empty_operation_group),
Expand Down Expand Up @@ -122,8 +114,6 @@ def _is_paging(operation):
async_global_parameters=async_global_parameters,
mixin_operations=mixin_operations,
any=any,
is_lro=_is_lro,
is_paging=_is_paging,
str=str,
sync_mixin_imports=(
_json_serialize_imports(sync_mixin_imports.imports)
Expand Down
10 changes: 1 addition & 9 deletions autorest/codegen/serializers/operation_group_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from jinja2 import Environment

from .import_serializer import FileImportSerializer
from ..models import LROOperation, PagingOperation, CodeModel, OperationGroup
from ..models import CodeModel, OperationGroup


class OperationGroupSerializer:
Expand All @@ -19,12 +19,6 @@ def __init__(
self.async_mode = async_mode

def serialize(self) -> str:
def _is_lro(operation):
return isinstance(operation, LROOperation)

def _is_paging(operation):
return isinstance(operation, PagingOperation)

operation_group_template = self.env.get_template("operations_container.py.jinja2")
if self.operation_group.is_empty_operation_group:
operation_group_template = self.env.get_template("operations_container_mixin.py.jinja2")
Expand All @@ -40,6 +34,4 @@ def _is_paging(operation):
is_python_3_file=self.async_mode
),
async_mode=self.async_mode,
is_lro=_is_lro,
is_paging=_is_paging,
)
19 changes: 10 additions & 9 deletions autorest/codegen/templates/metadata.json.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
{% set base_url = code_model.base_url if code_model.base_url else ('https://management.azure.com' if code_model.options["azure_arm"] else None )%}
"base_url": {{ (keywords.escape_str(base_url) if base_url else None) | tojson }},
"custom_base_url": {{ (keywords.escape_str(code_model.custom_base_url) if code_model.custom_base_url else None) | tojson }},
"azure_arm": {{ code_model.options["azure_arm"] | tojson }}
"azure_arm": {{ code_model.options["azure_arm"] | tojson }},
"has_lro_operations": {{ code_model.has_lro_operations | tojson }}
},
"global_parameters": {
"sync_method": {
Expand Down Expand Up @@ -57,16 +58,16 @@
},
"operation_mixins": {
{% for operation in mixin_operations %}
{% set operation_name = "begin_" + operation.name if is_lro(operation) else operation.name %}
{% set operation_name = "begin_" + operation.name if operation.is_lro else operation.name %}
{{ operation_name | tojson }} : {
"sync": {
{% if is_lro(operation) and is_paging(operation) %}
{% if operation.is_lro and operation.is_paging %}
{% from "lro_paging_operation.py.jinja2" import operation_docstring with context %}
{% set sync_return_type_wrapper = ["LROPoller", "ItemPaged"] %}
{% elif is_lro(operation) %}
{% elif operation.is_lro %}
{% from "lro_operation.py.jinja2" import operation_docstring with context %}
{% set sync_return_type_wrapper = ["LROPoller"] %}
{% elif is_paging(operation) %}
{% elif operation.is_paging %}
{% from "paging_operation.py.jinja2" import operation_docstring with context %}
{% set sync_return_type_wrapper = ["ItemPaged"] %}
{% else %}
Expand All @@ -77,15 +78,15 @@
"doc": {{ operation_docstring(async_mode=False) | tojson }}
},
"async": {
{% set coroutine = False if is_paging(operation) else True %}
{% set coroutine = False if operation.is_paging else True %}
"coroutine": {{ coroutine | tojson }},
{% if is_lro(operation) and is_paging(operation) %}
{% if operation.is_lro and operation.is_paging %}
{% from "lro_paging_operation.py.jinja2" import operation_docstring with context %}
{% set async_return_type_wrapper = ["AsyncLROPoller", "AsyncItemPaged"] %}
{% elif is_lro(operation) %}
{% elif operation.is_lro %}
{% from "lro_operation.py.jinja2" import operation_docstring with context %}
{% set async_return_type_wrapper = ["AsyncLROPoller"] %}
{% elif is_paging(operation) %}
{% elif operation.is_paging %}
{% from "paging_operation.py.jinja2" import operation_docstring with context %}
{% set async_return_type_wrapper = ["AsyncItemPaged"] %}
{% else %}
Expand Down
6 changes: 3 additions & 3 deletions autorest/codegen/templates/operations_container.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ class {{ operation_group.class_name }}{{ object_base_class }}:
self._config = config
{% for operation in operation_group.operations %}

{% if is_lro(operation) and is_paging(operation) %}
{% if operation.is_lro and operation.is_paging %}
{% macro someop() %}{% include "lro_paging_operation.py.jinja2" %}{% endmacro %}
{% elif is_lro(operation) %}
{% elif operation.is_lro %}
{% macro someop() %}{% include "lro_operation.py.jinja2" %}{% endmacro %}
{% elif is_paging(operation) %}
{% elif operation.is_paging %}
{% macro someop() %}{% include "paging_operation.py.jinja2" %}{% endmacro %}
{% else %}
{% macro someop() %}{% include "operation.py.jinja2" %}{% endmacro %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
class {{ operation_group.class_name }}{{ object_base_class }}:
{% for operation in operation_group.operations %}

{% if is_lro(operation) and is_paging(operation) %}
{% if operation.is_lro and operation.is_paging %}
{%- macro someop() %}{% include "lro_paging_operation.py.jinja2" %}{% endmacro %}
{% elif is_lro(operation) %}
{% elif operation.is_lro %}
{%- macro someop() %}{% include "lro_operation.py.jinja2" %}{% endmacro %}
{% elif is_paging(operation) %}
{% elif operation.is_paging %}
{% macro someop() %}{% include "paging_operation.py.jinja2" %}{% endmacro %}
{% else %}
{% macro someop() %}{% include "operation.py.jinja2" %}{% endmacro %}
Expand Down
2 changes: 2 additions & 0 deletions autorest/codegen/templates/service_client.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ class {{ code_model.class_name }}({{ base_class }}):
{% if not code_model.custom_base_url %}
:param str base_url: Service URL
{% endif %}
{% if code_model.has_lro_operations %}
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
{% endif %}
"""

{{ method_signature()|indent }}
Expand Down
14 changes: 13 additions & 1 deletion autorest/multiapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,17 @@ def _merge_mixin_imports_across_versions(

return imports

def _has_lro_operations(
self, paths_to_versions: List[Path]
) -> bool:
has_lro_operations = False
for version_path in paths_to_versions:
metadata_json = json.loads(self._autorestapi.read_file(version_path / "_metadata.json"))
current_client_has_lro_operations = metadata_json["client"]["has_lro_operations"]
if current_client_has_lro_operations:
has_lro_operations = True
return has_lro_operations

def process(self) -> bool:
_LOGGER.info("Generating multiapi client")
# If True, means the auto-profile will consider preview versions.
Expand Down Expand Up @@ -417,7 +428,8 @@ def process(self) -> bool:
"async_imports": str(FileImportSerializer(async_imports, is_python_3_file=True)),
"base_url": metadata_json["client"]["base_url"],
"custom_base_url_to_api_version": self._build_custom_base_url_to_api_version(paths_to_versions),
"azure_arm": metadata_json["client"]["azure_arm"]
"azure_arm": metadata_json["client"]["azure_arm"],
"has_lro_operations": self._has_lro_operations(paths_to_versions)
}

multiapi_serializer = MultiAPISerializer(
Expand Down
2 changes: 2 additions & 0 deletions autorest/multiapi/templates/multiapi_service_client.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ class {{ client_name }}({% if mixin_operations %}{{ client_name }}OperationsMixi
{% endif %}
:param profile: A profile definition, from KnownProfiles to dict.
:type profile: azure.profiles.KnownProfiles
{% if has_lro_operations %}
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
{% endif %}
"""

DEFAULT_API_VERSION = '{{ last_api_version }}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class AutoRestDurationTestService(object):
:ivar duration: DurationOperations operations
:vartype duration: bodyduration.operations.DurationOperations
:param str base_url: Service URL
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class AutoRestDurationTestService(object):
:ivar duration: DurationOperations operations
:vartype duration: bodyduration.aio.operations_async.DurationOperations
:param str base_url: Service URL
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class AutoRestParameterGroupingTestService(object):
:ivar parameter_grouping: ParameterGroupingOperations operations
:vartype parameter_grouping: azureparametergrouping.operations.ParameterGroupingOperations
:param str base_url: Service URL
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class AutoRestParameterGroupingTestService(object):
:ivar parameter_grouping: ParameterGroupingOperations operations
:vartype parameter_grouping: azureparametergrouping.aio.operations_async.ParameterGroupingOperations
:param str base_url: Service URL
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class AutoRestReportServiceForAzure(AutoRestReportServiceForAzureOperationsMixin
"""Test Infrastructure for AutoRest.

:param str base_url: Service URL
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class AutoRestReportServiceForAzure(AutoRestReportServiceForAzureOperationsMixin
"""Test Infrastructure for AutoRest.

:param str base_url: Service URL
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class AutoRestAzureSpecialParametersTestClient(object):
:param subscription_id: The subscription id, which appears in the path, always modeled in credentials. The value is always '1234-5678-9012-3456'.
:type subscription_id: str
:param str base_url: Service URL
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class AutoRestAzureSpecialParametersTestClient(object):
:param subscription_id: The subscription id, which appears in the path, always modeled in credentials. The value is always '1234-5678-9012-3456'.
:type subscription_id: str
:param str base_url: Service URL
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class AutoRestParameterizedHostTestClient(object):
:vartype paths: custombaseurl.operations.PathsOperations
:param host: A string value that is used as a global part of the parameterized host.
:type host: str
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class AutoRestParameterizedHostTestClient(object):
:vartype paths: custombaseurl.aio.operations_async.PathsOperations
:param host: A string value that is used as a global part of the parameterized host.
:type host: str
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class AutoRestParameterizedHostTestPagingClient(object):
:type credential: ~azure.core.credentials.TokenCredential
:param host: A string value that is used as a global part of the parameterized host.
:type host: str
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class AutoRestParameterizedHostTestPagingClient(object):
:type credential: ~azure.core.credentials_async.AsyncTokenCredential
:param host: A string value that is used as a global part of the parameterized host.
:type host: str
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class AutoRestHeadTestService(object):
:param credential: Credential needed for the client to connect to Azure.
:type credential: ~azure.core.credentials.TokenCredential
:param str base_url: Service URL
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class AutoRestHeadTestService(object):
:param credential: Credential needed for the client to connect to Azure.
:type credential: ~azure.core.credentials_async.AsyncTokenCredential
:param str base_url: Service URL
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class AutoRestHeadExceptionTestService(object):
:param credential: Credential needed for the client to connect to Azure.
:type credential: ~azure.core.credentials.TokenCredential
:param str base_url: Service URL
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class AutoRestHeadExceptionTestService(object):
:param credential: Credential needed for the client to connect to Azure.
:type credential: ~azure.core.credentials_async.AsyncTokenCredential
:param str base_url: Service URL
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class AutoRestHeadTestService(object):
:param credential: Credential needed for the client to connect to Azure.
:type credential: ~azure.core.credentials.TokenCredential
:param str base_url: Service URL
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class AutoRestHeadTestService(object):
:param credential: Credential needed for the client to connect to Azure.
:type credential: ~azure.core.credentials_async.AsyncTokenCredential
:param str base_url: Service URL
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
"""

def __init__(
Expand Down
Loading