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

Add headers and params in operation and request builder kwargs #1183

Merged
merged 26 commits into from
Mar 23, 2022

Conversation

changlong-liu
Copy link
Member

@changlong-liu changlong-liu commented Mar 11, 2022

for issue #1162

@changlong-liu changlong-liu force-pushed the 20220311-header-parameters branch 6 times, most recently from 7c7df7d to fa7303d Compare March 15, 2022 23:38
Copy link
Contributor

@iscai-msft iscai-msft left a comment

Choose a reason for hiding this comment

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

looks good!

@@ -860,6 +878,8 @@ def _call_request_builder_helper(
if not self.code_model.options["version_tolerant"]:
template_url = template_url or f"self.{builder.name}.metadata['url']"
retval.append(f" template_url={template_url},")
retval.append(' headers=_headers,')
retval.append(' params=_params,')
Copy link
Contributor

Choose a reason for hiding this comment

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

can you also update line 1015 to
retval.append("error_map.update(kwargs.pop('error_map', {})) or {}")

Copy link
Member Author

Choose a reason for hiding this comment

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

updated as this:

retval.append("error_map.update(kwargs.pop('error_map', {}) or {})")

since we want to avoid kwargs['error_map'] is None?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, if the user passes in operation(error_map=None), we would have error_map = None, then map_error gets mad

@@ -151,10 +164,15 @@ async def get_invalid(self, **kwargs: Any) -> "_models.Basic":
error_map = {401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError}
error_map.update(kwargs.pop("error_map", {}))

_headers = case_insensitive_dict(kwargs.pop("headers", {}) or {})
Copy link
Contributor

Choose a reason for hiding this comment

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

I know yesterday you didn't love that we had to convert to case_insensitive_dict in operations and in request builders. I think a good idea could be to only convert to case_insensitive_dict in the main operation if we need to, for example on line 121, we need case insensitive dict bc we're checking api-version. However, here we don't need a case insensitive dict bc we pass it directly to the request builder, which will convert it for us

If it makes autorest code too gross feel free to disregard, but I think it can be done neatly

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes @iscai-msft , this make lots sense on the generated code. I modified two parts of logic to achieve this:

  1. model.Operation._imports_base (for checking whether need to import case_insensitive_dict)
  2. serializer._OperationBaseSerializer.pop_kwargs_from_signature (for checking whether need to convert pop outcomes to ``case_insensitive`)

@changlong-liu changlong-liu force-pushed the 20220311-header-parameters branch from 42ed37c to 9003dc5 Compare March 18, 2022 02:45
@changlong-liu changlong-liu marked this pull request as ready for review March 18, 2022 05:15
@changlong-liu changlong-liu requested a review from msyyc as a code owner March 18, 2022 05:15
@changlong-liu changlong-liu changed the title WIP: headers and params Add headers and params in operation and request builder kwargs Mar 18, 2022
Copy link
Contributor

@iscai-msft iscai-msft left a comment

Choose a reason for hiding this comment

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

so close, it looks like we're not inspecting query / maybe header dicts for some of the params though

@@ -860,6 +878,8 @@ def _call_request_builder_helper(
if not self.code_model.options["version_tolerant"]:
template_url = template_url or f"self.{builder.name}.metadata['url']"
retval.append(f" template_url={template_url},")
retval.append(' headers=_headers,')
retval.append(' params=_params,')
Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, if the user passes in operation(error_map=None), we would have error_map = None, then map_error gets mad

@@ -102,13 +103,36 @@ def method_signature_and_response_type_annotation_template(
return f"{method_signature} -> {response_type_annotation}:"
return f"{method_signature}:\n # type: (...) -> {response_type_annotation}"

def pop_kwargs_from_signature(kwargs_to_pop: List[Parameter]) -> List[str]:
class PopKwargType(Enum):
NO = auto()
Copy link
Contributor

Choose a reason for hiding this comment

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

like the enum for the three kinds of popping!

_query_parameters = kwargs.pop("params", {}) # type: Dict[str, Any]
_query_parameters['parameterOne'] = _SERIALIZER.query("parameter_one", parameter_one, 'bool')
_query_parameters['api-version'] = _SERIALIZER.query("api_version", api_version, 'str')
_params['parameterOne'] = _SERIALIZER.query("parameter_one", parameter_one, 'bool')
Copy link
Contributor

Choose a reason for hiding this comment

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

it seems like we're missing a case here at least for query params: parameterOne is a query param but we're not checking the param dict for it here

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @iscai-msft , I think this can be a problem for some build_xx_request functions, so I added changes at line 42 of this file (as generated result) in commit 8db1a08

Add I added some test case for this scenario: 20bfab2

Copy link
Contributor

Choose a reason for hiding this comment

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

hey @changlong-liu sorry I should've thought more before I added this comment. Even though these required parameters are kwargs in the builders (and in some version tolerant operations), they are still required and there will be an error if users don't input them, so I think the reversal I did still stands: we don't need to check headers or params because users will be required to input and we want to keep raising the error if users don't put them in. What do you think?
Screen Shot 2022-03-22 at 11 13 49 AM

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the talk in meeting about this. Keeping current logic is the best way to ensure same behavior across different style of build_xx_request signatures.
So I reverted changes in 8db1a08 and 20bfab2

Copy link
Contributor

@iscai-msft iscai-msft left a comment

Choose a reason for hiding this comment

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

yay this looks great!

@iscai-msft iscai-msft merged commit d7291e1 into Azure:autorestv3 Mar 23, 2022
iscai-msft added a commit that referenced this pull request Mar 23, 2022
…into fix_dpg_customize_tests

* 'autorestv3' of https://github.com/Azure/autorest.python:
  Add headers and params in operation and request builder kwargs (#1183)
  use dict to handle generations (#1200)
iscai-msft added a commit that referenced this pull request Mar 23, 2022
…into update_models_filename

* 'autorestv3' of https://github.com/Azure/autorest.python:
  Add headers and params in operation and request builder kwargs (#1183)
  use dict to handle generations (#1200)
  pylint and mypy support for version tolerant with msrest models (#1202)
  rename-dpg-swagger (#1201)
iscai-msft added a commit that referenced this pull request May 17, 2022
…into post_process_mypy

* 'autorestv3' of https://github.com/Azure/autorest.python:
  switch to dict models (#1203)
  change models filename for version tolerant (#1204)
  get tests green (#1205)
  Add headers and params in operation and request builder kwargs (#1183)
  use dict to handle generations (#1200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants