From 366d830d324903e85490553e1990fcd6fcaa7fb6 Mon Sep 17 00:00:00 2001 From: Josef Kudera <46950237+kudj@users.noreply.github.com> Date: Thu, 24 Oct 2024 09:00:45 +0200 Subject: [PATCH] review - using urlparse --- python-sync-actions/src/http_generic/auth.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/python-sync-actions/src/http_generic/auth.py b/python-sync-actions/src/http_generic/auth.py index 733d6d4..8cc990c 100644 --- a/python-sync-actions/src/http_generic/auth.py +++ b/python-sync-actions/src/http_generic/auth.py @@ -3,7 +3,7 @@ import re from abc import ABC, abstractmethod from typing import Callable, Union, Dict, Literal -from urllib.parse import urlencode +from urllib.parse import urlparse, parse_qs, urlencode, urlunparse import requests from requests import auth @@ -167,10 +167,12 @@ def __call__(self, r): r.headers[self.key] = f"{self.token}" elif self.position == 'query': - if '?' in r.url: - r.url = f"{r.url}&{urlencode({self.key: self.token})}" - else: - r.url = f"{r.url}?{urlencode({self.key: self.token})}" + parsed_url = urlparse(r.url) + query_params = parse_qs(parsed_url.query) + query_params.update({self.key: self.token}) + new_query = urlencode(query_params, doseq=True) + r.url = urlunparse(parsed_url._replace(query=new_query)) + else: raise AuthBuilderError(f"Unsupported position {self.position} for API Key auth method") return r