Skip to content

Commit e5e1bc1

Browse files
authored
Merge pull request #229 from telekom-mms/fix/url-params
fix: params should be an dict and not written directly to urls this fixes #228
2 parents 0abb185 + 7003017 commit e5e1bc1

1 file changed

Lines changed: 39 additions & 14 deletions

File tree

fortilib/fortigateapi.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -594,12 +594,16 @@ def __init__(
594594
url: str,
595595
query_type: FortiGateQueryType,
596596
identifier: str,
597-
data: Dict,
597+
data: dict | None = None,
598+
params: dict | None = None,
598599
):
599600
self.url: str = url
601+
if params:
602+
self.url = f"{self.url}?{urllib.parse.urlencode(params)}"
603+
600604
self.query_type: FortiGateQueryType = query_type
601605
self.identifier: str = identifier
602-
self.data: Dict = data
606+
self.data: dict = data or dict()
603607

604608
def __repr__(self):
605609
return (
@@ -706,16 +710,19 @@ def logout(self):
706710
url = self.urlbase + "logout"
707711
self.client.get(url)
708712

709-
def does_exist(self, object_url: str) -> bool:
713+
def does_exist(self, object_url: str, params: dict | None = None) -> bool:
714+
params = params or dict()
715+
params.update(vdom=self.vdom)
716+
710717
response = self.client.get(
711718
object_url,
712-
params=f"vdom={self.vdom}",
719+
params=params,
713720
)
714721
if response.status_code == 200:
715722
return True
716723
return False
717724

718-
def get(self, url, params: Optional[dict] = None):
725+
def get(self, url, params: dict | None = None):
719726
params = params or dict()
720727
params.update(vdom=self.vdom)
721728

@@ -724,24 +731,33 @@ def get(self, url, params: Optional[dict] = None):
724731
params=params,
725732
)
726733

727-
def put(self, url, data: Dict):
734+
def put(self, url, data: dict | None, params: dict | None = None):
735+
params = params or dict()
736+
params.update(vdom=self.vdom)
737+
728738
return self.client.put(
729739
url,
730740
json=data,
731-
params=f"vdom={self.vdom}",
741+
params=params,
732742
)
733743

734-
def post(self, url, data: Dict):
744+
def post(self, url, data: dict, params: dict | None = None):
745+
params = params or dict()
746+
params.update(vdom=self.vdom)
747+
735748
return self.client.post(
736749
url,
737750
json=data,
738-
params=f"vdom={self.vdom}",
751+
params=params,
739752
)
740753

741-
def delete(self, url):
754+
def delete(self, url, params: dict | None = None):
755+
params = params or dict()
756+
params.update(vdom=self.vdom)
757+
742758
return self.client.delete(
743759
url,
744-
params=f"vdom={self.vdom}",
760+
params=params,
745761
)
746762

747763
def check_response_code(self, response: httpx.Response):
@@ -837,10 +853,18 @@ def query_api_move(
837853
move_identifier: str = None,
838854
) -> Union[dict, int]:
839855
api_url = self.urlbase + uri
840-
move_url = f"{api_url}/{identifier}?action=move&{move_direction.value}={move_identifier}"
856+
move_url = f"{api_url}/{identifier}"
857+
params = {
858+
"action": "move",
859+
move_direction.value: move_identifier,
860+
}
841861
self.operations.append(
842862
FortiGateOperation(
843-
move_url, FortiGateQueryType.PUT, identifier, ""
863+
move_url,
864+
FortiGateQueryType.PUT,
865+
identifier,
866+
data=None,
867+
params=params,
844868
)
845869
)
846870

@@ -849,7 +873,8 @@ def query_api_move(
849873

850874
result = self.put(
851875
move_url,
852-
None,
876+
data=None,
877+
params=params,
853878
)
854879
self.check_response_code(result)
855880
return result.json()

0 commit comments

Comments
 (0)