Skip to content

Commit f4040ff

Browse files
author
SDKAuto
committed
CodeGen from PR 26911 in Azure/azure-rest-api-specs
Merge f007d69e1875b03ebbf6aed4d71b3a67f4caba1a into b8c74fd80b415fa1ebb6fa787d454694c39e0fd5
1 parent b6649a0 commit f4040ff

27 files changed

Lines changed: 1673 additions & 1289 deletions
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"commit": "96809b2ae082df24ae0834f7bb08d9b030c4193b",
3+
"repository_url": "https://github.com/Azure/azure-rest-api-specs",
4+
"typespec_src": "specification/cognitiveservices/ContentSafety",
5+
"@azure-tools/typespec-python": "0.16.1"
6+
}

sdk/contentsafety/azure-ai-contentsafety/azure/ai/contentsafety/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# --------------------------------------------------------------------------
88

99
from ._client import ContentSafetyClient
10+
from ._client import BlocklistClient
1011
from ._version import VERSION
1112

1213
__version__ = VERSION
@@ -20,6 +21,7 @@
2021

2122
__all__ = [
2223
"ContentSafetyClient",
24+
"BlocklistClient",
2325
]
2426
__all__.extend([p for p in _patch_all if p not in __all__])
2527

sdk/contentsafety/azure-ai-contentsafety/azure/ai/contentsafety/_client.py

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,38 @@
77
# --------------------------------------------------------------------------
88

99
from copy import deepcopy
10-
from typing import Any
10+
from typing import Any, TYPE_CHECKING, Union
1111

1212
from azure.core import PipelineClient
1313
from azure.core.credentials import AzureKeyCredential
1414
from azure.core.pipeline import policies
1515
from azure.core.rest import HttpRequest, HttpResponse
1616

17-
from ._configuration import ContentSafetyClientConfiguration
18-
from ._operations import ContentSafetyClientOperationsMixin
17+
from ._configuration import BlocklistClientConfiguration, ContentSafetyClientConfiguration
18+
from ._operations import BlocklistClientOperationsMixin, ContentSafetyClientOperationsMixin
1919
from ._serialization import Deserializer, Serializer
2020

21+
if TYPE_CHECKING:
22+
# pylint: disable=unused-import,ungrouped-imports
23+
from azure.core.credentials import TokenCredential
24+
2125

2226
class ContentSafetyClient(ContentSafetyClientOperationsMixin): # pylint: disable=client-accepts-api-version-keyword
23-
"""Analyze harmful content.
27+
"""ContentSafetyClient.
2428
2529
:param endpoint: Supported Cognitive Services endpoints (protocol and hostname, for example:
2630
https://:code:`<resource-name>`.cognitiveservices.azure.com). Required.
2731
:type endpoint: str
28-
:param credential: Credential needed for the client to connect to Azure. Required.
29-
:type credential: ~azure.core.credentials.AzureKeyCredential
30-
:keyword api_version: The API version to use for this operation. Default value is
31-
"2023-04-30-preview". Note that overriding this default value may result in unsupported
32-
behavior.
32+
:param credential: Credential needed for the client to connect to Azure. Is either a
33+
AzureKeyCredential type or a TokenCredential type. Required.
34+
:type credential: ~azure.core.credentials.AzureKeyCredential or
35+
~azure.core.credentials.TokenCredential
36+
:keyword api_version: The API version to use for this operation. Default value is "2023-10-01".
37+
Note that overriding this default value may result in unsupported behavior.
3338
:paramtype api_version: str
3439
"""
3540

36-
def __init__(self, endpoint: str, credential: AzureKeyCredential, **kwargs: Any) -> None:
41+
def __init__(self, endpoint: str, credential: Union[AzureKeyCredential, "TokenCredential"], **kwargs: Any) -> None:
3742
_endpoint = "{endpoint}/contentsafety"
3843
self._config = ContentSafetyClientConfiguration(endpoint=endpoint, credential=credential, **kwargs)
3944
_policies = kwargs.pop("policies", None)
@@ -59,7 +64,7 @@ def __init__(self, endpoint: str, credential: AzureKeyCredential, **kwargs: Any)
5964
self._deserialize = Deserializer()
6065
self._serialize.client_side_validation = False
6166

62-
def send_request(self, request: HttpRequest, **kwargs: Any) -> HttpResponse:
67+
def send_request(self, request: HttpRequest, *, stream: bool = False, **kwargs: Any) -> HttpResponse:
6368
"""Runs the network request through the client's chained policies.
6469
6570
>>> from azure.core.rest import HttpRequest
@@ -83,7 +88,7 @@ def send_request(self, request: HttpRequest, **kwargs: Any) -> HttpResponse:
8388
}
8489

