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

regen and move code model specific args to metadata.json #867

Merged
merged 1 commit into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions autorest/codegen/models/code_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,9 @@ def has_lro_operations(self) -> bool:
for operation_group in self.operation_groups
for operation in operation_group.operations
])

@staticmethod
def base_url_method_signature(async_mode: bool) -> str:
if async_mode:
return "base_url: Optional[str] = None,"
return "base_url=None, # type: Optional[str]"
2 changes: 2 additions & 0 deletions autorest/codegen/serializers/metadata_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ def _is_paging(operation):

# setting to true, because for multiapi we always generate with a version file with version 0.1.0
self.code_model.options['package_version'] = '0.1.0'
if self.code_model.options['azure_arm'] and not self.code_model.base_url:
self.code_model.base_url = 'https://management.azure.com'
return template.render(
chosen_version=chosen_version,
total_api_version_list=total_api_version_list,
Expand Down
51 changes: 48 additions & 3 deletions autorest/codegen/templates/metadata.json.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"name": {{ code_model.class_name | tojson }},
"filename": {{ ("_" + code_model.module_name) | tojson }},
"description": {{ code_model.description | tojson }},
{% 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 }},
"base_url": {{ (keywords.escape_str(code_model.base_url) if code_model.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 }},
"has_lro_operations": {{ code_model.has_lro_operations | tojson }},
Expand Down Expand Up @@ -42,7 +41,53 @@
{{ gp.serialized_name | tojson }}: {{ gp.constant_declaration | tojson }}{{ "," if not loop.last else "" }}
{% endfor %}
},
"call": {{ code_model.global_parameters.method | map(attribute="serialized_name") | join(', ') | tojson }}
"call": {{ code_model.global_parameters.method | map(attribute="serialized_name") | join(', ') | tojson }},
"service_client_specific": {
"sync": {
"api_version": {
"signature": "api_version=None, # type: Optional[str]",
"description": "API version to use if no profile is provided, or if missing in profile.",
"docstring_type": "str",
"required": false
},
{% if not code_model.custom_base_url %}
"base_url": {
"signature": {{ code_model.base_url_method_signature(False) | tojson }},
"description": "Service URL",
"docstring_type": "str",
"required": false
},
{% endif %}
"profile": {
"signature": "profile=KnownProfiles.default, # type: KnownProfiles",
"description": "A profile definition, from KnownProfiles to dict.",
"docstring_type": "azure.profiles.KnownProfiles",
"required": false
}
},
"async": {
"api_version": {
"signature": "api_version: Optional[str] = None,",
"description": "API version to use if no profile is provided, or if missing in profile.",
"docstring_type": "str",
"required": false
},
{% if not code_model.custom_base_url %}
"base_url": {
"signature": {{ code_model.base_url_method_signature(True) | tojson }},
"description": "Service URL",
"docstring_type": "str",
"required": false
},
{% endif %}
"profile": {
"signature": "profile: KnownProfiles = KnownProfiles.default,",
"description": "A profile definition, from KnownProfiles to dict.",
"docstring_type": "azure.profiles.KnownProfiles",
"required": false
}
}
}
},
"config": {
"credential": {{ code_model.options['credential'] | tojson }},
Expand Down
4 changes: 2 additions & 2 deletions autorest/codegen/templates/service_client.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ def __init__(
{{ param_signature }},
{% endfor %}
{% if code_model.base_url %}
base_url: Optional[str] = None,
{{ code_model.base_url_method_signature(True) }}
{% endif %}
**kwargs: Any
{% else %}
{% for param_signature in code_model.global_parameters.sync_method_signature %}
{{ param_signature }}
{% endfor %}
{% if code_model.base_url %}
base_url=None, # type: Optional[str]
{{ code_model.base_url_method_signature(False) }}
{% endif %}
**kwargs # type: Any
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion autorest/multiapi/models/code_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(
self.config = Config(default_version_metadata)
self.operation_mixin_group = OperationMixinGroup(version_path_to_metadata, default_api_version)
self.global_parameters = GlobalParameters(
default_version_metadata["global_parameters"], self.service_client.base_url
default_version_metadata["global_parameters"]
)
self.user_specified_default_api = user_specified_default_api

Expand Down
86 changes: 26 additions & 60 deletions autorest/multiapi/models/global_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,85 +3,51 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List
from .global_parameter import GlobalParameter
from .constant_global_parameter import ConstantGlobalParameter

def _convert_global_parameters(sync_metadata, async_metadata):
global_parameters = [
GlobalParameter(
name=parameter_name,
global_parameter_metadata_sync=gp_sync,
global_parameter_metadata_async=async_metadata[parameter_name]
)
for parameter_name, gp_sync in sync_metadata.items()
]
return global_parameters

class GlobalParameters:
def __init__(self, global_parameters_metadata: Dict[str, Any], service_client_base_url: Optional[str]):
def __init__(
self,
global_parameters_metadata: Dict[str, Any],
):
self.call = global_parameters_metadata["call"]
self.global_parameters_metadata = global_parameters_metadata
self.service_client_base_url = service_client_base_url

@property
def service_client_specific_global_parameters(self) -> List[GlobalParameter]:
"""Return global params specific to multiapi service client + config
api_version, base_url (re-adding it in specific are), and profile
"""
api_version_param = GlobalParameter(
name="api_version",
global_parameter_metadata_sync={
"signature": "api_version=None, # type: Optional[str]",
"description": "API version to use if no profile is provided, or if missing in profile.",
"docstring_type": "str",
"required": False
},
global_parameter_metadata_async={
"signature": "api_version: Optional[str] = None,",
"description": "API version to use if no profile is provided, or if missing in profile.",
"docstring_type": "str",
"required": False
},
)
base_url_param = None
if self.service_client_base_url:
base_url_param = GlobalParameter(
name="base_url",
global_parameter_metadata_sync={
"signature": "base_url=None, # type: Optional[str]",
"description": "Service URL",
"docstring_type": "str",
"required": False
},
global_parameter_metadata_async={
"signature": "base_url: Optional[str] = None,",
"description": "Service URL",
"docstring_type": "str",
"required": False
},
)
profile_param = GlobalParameter(
name="profile",
global_parameter_metadata_sync={
"signature": "profile=KnownProfiles.default, # type: KnownProfiles",
"description": "A profile definition, from KnownProfiles to dict.",
"docstring_type": "azure.profiles.KnownProfiles",
"required": False
},
global_parameter_metadata_async={
"signature": "profile: KnownProfiles = KnownProfiles.default,",
"description": "A profile definition, from KnownProfiles to dict.",
"docstring_type": "azure.profiles.KnownProfiles",
"required": False
},
service_client_params_sync = self.global_parameters_metadata["service_client_specific"]["sync"]
service_client_params_async = self.global_parameters_metadata["service_client_specific"]["async"]

return _convert_global_parameters(
service_client_params_sync,
service_client_params_async
)
# return non-None members
return [p for p in [api_version_param, base_url_param, profile_param] if p]

@property
def parameters(self) -> List[GlobalParameter]:
global_parameters_metadata_sync = self.global_parameters_metadata["sync"]
global_parameters_metadata_async = self.global_parameters_metadata["async"]

global_parameters = [
GlobalParameter(
name=parameter_name,
global_parameter_metadata_sync=gp_sync,
global_parameter_metadata_async=global_parameters_metadata_async[parameter_name]
)
for parameter_name, gp_sync in global_parameters_metadata_sync.items()
]
return global_parameters
return _convert_global_parameters(
global_parameters_metadata_sync,
global_parameters_metadata_async
)

@property
def constant_parameters(self) -> List[ConstantGlobalParameter]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
from azure.core.pipeline import PipelineResponse
from azure.core.pipeline.transport import AsyncHttpResponse, HttpRequest
from azure.core.polling import AsyncNoPolling, AsyncPollingMethod
from azure.core.polling.async_base_polling import AsyncLROBasePolling
from my.library.aio import AsyncCustomPager, AsyncCustomPoller
from my.library.aio import AsyncCustomDefaultPollingMethod, AsyncCustomPager, AsyncCustomPoller

from ... import models as _models

Expand Down Expand Up @@ -83,8 +82,8 @@ async def begin_basic_polling(
:type product: ~azure.directives.sample.models.Product
:keyword callable cls: A custom type or function that will be passed the direct response
:keyword str continuation_token: A continuation token to restart a poller from a saved state.
:keyword polling: True for ARMPolling, False for no polling, or a
polling object for personal polling strategy
:keyword polling: Pass in True if you'd like the AsyncCustomDefaultPollingMethod polling method,
False for no polling, or your own initialized polling object for a personal polling strategy.
:paramtype polling: bool or ~azure.core.polling.AsyncPollingMethod
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
:return: An instance of AsyncCustomPoller that returns either Product or the result of cls(response)
Expand Down Expand Up @@ -115,7 +114,7 @@ def get_long_running_output(pipeline_response):
return cls(pipeline_response, deserialized, {})
return deserialized

if polling is True: polling_method = AsyncLROBasePolling(lro_delay, **kwargs)
if polling is True: polling_method = AsyncCustomDefaultPollingMethod(lro_delay, **kwargs)
elif polling is False: polling_method = AsyncNoPolling()
else: polling_method = polling
if cont_token:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
from azure.core.pipeline import PipelineResponse
from azure.core.pipeline.transport import HttpRequest, HttpResponse
from azure.core.polling import NoPolling, PollingMethod
from azure.core.polling.base_polling import LROBasePolling
from my.library import CustomPager, CustomPoller
from my.library import CustomDefaultPollingMethod, CustomPager, CustomPoller

from .. import models as _models

Expand Down Expand Up @@ -88,8 +87,8 @@ def begin_basic_polling(
:type product: ~azure.directives.sample.models.Product
:keyword callable cls: A custom type or function that will be passed the direct response
:keyword str continuation_token: A continuation token to restart a poller from a saved state.
:keyword polling: True for ARMPolling, False for no polling, or a
polling object for personal polling strategy
:keyword polling: Pass in True if you'd like the CustomDefaultPollingMethod polling method,
False for no polling, or your own initialized polling object for a personal polling strategy.
:paramtype polling: bool or ~azure.core.polling.PollingMethod
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
:return: An instance of CustomPoller that returns either Product or the result of cls(response)
Expand Down Expand Up @@ -120,7 +119,7 @@ def get_long_running_output(pipeline_response):
return cls(pipeline_response, deserialized, {})
return deserialized

if polling is True: polling_method = LROBasePolling(lro_delay, **kwargs)
if polling is True: polling_method = CustomDefaultPollingMethod(lro_delay, **kwargs)
elif polling is False: polling_method = NoPolling()
else: polling_method = polling
if cont_token:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(
self,
credential, # type: "TokenCredential"
api_version=None, # type: Optional[str]
base_url=None, # type: Optional[str]
base_url=None, # type: Optional[str]
profile=KnownProfiles.default, # type: KnownProfiles
**kwargs # type: Any
):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def begin_test_lro(
:type product: ~azure.multiapi.sample.models.Product
:keyword callable cls: A custom type or function that will be passed the direct response
:keyword str continuation_token: A continuation token to restart a poller from a saved state.
:keyword polling: True for ARMPolling, False for no polling, or a
polling object for personal polling strategy
:keyword polling: Pass in True if you'd like the ARMPolling polling method,
False for no polling, or your own initialized polling object for a personal polling strategy.
:paramtype polling: bool or ~azure.core.polling.PollingMethod
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
:return: An instance of LROPoller that returns either Product or the result of cls(response)
Expand Down Expand Up @@ -73,8 +73,8 @@ def begin_test_lro_and_paging(
:type test_lro_and_paging_options: ~azure.multiapi.sample.models.TestLroAndPagingOptions
:keyword callable cls: A custom type or function that will be passed the direct response
:keyword str continuation_token: A continuation token to restart a poller from a saved state.
:keyword polling: True for ARMPolling, False for no polling, or a
polling object for personal polling strategy
:keyword polling: Pass in True if you'd like the ARMPolling polling method,
False for no polling, or your own initialized polling object for a personal polling strategy.
:paramtype polling: bool or ~azure.core.polling.PollingMethod
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
:return: An instance of LROPoller that returns an iterator like instance of either PagingResult or the result of cls(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ async def begin_test_lro(
:type product: ~azure.multiapi.sample.models.Product
:keyword callable cls: A custom type or function that will be passed the direct response
:keyword str continuation_token: A continuation token to restart a poller from a saved state.
:keyword polling: True for ARMPolling, False for no polling, or a
polling object for personal polling strategy
:keyword polling: Pass in True if you'd like the AsyncARMPolling polling method,
False for no polling, or your own initialized polling object for a personal polling strategy.
:paramtype polling: bool or ~azure.core.polling.AsyncPollingMethod
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
:return: An instance of AsyncLROPoller that returns either Product or the result of cls(response)
Expand Down Expand Up @@ -69,8 +69,8 @@ def begin_test_lro_and_paging(
:type test_lro_and_paging_options: ~azure.multiapi.sample.models.TestLroAndPagingOptions
:keyword callable cls: A custom type or function that will be passed the direct response
:keyword str continuation_token: A continuation token to restart a poller from a saved state.
:keyword polling: True for ARMPolling, False for no polling, or a
polling object for personal polling strategy
:keyword polling: Pass in True if you'd like the AsyncARMPolling polling method,
False for no polling, or your own initialized polling object for a personal polling strategy.
:paramtype polling: bool or ~azure.core.polling.AsyncPollingMethod
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
:return: An instance of AsyncLROPoller that returns an iterator like instance of either PagingResult or the result of cls(response)
Expand Down
Loading