Skip to content

Commit

Permalink
feat: use *args and **kwargs on add_path/query
Browse files Browse the repository at this point in the history
`add_path` now uses `*args`
`add_query` now uses both `*args` and `**kwargs`
Closes #13

`add_path` now checks individual types inside given list
Related to #11

BREAKING CHANGE: Modified parameter names for `add_path` and `add_query`
  • Loading branch information
MicaelJarniac committed Apr 3, 2021
1 parent ae6c9ac commit 63072db
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
36 changes: 22 additions & 14 deletions buildurl/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def copy(self) -> "BuildURL":
"""
return deepcopy(self)

def add_path(self, path: Path) -> "BuildURL":
def add_path(self, *args: Path) -> "BuildURL":
"""Add to the path.
Args:
Expand All @@ -80,12 +80,16 @@ def add_path(self, path: Path) -> "BuildURL":
"""

path_list = list()
if isinstance(path, str):
path_list = path.split("/")
elif isinstance(path, list):
path_list = path
else:
raise AttributeError
for path in args:
if isinstance(path, str):
path_list.extend(path.split("/"))
elif isinstance(path, list):
# TODO Convert some types to `str`, like `int` and `float`
if not all((isinstance(p, str) for p in path)):
raise AttributeError
path_list.extend(path)
else:
raise AttributeError

if len(path_list):
self.trailing_slash = path_list[-1] == ""
Expand All @@ -95,7 +99,7 @@ def add_path(self, path: Path) -> "BuildURL":

return self

def add_query(self, query: Query) -> "BuildURL":
def add_query(self, *args: Query, **kwargs) -> "BuildURL":
"""Add a query argument.
Args:
Expand All @@ -116,12 +120,16 @@ def add_query(self, query: Query) -> "BuildURL":
"""

query_dict = dict()
if isinstance(query, str):
query_dict = parse_qs(query)
elif isinstance(query, dict):
query_dict = query
else:
raise AttributeError
for query in args:
if isinstance(query, str):
query_dict.update(parse_qs(query))
elif isinstance(query, dict):
query_dict.update(query)
else:
raise AttributeError

if kwargs:
query_dict.update(kwargs)

self.query_dict.update(query_dict)

Expand Down
8 changes: 8 additions & 0 deletions tests/test_buildurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def test_path():
url = BuildURL("https://example.com/folder/")
assert url.get == "https://example.com/folder/"

url = BuildURL("https://example.com")
url.add_path("one", "two", ["three", "four", "five", "six"], "seven//eight")
assert url.get == "https://example.com/one/two/three/four/five/six/seven/eight"


def test_query():
url = BuildURL("https://example.com")
Expand Down Expand Up @@ -108,6 +112,10 @@ def test_query():
url.query = None
assert url.get == "https://example.com"

url = BuildURL("https://example.com")
url.add_query("a=b&c=d", {"e": "f", "g": "h"}, i="j", k="l")
assert url.get == "https://example.com?a=b&c=d&e=f&g=h&i=j&k=l"


def test_copy():
url = BuildURL("https://example.com")
Expand Down

0 comments on commit 63072db

Please sign in to comment.