8590
request_copy.url = self._client.format_url(request_copy.url, **path_format_arguments)
86-
return self._client.send_request(request_copy, **kwargs) # type: ignore
91+
return self._client.send_request(request_copy, stream=stream, **kwargs) # type: ignore
8792

8893
def close(self) -> None:
8994
self._client.close()
@@ -94,3 +99,81 @@ def __enter__(self) -> "ContentSafetyClient":
9499

95100
def __exit__(self, *exc_details: Any) -> None:
96101
self._client.__exit__(*exc_details)
102+
103+
104+
class BlocklistClient(BlocklistClientOperationsMixin): # pylint: disable=client-accepts-api-version-keyword
105+
"""BlocklistClient.
106+
107+
:param endpoint: Supported Cognitive Services endpoints (protocol and hostname, for example:
108+
https://:code:`<resource-name>`.cognitiveservices.azure.com). Required.
109+
:type endpoint: str
110+
:param credential: Credential needed for the client to connect to Azure. Is either a
111+
AzureKeyCredential type or a TokenCredential type. Required.
112+
:type credential: ~azure.core.credentials.AzureKeyCredential or
113+
~azure.core.credentials.TokenCredential
114+
:keyword api_version: The API version to use for this operation. Default value is "2023-10-01".
115+
Note that overriding this default value may result in unsupported behavior.
116+
:paramtype api_version: str
117+
"""
118+
119+
def __init__(self, endpoint: str, credential: Union[AzureKeyCredential, "TokenCredential"], **kwargs: Any) -> None:
120+
_endpoint = "{endpoint}/contentsafety"
121+
self._config = BlocklistClientConfiguration(endpoint=endpoint, credential=credential, **kwargs)
122+
_policies = kwargs.pop("policies", None)
123+
if _policies is None:
124+
_policies = [
125+
policies.RequestIdPolicy(**kwargs),
126+
self._config.headers_policy,
127+
self._config.user_agent_policy,
128+
self._config.proxy_policy,
129+
policies.ContentDecodePolicy(**kwargs),
130+
self._config.redirect_policy,
131+
self._config.retry_policy,
132+
self._config.authentication_policy,
133+
self._config.custom_hook_policy,
134+
self._config.logging_policy,
135+
policies.DistributedTracingPolicy(**kwargs),
136+
policies.SensitiveHeaderCleanupPolicy(**kwargs) if self._config.redirect_policy else None,
137+
self._config.http_logging_policy,
138+
]
139+
self._client: PipelineClient = PipelineClient(base_url=_endpoint, policies=_policies, **kwargs)
140+
141+
self._serialize = Serializer()
142+
self._deserialize = Deserializer()
143+
self._serialize.client_side_validation = False
144+
145+
def send_request(self, request: HttpRequest, *, stream: bool = False, **kwargs: Any) -> HttpResponse:
146+
"""Runs the network request through the client's chained policies.
147+
148+
>>> from azure.core.rest import HttpRequest
149+
>>> request = HttpRequest("GET", "https://www.example.org/")
150+
<HttpRequest [GET], url: 'https://www.example.org/'>
151+
>>> response = client.send_request(request)
152+
<HttpResponse: 200 OK>
153+
154+
For more information on this code flow, see https://aka.ms/azsdk/dpcodegen/python/send_request
155+
156+
:param request: The network request you want to make. Required.
157+
:type request: ~azure.core.rest.HttpRequest
158+
:keyword bool stream: Whether the response payload will be streamed. Defaults to False.
159+
:return: The response of your network call. Does not do error handling on your response.
160+
:rtype: ~azure.core.rest.HttpResponse
161+
"""
162+
163+
request_copy = deepcopy(request)
164+
path_format_arguments = {
165+
"endpoint": self._serialize.url("self._config.endpoint", self._config.endpoint, "str", skip_quote=True),
166+
}
167+
168+
request_copy.url = self._client.format_url(request_copy.url, **path_format_arguments)
169+
return self._client.send_request(request_copy, stream=stream, **kwargs) # type: ignore
170+
171+
def close(self) -> None:
172+
self._client.close()
173+
174+
def __enter__(self) -> "BlocklistClient":
175+
self._client.__enter__()
176+
return self
177+
178+
def __exit__(self, *exc_details: Any) -> None:
179+
self._client.__exit__(*exc_details)

