Skip to content

Commit

Permalink
Fixed CI errors
Browse files Browse the repository at this point in the history
Signed-off-by: Nathalie Jonathan <[email protected]>
  • Loading branch information
nathaliellenaa committed Nov 22, 2024
1 parent 9926ff2 commit cc18e36
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
5 changes: 1 addition & 4 deletions opensearchpy/_async/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,6 @@ async def perform_request(
method, params, body, ignore, timeout
)

from urllib.parse import quote

for attempt in range(self.max_retries + 1):
connection = self.get_connection()

Expand All @@ -399,8 +397,7 @@ async def perform_request(
ignore=ignore,
timeout=timeout,
)
url = quote(url)


# Lowercase all the header names for consistency in accessing them.
headers_response = {
header.lower(): value for header, value in headers_response.items()
Expand Down
16 changes: 14 additions & 2 deletions opensearchpy/client/indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,11 +583,17 @@ def delete(
received before the timeout expires, the request fails and returns an
error.
"""
from urllib.parse import quote

if index in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'index'.")

if isinstance(index, (list, tuple)):
index = ",".join(str(x) for x in index)

encoded_index = quote(index, safe=",")
return self.transport.perform_request(
"DELETE", _make_path(index), params=params, headers=headers
"DELETE", _make_path(encoded_index), params=params, headers=headers
)

@query_params(
Expand Down Expand Up @@ -646,11 +652,17 @@ def exists(
:arg source: The URL-encoded request definition. Useful for
libraries that do not accept a request body for non-POST requests.
"""
from urllib.parse import quote

if index in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'index'.")

if isinstance(index, (list, tuple)):
index = ",".join(str(x) for x in index)

encoded_index = quote(index, safe=",")
return self.transport.perform_request(
"HEAD", _make_path(index), params=params, headers=headers
"HEAD", _make_path(encoded_index), params=params, headers=headers
)

@query_params(
Expand Down
8 changes: 2 additions & 6 deletions opensearchpy/client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

from opensearchpy.serializer import Serializer

from ..compat import quote, string_types, to_bytes, to_str, unquote, urlparse
from ..compat import string_types, to_bytes, to_str, unquote, urlparse

# parts of URL to be omitted
SKIP_IN_PATH: Any = (None, "", b"", [], ())
Expand Down Expand Up @@ -120,11 +120,7 @@ def _make_path(*parts: Any) -> str:
Convert lists and tuples to comma separated values.
"""
# TODO: maybe only allow some parts to be lists/tuples ?
return "/" + "/".join(
str(p)
for p in parts
if p not in SKIP_IN_PATH
)
return "/" + "/".join(str(p) for p in parts if p not in SKIP_IN_PATH)


# parameters that apply to all methods
Expand Down
20 changes: 12 additions & 8 deletions test_opensearchpy/test_connection/test_requests_http_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,8 @@ def test_aws_signer_signs_with_query_string(self, mock_sign: Any) -> None:
def test_aws_signer_consitent_url(self) -> None:
region = "us-west-2"

from typing import Any, Collection, Mapping, Optional, Union

from opensearchpy import OpenSearch
from opensearchpy.helpers.signer import RequestsAWSV4SignerAuth

Expand All @@ -528,7 +530,7 @@ def test_aws_signer_consitent_url(self) -> None:

# Create a mock signer class to capture the signed URL
class MockSigner(RequestsAWSV4SignerAuth):
def __call__(self, prepared_request):
def __call__(self, prepared_request): # type: ignore
nonlocal signed_url
if isinstance(prepared_request, str):
signed_url = prepared_request
Expand All @@ -538,16 +540,17 @@ def __call__(self, prepared_request):

# Create a mock connection class to capture the sent URL
class MockConnection(RequestsHttpConnection):
def perform_request(
def perform_request( # type: ignore
self,
method: str,
url: str,
params=None,
body=None,
timeout=None,
ignore=(),
headers=None,
):
params: Optional[Mapping[str, Any]] = None,
body: Optional[bytes] = None,
timeout: Optional[Union[int, float]] = None,
allow_redirects: Optional[bool] = True,
ignore: Collection[int] = (),
headers: Optional[Mapping[str, str]] = None,
) -> Any:
nonlocal sent_url
sent_url = f"{self.host}{url}"
return 200, {}, "{}"
Expand All @@ -564,6 +567,7 @@ def perform_request(
client.index("index", {"test": "data"}, id=doc_id)
assert signed_url == sent_url, "URLs don't match"


class TestRequestsConnectionRedirect(TestCase):
server1: TestHTTPServer
server2: TestHTTPServer
Expand Down

0 comments on commit cc18e36

Please sign in to comment.