Skip to content

Commit

Permalink
feat: allow chaining with add_path/add_query
Browse files Browse the repository at this point in the history
Related to #12
  • Loading branch information
MicaelJarniac committed Apr 3, 2021
1 parent 0cdb580 commit ae6c9ac
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions buildurl/builder.py
Expand Up @@ -57,7 +57,7 @@ def copy(self) -> "BuildURL":
"""
return deepcopy(self)

def add_path(self, path: Path) -> None:
def add_path(self, path: Path) -> "BuildURL":
"""Add to the path.
Args:
Expand Down Expand Up @@ -93,7 +93,9 @@ def add_path(self, path: Path) -> None:

self._path_list.extend(path_list)

def add_query(self, query: Query) -> None:
return self

def add_query(self, query: Query) -> "BuildURL":
"""Add a query argument.
Args:
Expand Down Expand Up @@ -123,6 +125,8 @@ def add_query(self, query: Query) -> None:

self.query_dict.update(query_dict)

return self

@property
def path(self) -> str:
"""Path string."""
Expand Down
8 changes: 8 additions & 0 deletions tests/test_buildurl.py
Expand Up @@ -125,3 +125,11 @@ def test_repr():
assert repr(url) == "BuildURL(base='https://example.com/repr')"
url += {"testing": "it"}
assert repr(url) == "BuildURL(base='https://example.com/repr?testing=it')"


def test_chaining():
url = BuildURL("https://example.com")
url.add_path("one").add_path("two")
assert url.get == "https://example.com/one/two"
url.add_query({"test": "more"}).add_path("three").add_query("testing=alot")
assert url.get == "https://example.com/one/two/three?test=more&testing=alot"

0 comments on commit ae6c9ac

Please sign in to comment.