sdk/contentsafety/azure-ai-contentsafety/azure/ai/contentsafety/_configuration.py

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
77
# --------------------------------------------------------------------------
88

9-
from typing import Any
9+
from typing import Any, TYPE_CHECKING, Union
1010

1111
from azure.core.credentials import AzureKeyCredential
1212
from azure.core.pipeline import policies
1313

1414
from ._version import VERSION
1515

16+
if TYPE_CHECKING:
17+
# pylint: disable=unused-import,ungrouped-imports
18+
from azure.core.credentials import TokenCredential
19+
1620

1721
class ContentSafetyClientConfiguration: # pylint: disable=too-many-instance-attributes,name-too-long
1822
"""Configuration for ContentSafetyClient.
@@ -23,16 +27,17 @@ class ContentSafetyClientConfiguration: # pylint: disable=too-many-instance-att
2327
:param endpoint: Supported Cognitive Services endpoints (protocol and hostname, for example:
2428
https://:code:`<resource-name>`.cognitiveservices.azure.com). Required.
2529
:type endpoint: str
26-
:param credential: Credential needed for the client to connect to Azure. Required.
27-
:type credential: ~azure.core.credentials.AzureKeyCredential
28-
:keyword api_version: The API version to use for this operation. Default value is
29-
"2023-04-30-preview". Note that overriding this default value may result in unsupported
30-
behavior.
30+
:param credential: Credential needed for the client to connect to Azure. Is either a
31+
AzureKeyCredential type or a TokenCredential type. Required.
32+
:type credential: ~azure.core.credentials.AzureKeyCredential or
33+
~azure.core.credentials.TokenCredential
34+
:keyword api_version: The API version to use for this operation. Default value is "2023-10-01".
35+
Note that overriding this default value may result in unsupported behavior.
3136
:paramtype api_version: str
3237
"""
3338

34-
def __init__(self, endpoint: str, credential: AzureKeyCredential, **kwargs: Any) -> None:
35-
api_version: str = kwargs.pop("api_version", "2023-04-30-preview")
39+
def __init__(self, endpoint: str, credential: Union[AzureKeyCredential, "TokenCredential"], **kwargs: Any) -> None:
40+
api_version: str = kwargs.pop("api_version", "2023-10-01")
3641

3742
if endpoint is None:
3843
raise ValueError("Parameter 'endpoint' must not be None.")
@@ -42,21 +47,82 @@ def __init__(self, endpoint: str, credential: AzureKeyCredential, **kwargs: Any)
4247
self.endpoint = endpoint
4348
self.credential = credential
4449
self.api_version = api_version
50+
self.credential_scopes = kwargs.pop("credential_scopes", ["https://cognitiveservices.azure.com/.default"])
4551
kwargs.setdefault("sdk_moniker", "ai-contentsafety/{}".format(VERSION))
4652
self.polling_interval = kwargs.get("polling_interval", 30)
4753
self._configure(**kwargs)
4854

