-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial version of
rest_client.paginate()
- Loading branch information
Showing
6 changed files
with
168 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,46 @@ | ||
from typing import Optional, Dict, Iterator, Union, Any | ||
|
||
from dlt.common import jsonpath | ||
|
||
from .client import RESTClient # noqa: F401 | ||
from .client import PageData | ||
from .auth import AuthConfigBase | ||
from .paginators import BasePaginator | ||
from .typing import HTTPMethodBasic, Hooks | ||
|
||
|
||
def paginate( | ||
url: str, | ||
method: HTTPMethodBasic = "GET", | ||
headers: Optional[Dict[str, str]] = None, | ||
params: Optional[Dict[str, Any]] = None, | ||
json: Optional[Dict[str, Any]] = None, | ||
auth: AuthConfigBase = None, | ||
paginator: Union[str, BasePaginator] = None, | ||
data_selector: Optional[jsonpath.TJsonPath] = None, | ||
hooks: Optional[Hooks] = None, | ||
) -> Iterator[PageData[Any]]: | ||
""" | ||
Paginate over a REST API endpoint. | ||
Args: | ||
url: URL to paginate over. | ||
**kwargs: Keyword arguments to pass to `RESTClient.paginate`. | ||
Returns: | ||
Iterator[Page]: Iterator over pages. | ||
""" | ||
client = RESTClient( | ||
base_url=url, | ||
headers=headers, | ||
) | ||
return client.paginate( | ||
path="", | ||
method=method, | ||
params=params, | ||
json=json, | ||
auth=auth, | ||
paginator=paginator, | ||
data_selector=data_selector, | ||
hooks=hooks, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
from typing import ( | ||
List, | ||
Dict, | ||
Union, | ||
Literal, | ||
Callable, | ||
Any, | ||
) | ||
from dlt.sources.helpers.requests import Response | ||
|
||
|
||
HTTPMethodBasic = Literal["GET", "POST"] | ||
HTTPMethodExtended = Literal["PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"] | ||
HTTPMethod = Union[HTTPMethodBasic, HTTPMethodExtended] | ||
HookFunction = Callable[[Response, Any, Any], None] | ||
HookEvent = Union[HookFunction, List[HookFunction]] | ||
Hooks = Dict[str, HookEvent] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import pytest | ||
from dlt.sources.helpers.rest_client.utils import join_url | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"base_url, path, expected", | ||
[ | ||
# Normal cases | ||
( | ||
"http://example.com", | ||
"path/to/resource", | ||
"http://example.com/path/to/resource", | ||
), | ||
( | ||
"http://example.com/", | ||
"/path/to/resource", | ||
"http://example.com/path/to/resource", | ||
), | ||
( | ||
"http://example.com/", | ||
"path/to/resource", | ||
"http://example.com/path/to/resource", | ||
), | ||
( | ||
"http://example.com", | ||
"//path/to/resource", | ||
"http://example.com/path/to/resource", | ||
), | ||
( | ||
"http://example.com///", | ||
"//path/to/resource", | ||
"http://example.com/path/to/resource", | ||
), | ||
# Trailing and leading slashes | ||
("http://example.com/", "/", "http://example.com/"), | ||
("http://example.com", "/", "http://example.com/"), | ||
("http://example.com/", "///", "http://example.com/"), | ||
("http://example.com", "///", "http://example.com/"), | ||
("/", "path/to/resource", "/path/to/resource"), | ||
("/", "/path/to/resource", "/path/to/resource"), | ||
# Empty strings | ||
("", "", ""), | ||
( | ||
"", | ||
"http://example.com/path/to/resource", | ||
"http://example.com/path/to/resource", | ||
), | ||
("", "path/to/resource", "path/to/resource"), | ||
("http://example.com", "", "http://example.com"), | ||
# Query parameters and fragments | ||
( | ||
"http://example.com", | ||
"path/to/resource?query=123", | ||
"http://example.com/path/to/resource?query=123", | ||
), | ||
( | ||
"http://example.com/", | ||
"path/to/resource#fragment", | ||
"http://example.com/path/to/resource#fragment", | ||
), | ||
# Special characters in the path | ||
( | ||
"http://example.com", | ||
"/path/to/resource with spaces", | ||
"http://example.com/path/to/resource with spaces", | ||
), | ||
("http://example.com", "/path/with/中文", "http://example.com/path/with/中文"), | ||
# Protocols and subdomains | ||
("https://sub.example.com", "path", "https://sub.example.com/path"), | ||
("ftp://example.com", "/path", "ftp://example.com/path"), | ||
# Missing protocol in base_url | ||
("example.com", "path", "example.com/path"), | ||
], | ||
) | ||
def test_join_url(base_url, path, expected): | ||
assert join_url(base_url, path) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"base_url, path, exception", | ||
[ | ||
(None, "path", ValueError), | ||
("http://example.com", None, AttributeError), | ||
(123, "path", AttributeError), | ||
("http://example.com", 123, AttributeError), | ||
], | ||
) | ||
def test_join_url_invalid_input_types(base_url, path, exception): | ||
with pytest.raises(exception): | ||
join_url(base_url, path) |