Skip to content

Commit cbe5036

Browse files
authored
Merge pull request #161 from creisle/fix/host-mistmatch-next-page
Fix/host mistmatch next page
2 parents 9c2d20a + 60b95a3 commit cbe5036

File tree

5 files changed

+150
-187
lines changed

5 files changed

+150
-187
lines changed

doccano_client/repositories/base.py

+26
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@
88
from doccano_client.exceptions import DoccanoAPIError
99

1010

11+
def get_next_url(base_url: str, initial_url: str, response_data: dict) -> Optional[str]:
12+
"""
13+
Get the "next" url from the response object correcting for issues when the API is running
14+
at the non-default ports
15+
16+
Args:
17+
base_url: the base url of the doccano instance which is passed when creating the doccano client
18+
initial_url: the url which was used for the first non-paged request
19+
response_data: the json payload from the reponse
20+
21+
Returns:
22+
The adjusted url to get the next page or None when no next page is available
23+
24+
"""
25+
if response_data.get("next") is None:
26+
return None
27+
next_url = response_data["next"]
28+
try:
29+
resource = initial_url[len(base_url) :]
30+
_, next_suffix = next_url.split(resource)
31+
return base_url + resource + next_suffix
32+
except ValueError:
33+
# fallback to returning the unmodified next url
34+
return next_url
35+
36+
1137
def verbose_raise_for_status(response: Response) -> Response:
1238
"""Output a bad response's text before raising for verbosity, return response otherwise.
1339

doccano_client/repositories/project.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Any, Dict, Iterator
44

55
from doccano_client.models.project import Project
6-
from doccano_client.repositories.base import BaseRepository
6+
from doccano_client.repositories.base import BaseRepository, get_next_url
77

88

99
class ProjectRepository:
@@ -56,16 +56,19 @@ def list(self) -> Iterator[Project]:
5656
Project: The next project.
5757
"""
5858
response = self._client.get("projects")
59+
initial_url = response.url
5960

6061
while True:
6162
projects = response.json()
6263
for project in projects["results"]:
6364
yield self._to_domain(project)
6465

65-
if projects["next"] is None:
66+
next_page = get_next_url(self._client.api_url, initial_url, projects)
67+
68+
if next_page is None:
6669
break
6770
else:
68-
response = self._client.get(projects["next"])
71+
response = self._client.get(next_page)
6972

7073
def create(self, project: Project) -> Project:
7174
"""Create a new project

0 commit comments

Comments
 (0)