-
Notifications
You must be signed in to change notification settings - Fork 62
add credential schema policy classes to improve code flow #974
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
61320ee
add credential schema policy classes to improve code flow
iscai-msft 549dd62
challenge_authentication_policy -> default_authentication_policy
iscai-msft b6e3891
Merge branch 'autorestv3' of https://github.com/Azure/autorest.python…
iscai-msft 8990452
improve code
iscai-msft d17a0e1
Merge branch 'autorestv3' into fix_credential_ordering
iscai-msft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for | ||
| # license information. | ||
| # -------------------------------------------------------------------------- | ||
| from abc import abstractmethod | ||
| from typing import Optional | ||
| from .credential_schema import CredentialSchema | ||
|
|
||
| class CredentialSchemaPolicy: | ||
| name: Optional[str] = None | ||
|
lmazuel marked this conversation as resolved.
Outdated
|
||
|
|
||
| def __init__(self) -> None: | ||
| self._credential = None | ||
|
|
||
| @property | ||
| def credential(self) -> CredentialSchema: | ||
| if not self._credential: | ||
| raise ValueError( | ||
| "You have not initialized this policy with its credential yet" | ||
| ) | ||
| return self._credential | ||
|
|
||
| def initialize(self, credential, **kwargs): # pylint: disable=unused-argument | ||
| """Initialize your schema policy""" | ||
| self._credential = credential | ||
|
|
||
| @abstractmethod | ||
| def call(self, async_mode: bool) -> str: | ||
| ... | ||
|
|
||
|
|
||
| class BearerTokenCredentialPolicy(CredentialSchemaPolicy): | ||
| name = "BearerTokenCredentialPolicy" | ||
|
|
||
| def __init__(self) -> None: | ||
| super().__init__() | ||
| self.credential_scopes = None | ||
|
|
||
| def initialize(self, credential, **kwargs): | ||
| super().initialize(credential) | ||
| self.credential_scopes = kwargs.pop("credential_scopes") | ||
|
|
||
| def call(self, async_mode: bool) -> str: | ||
| policy_name = f"Async{self.name}" if async_mode else self.name | ||
| return f"policies.{policy_name}(self.credential, *self.credential_scopes, **kwargs)" | ||
|
|
||
|
|
||
| class AzureKeyCredentialPolicy(CredentialSchemaPolicy): | ||
| name = "AzureKeyCredentialPolicy" | ||
|
|
||
| def __init__(self) -> None: | ||
| super().__init__() | ||
| self.credential_key_header_name = None | ||
|
|
||
| def initialize(self, credential, **kwargs): | ||
|
lmazuel marked this conversation as resolved.
Outdated
|
||
| super().initialize(credential) | ||
| self.credential_key_header_name = kwargs.pop("credential_key_header_name") | ||
|
|
||
| def call(self, async_mode: bool) -> str: | ||
| return f'policies.AzureKeyCredentialPolicy(self.credential, "{self.credential_key_header_name}", **kwargs)' | ||
|
|
||
| def get_credential_schema_policy(name): | ||
| policies = [BearerTokenCredentialPolicy(), AzureKeyCredentialPolicy()] | ||
| try: | ||
| return next(p for p in policies if p.name.lower() == name.lower()) | ||
| except StopIteration: | ||
| raise ValueError( | ||
| "The credential policy you pass in with --credential-default-policy-type must be either " | ||
| "{}".format( | ||
| " or ".join([p.name for p in policies]) | ||
| ) | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.