Skip to content

Commit

Permalink
Move fetch_url inside the signer class.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Oct 23, 2023
1 parent cb76874 commit 9e6b9aa
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions opensearchpy/helpers/signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,6 @@
from urllib.parse import parse_qs, urlencode, urlparse


def fetch_url(prepared_request): # type: ignore
"""
This is a util method that helps in reconstructing the request url.
:param prepared_request: unsigned request
:return: reconstructed url
"""
url = urlparse(prepared_request.url)
path = url.path or "/"

# fetch the query string if present in the request
querystring = ""
if url.query:
querystring = "?" + urlencode(
parse_qs(url.query, keep_blank_values=True), doseq=True
)

# fetch the host information from headers
headers = dict(
(key.lower(), value) for key, value in prepared_request.headers.items()
)
location = headers.get("host") or url.netloc

# construct the url and return
return url.scheme + "://" + location + path + querystring


class AWSV4Signer:
"""
Generic AWS V4 Request Signer.
Expand Down Expand Up @@ -122,13 +96,38 @@ def _sign_request(self, prepared_request): # type: ignore
prepared_request.headers.update(
self.signer.sign(
prepared_request.method,
fetch_url(prepared_request), # type: ignore
self._fetch_url(prepared_request), # type: ignore
prepared_request.body,
)
)

return prepared_request

def _fetch_url(self, prepared_request): # type: ignore
"""
This is a util method that helps in reconstructing the request url.
:param prepared_request: unsigned request
:return: reconstructed url
"""
url = urlparse(prepared_request.url)
path = url.path or "/"

# fetch the query string if present in the request
querystring = ""
if url.query:
querystring = "?" + urlencode(

Check warning on line 118 in opensearchpy/helpers/signer.py

View check run for this annotation

Codecov / codecov/patch

opensearchpy/helpers/signer.py#L118

Added line #L118 was not covered by tests
parse_qs(url.query, keep_blank_values=True), doseq=True
)

# fetch the host information from headers
headers = dict(
(key.lower(), value) for key, value in prepared_request.headers.items()
)
location = headers.get("host") or url.netloc

# construct the url and return
return url.scheme + "://" + location + path + querystring


# Deprecated: use RequestsAWSV4SignerAuth
class AWSV4SignerAuth(RequestsAWSV4SignerAuth):
Expand Down

0 comments on commit 9e6b9aa

Please sign in to comment.