55+
def _infer_policy(self, **kwargs):
56+
if isinstance(self.credential, AzureKeyCredential):
57+
return policies.AzureKeyCredentialPolicy(self.credential, "Ocp-Apim-Subscription-Key", **kwargs)
58+
if hasattr(self.credential, "get_token"):
59+
return policies.BearerTokenCredentialPolicy(self.credential, *self.credential_scopes, **kwargs)
60+
raise TypeError(f"Unsupported credential: {self.credential}")
61+
4962
def _configure(self, **kwargs: Any) -> None:
5063
self.user_agent_policy = kwargs.get("user_agent_policy") or policies.UserAgentPolicy(**kwargs)
5164
self.headers_policy = kwargs.get("headers_policy") or policies.HeadersPolicy(**kwargs)
5265
self.proxy_policy = kwargs.get("proxy_policy") or policies.ProxyPolicy(**kwargs)
5366
self.logging_policy = kwargs.get("logging_policy") or policies.NetworkTraceLoggingPolicy(**kwargs)
5467
self.http_logging_policy = kwargs.get("http_logging_policy") or policies.HttpLoggingPolicy(**kwargs)
68+
self.custom_hook_policy = kwargs.get("custom_hook_policy") or policies.CustomHookPolicy(**kwargs)
69+
self.redirect_policy = kwargs.get("redirect_policy") or policies.RedirectPolicy(**kwargs)
5570
self.retry_policy = kwargs.get("retry_policy") or policies.RetryPolicy(**kwargs)
71+
self.authentication_policy = kwargs.get("authentication_policy")
72+
if self.credential and not self.authentication_policy:
73+
self.authentication_policy = self._infer_policy(**kwargs)
74+
75+
76+
class BlocklistClientConfiguration: # pylint: disable=too-many-instance-attributes,name-too-long
77+
"""Configuration for BlocklistClient.
78+
79+
Note that all parameters used to create this instance are saved as instance
80+
attributes.
81+
82+
:param endpoint: Supported Cognitive Services endpoints (protocol and hostname, for example:
83+
https://:code:`<resource-name>`.cognitiveservices.azure.com). Required.
84+
:type endpoint: str
85+
:param credential: Credential needed for the client to connect to Azure. Is either a
86+
AzureKeyCredential type or a TokenCredential type. Required.
87+
:type credential: ~azure.core.credentials.AzureKeyCredential or
88+
~azure.core.credentials.TokenCredential
89+
:keyword api_version: The API version to use for this operation. Default value is "2023-10-01".
90+
Note that overriding this default value may result in unsupported behavior.
91+
:paramtype api_version: str
92+
"""
93+
94+
def __init__(self, endpoint: str, credential: Union[AzureKeyCredential, "TokenCredential"], **kwargs: Any) -> None:
95+
api_version: str = kwargs.pop("api_version", "2023-10-01")
96+
97+
if endpoint is None:
98+
raise ValueError("Parameter 'endpoint' must not be None.")
99+
if credential is None:
100+
raise ValueError("Parameter 'credential' must not be None.")
101+
102+
self.endpoint = endpoint
103+
self.credential = credential
104+
self.api_version = api_version
105+
self.credential_scopes = kwargs.pop("credential_scopes", ["https://cognitiveservices.azure.com/.default"])
106+
kwargs.setdefault("sdk_moniker", "ai-contentsafety/{}".format(VERSION))
107+
self.polling_interval = kwargs.get("polling_interval", 30)
108+
self._configure(**kwargs)
109+
110+
def _infer_policy(self, **kwargs):
111+
if isinstance(self.credential, AzureKeyCredential):
112+
return policies.AzureKeyCredentialPolicy(self.credential, "Ocp-Apim-Subscription-Key", **kwargs)
113+
if hasattr(self.credential, "get_token"):
114+
return policies.BearerTokenCredentialPolicy(self.credential, *self.credential_scopes, **kwargs)
115+
raise TypeError(f"Unsupported credential: {self.credential}")
116+
117+
def _configure(self, **kwargs: Any) -> None:
118+
self.user_agent_policy = kwargs.get("user_agent_policy") or policies.UserAgentPolicy(**kwargs)
119+
self.headers_policy = kwargs.get("headers_policy") or policies.HeadersPolicy(**kwargs)
120+
self.proxy_policy = kwargs.get("proxy_policy") or policies.ProxyPolicy(**kwargs)
121+
self.logging_policy = kwargs.get("logging_policy") or policies.NetworkTraceLoggingPolicy(**kwargs)
122+
self.http_logging_policy = kwargs.get("http_logging_policy") or policies.HttpLoggingPolicy(**kwargs)
56123
self.custom_hook_policy = kwargs.get("custom_hook_policy") or policies.CustomHookPolicy(**kwargs)
57124
self.redirect_policy = kwargs.get("redirect_policy") or policies.RedirectPolicy(**kwargs)
125+
self.retry_policy = kwargs.get("retry_policy") or policies.RetryPolicy(**kwargs)
58126
self.authentication_policy = kwargs.get("authentication_policy")
59127
if self.credential and not self.authentication_policy:
60-
self.authentication_policy = policies.AzureKeyCredentialPolicy(
61-
self.credential, "Ocp-Apim-Subscription-Key", **kwargs
62-
)
128+
self.authentication_policy = self._infer_policy(**kwargs)

0 commit comments

Comments
 (0)