Skip to content

Commit 92593dd

Browse files
authored
add send request method (#34038)
* add send request method * update * update * update * update
1 parent 629a2ab commit 92593dd

11 files changed

+256
-10
lines changed

sdk/search/azure-search-documents/CHANGELOG.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Release History
22

3-
## 11.6.0b1 (Unreleased)
3+
## 11.6.0b1 (2024-01-31)
44

55
### Features Added
66

7-
- Added `semantic_query` for `Search` method.
8-
- Added back alias operations to `SearchIndexClient`
7+
- Added back `semantic_query` for `Search` method.
8+
- Added back alias operations to `SearchIndexClient`.
9+
- Added back `AzureOpenAIEmbeddingSkill`, `AzureOpenAIParameters` and `AzureOpenAIVectorizer`.
910
- Added back `query_language`, `query_speller`, `semantic_fields` and `debug` for `Search` method.
10-
11-
### Breaking Changes
11+
- Added `send_request` method for `SearchClient` & `SearchIndexClient` to run a network request using the client's existing pipeline.
1212

1313
### Bugs Fixed
1414

sdk/search/azure-search-documents/azure/search/documents/_generated/models/_search_index_client_enums.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,6 @@ class ScoringStatistics(str, Enum, metaclass=CaseInsensitiveEnumMeta):
276276
"""The scoring statistics will be calculated locally for lower latency."""
277277
GLOBAL = "global"
278278
"""The scoring statistics will be calculated globally for more consistent scoring."""
279-
GLOBAL_ENUM = "global"
280-
"""The scoring statistics will be calculated globally for more consistent scoring."""
281279

282280

283281
class SearchMode(str, Enum, metaclass=CaseInsensitiveEnumMeta):

sdk/search/azure-search-documents/azure/search/documents/_headers_mixin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
# license information.
55
# --------------------------------------------------------------------------
66

7-
from typing import Optional, Dict, Any
7+
from typing import Optional, Dict, Any, MutableMapping
88

99

1010
class HeadersMixin:
1111
@property
1212
def _headers(self) -> Dict[str, Any]:
1313
return {"api-key": self._credential.key, "Accept": self._ODATA_ACCEPT} # type: ignore
1414

15-
def _merge_client_headers(self, headers: Optional[Dict[str, Any]]) -> Optional[Dict[str, Any]]:
15+
def _merge_client_headers(self, headers: Optional[MutableMapping[str, str]]) -> MutableMapping[str, str]:
1616
if self._aad: # type: ignore
17-
return headers
17+
return headers or {}
1818
headers = headers or {}
1919
combined = self._headers
2020
combined.update(headers)

sdk/search/azure-search-documents/azure/search/documents/_search_client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# --------------------------------------------------------------------------
66
from typing import cast, List, Any, Union, Dict, Optional
77

8+
from azure.core.rest import HttpRequest, HttpResponse
89
from azure.core.credentials import AzureKeyCredential, TokenCredential
910
from azure.core.tracing.decorator import distributed_trace
1011
from ._api_versions import DEFAULT_VERSION
@@ -705,3 +706,16 @@ def __enter__(self) -> "SearchClient":
705706

706707
def __exit__(self, *args) -> None:
707708
self._client.__exit__(*args)
709+
710+
@distributed_trace
711+
def send_request(self, request: HttpRequest, *, stream: bool = False, **kwargs) -> HttpResponse:
712+
"""Runs a network request using the client's existing pipeline.
713+
714+
:param request: The network request you want to make.
715+
:type request: ~azure.core.rest.HttpRequest
716+
:keyword bool stream: Whether the response payload will be streamed. Defaults to False.
717+
:return: The response of your network call. Does not do error handling on your response.
718+
:rtype: ~azure.core.rest.HttpResponse
719+
"""
720+
request.headers = self._merge_client_headers(request.headers)
721+
return self._client._send_request(request, stream=stream, **kwargs) # pylint:disable=protected-access

sdk/search/azure-search-documents/azure/search/documents/aio/_search_client_async.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# --------------------------------------------------------------------------
66
from typing import cast, List, Union, Any, Optional, Dict
77

8+
from azure.core.rest import HttpRequest, AsyncHttpResponse
89
from azure.core.credentials import AzureKeyCredential
910
from azure.core.credentials_async import AsyncTokenCredential
1011
from azure.core.tracing.decorator_async import distributed_trace_async
@@ -703,3 +704,16 @@ async def __aenter__(self) -> "SearchClient":
703704

704705
async def __aexit__(self, *args) -> None:
705706
await self._client.__aexit__(*args)
707+
708+
@distributed_trace_async
709+
async def send_request(self, request: HttpRequest, *, stream: bool = False, **kwargs) -> AsyncHttpResponse:
710+
"""Runs a network request using the client's existing pipeline.
711+
712+
:param request: The network request you want to make.
713+
:type request: ~azure.core.rest.HttpRequest
714+
:keyword bool stream: Whether the response payload will be streamed. Defaults to False.
715+
:return: The response of your network call. Does not do error handling on your response.
716+
:rtype: ~azure.core.rest.AsyncHttpResponse
717+
"""
718+
request.headers = self._merge_client_headers(request.headers)
719+
return await self._client._send_request(request, stream=stream, **kwargs) # pylint:disable=protected-access

sdk/search/azure-search-documents/azure/search/documents/indexes/_search_index_client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# --------------------------------------------------------------------------
66
from typing import Any, List, Union, Optional, MutableMapping, cast
77

8+
from azure.core.rest import HttpRequest, HttpResponse
89
from azure.core import MatchConditions
910
from azure.core.credentials import AzureKeyCredential, TokenCredential
1011
from azure.core.tracing.decorator import distributed_trace
@@ -614,3 +615,16 @@ def create_or_update_alias(
614615
alias_name=alias.name, alias=alias, prefer="return=representation", error_map=error_map, **kwargs
615616
)
616617
return result # pylint:disable=protected-access
618+
619+
@distributed_trace
620+
def send_request(self, request: HttpRequest, *, stream: bool = False, **kwargs) -> HttpResponse:
621+
"""Runs a network request using the client's existing pipeline.
622+
623+
:param request: The network request you want to make.
624+
:type request: ~azure.core.rest.HttpRequest
625+
:keyword bool stream: Whether the response payload will be streamed. Defaults to False.
626+
:return: The response of your network call. Does not do error handling on your response.
627+
:rtype: ~azure.core.rest.HttpResponse
628+
"""
629+
request.headers = self._merge_client_headers(request.headers)
630+
return self._client._send_request(request, stream=stream, **kwargs) # pylint:disable=protected-access

sdk/search/azure-search-documents/azure/search/documents/indexes/aio/_search_index_client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# --------------------------------------------------------------------------
66
from typing import Union, Any, List, Optional, MutableMapping, cast
77

8+
from azure.core.rest import HttpRequest, AsyncHttpResponse
89
from azure.core import MatchConditions
910
from azure.core.credentials import AzureKeyCredential
1011
from azure.core.credentials_async import AsyncTokenCredential
@@ -603,3 +604,16 @@ async def create_or_update_alias(
603604
alias_name=alias.name, alias=alias, prefer="return=representation", error_map=error_map, **kwargs
604605
)
605606
return result # pylint:disable=protected-access
607+
608+
@distributed_trace_async
609+
async def send_request(self, request: HttpRequest, *, stream: bool = False, **kwargs) -> AsyncHttpResponse:
610+
"""Runs a network request using the client's existing pipeline.
611+
612+
:param request: The network request you want to make.
613+
:type request: ~azure.core.rest.HttpRequest
614+
:keyword bool stream: Whether the response payload will be streamed. Defaults to False.
615+
:return: The response of your network call. Does not do error handling on your response.
616+
:rtype: ~azure.core.rest.AsyncHttpResponse
617+
"""
618+
request.headers = self._merge_client_headers(request.headers)
619+
return await self._client._send_request(request, stream=stream, **kwargs) # pylint:disable=protected-access
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# coding: utf-8
2+
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
9+
"""
10+
FILE: sample_index_client_send_request_async.py
11+
12+
DESCRIPTION:
13+
This sample demonstrates how to make custom HTTP requests through a client pipeline.
14+
15+
USAGE:
16+
python sample_index_client_send_request_async.py
17+
18+
Set the environment variables with your own values before running the sample:
19+
1) AZURE_SEARCH_SERVICE_ENDPOINT - the endpoint of your Azure Cognitive Search service
20+
2) AZURE_SEARCH_INDEX_NAME - the name of your search index (e.g. "hotels-sample-index")
21+
3) AZURE_SEARCH_API_KEY - your search API key
22+
"""
23+
24+
import asyncio
25+
import os
26+
from azure.core.credentials import AzureKeyCredential
27+
from azure.core.rest import HttpRequest
28+
from azure.search.documents.indexes.aio import SearchIndexClient
29+
30+
31+
async def sample_send_request():
32+
endpoint = os.environ["AZURE_SEARCH_SERVICE_ENDPOINT"]
33+
index_name = os.environ["AZURE_SEARCH_INDEX_NAME"]
34+
key = os.environ["AZURE_SEARCH_API_KEY"]
35+
36+
client = SearchIndexClient(endpoint, AzureKeyCredential(key))
37+
38+
# The `send_request` method can send custom HTTP requests that share the client's existing pipeline,
39+
# while adding convenience for endpoint construction.
40+
request = HttpRequest(method="GET", url=f"/indexes('{index_name}')?api-version=2023-10-01-Preview")
41+
async with client:
42+
response = await client.send_request(request)
43+
response.raise_for_status()
44+
response_body = response.json()
45+
print(response_body)
46+
47+
48+
if __name__ == "__main__":
49+
asyncio.run(sample_send_request())
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# coding: utf-8
2+
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
9+
"""
10+
FILE: sample_search_client_send_request_async.py
11+
12+
DESCRIPTION:
13+
This sample demonstrates how to make custom HTTP requests through a client pipeline.
14+
15+
USAGE:
16+
python sample_search_client_send_request_async.py
17+
18+
Set the environment variables with your own values before running the sample:
19+
1) AZURE_SEARCH_SERVICE_ENDPOINT - the endpoint of your Azure Cognitive Search service
20+
2) AZURE_SEARCH_INDEX_NAME - the name of your search index (e.g. "hotels-sample-index")
21+
3) AZURE_SEARCH_API_KEY - your search API key
22+
"""
23+
24+
import asyncio
25+
import os
26+
from azure.core.credentials import AzureKeyCredential
27+
from azure.core.rest import HttpRequest
28+
from azure.search.documents.aio import SearchClient
29+
30+
31+
async def sample_send_request():
32+
endpoint = os.environ["AZURE_SEARCH_SERVICE_ENDPOINT"]
33+
index_name = os.environ["AZURE_SEARCH_INDEX_NAME"]
34+
key = os.environ["AZURE_SEARCH_API_KEY"]
35+
36+
client = SearchClient(endpoint, index_name, AzureKeyCredential(key))
37+
38+
# The `send_request` method can send custom HTTP requests that share the client's existing pipeline,
39+
# while adding convenience for endpoint construction.
40+
request = HttpRequest(method="GET", url=f"/docs/$count?api-version=2023-10-01-Preview")
41+
async with client:
42+
response = await client.send_request(request)
43+
response.raise_for_status()
44+
response_body = response.json()
45+
print(response_body)
46+
47+
48+
if __name__ == "__main__":
49+
asyncio.run(sample_send_request())
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# coding: utf-8
2+
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
9+
"""
10+
FILE: sample_index_client_send_request.py
11+
12+
DESCRIPTION:
13+
This sample demonstrates how to make custom HTTP requests through a client pipeline.
14+
15+
USAGE:
16+
python sample_index_client_send_request.py
17+
18+
Set the environment variables with your own values before running the sample:
19+
1) AZURE_SEARCH_SERVICE_ENDPOINT - the endpoint of your Azure Cognitive Search service
20+
2) AZURE_SEARCH_INDEX_NAME - the name of your search index (e.g. "hotels-sample-index")
21+
3) AZURE_SEARCH_API_KEY - your search API key
22+
"""
23+
24+
import os
25+
from azure.core.credentials import AzureKeyCredential
26+
from azure.core.rest import HttpRequest
27+
from azure.search.documents.indexes import SearchIndexClient
28+
29+
30+
def sample_send_request():
31+
endpoint = os.environ["AZURE_SEARCH_SERVICE_ENDPOINT"]
32+
index_name = os.environ["AZURE_SEARCH_INDEX_NAME"]
33+
key = os.environ["AZURE_SEARCH_API_KEY"]
34+
35+
client = SearchIndexClient(endpoint, AzureKeyCredential(key))
36+
37+
# The `send_request` method can send custom HTTP requests that share the client's existing pipeline,
38+
# while adding convenience for endpoint construction.
39+
request = HttpRequest(method="GET", url=f"/indexes('{index_name}')?api-version=2023-10-01-Preview")
40+
response = client.send_request(request)
41+
response.raise_for_status()
42+
response_body = response.json()
43+
print(response_body)
44+
45+
46+
if __name__ == "__main__":
47+
sample_send_request()

0 commit comments

Comments
 (0)