-
Notifications
You must be signed in to change notification settings - Fork 58
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
Add headers and params in operation and request builder kwargs #1183
Conversation
7c7df7d
to
fa7303d
Compare
There was a problem hiding this 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,') |
There was a problem hiding this comment.
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 {}")
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 {}) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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:
model.Operation._imports_base
(for checking whether need to importcase_insensitive_dict
)serializer._OperationBaseSerializer.pop_kwargs_from_signature
(for checking whether need to convert pop outcomes to ``case_insensitive`)
import for headers and queries
lint
42ed37c
to
9003dc5
Compare
There was a problem hiding this 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,') |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this 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!
…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)
…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)
…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)
for issue #1162