Skip to content

Commit

Permalink
Merge pull request #161 from creisle/fix/host-mistmatch-next-page
Browse files Browse the repository at this point in the history
Fix/host mistmatch next page
  • Loading branch information
Hironsan authored Jan 19, 2024
2 parents 9c2d20a + 60b95a3 commit cbe5036
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 187 deletions.
26 changes: 26 additions & 0 deletions doccano_client/repositories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@
from doccano_client.exceptions import DoccanoAPIError


def get_next_url(base_url: str, initial_url: str, response_data: dict) -> Optional[str]:
"""
Get the "next" url from the response object correcting for issues when the API is running
at the non-default ports
Args:
base_url: the base url of the doccano instance which is passed when creating the doccano client
initial_url: the url which was used for the first non-paged request
response_data: the json payload from the reponse
Returns:
The adjusted url to get the next page or None when no next page is available
"""
if response_data.get("next") is None:
return None
next_url = response_data["next"]
try:
resource = initial_url[len(base_url) :]
_, next_suffix = next_url.split(resource)
return base_url + resource + next_suffix
except ValueError:
# fallback to returning the unmodified next url
return next_url


def verbose_raise_for_status(response: Response) -> Response:
"""Output a bad response's text before raising for verbosity, return response otherwise.
Expand Down
9 changes: 6 additions & 3 deletions doccano_client/repositories/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Any, Dict, Iterator

from doccano_client.models.project import Project
from doccano_client.repositories.base import BaseRepository
from doccano_client.repositories.base import BaseRepository, get_next_url


class ProjectRepository:
Expand Down Expand Up @@ -56,16 +56,19 @@ def list(self) -> Iterator[Project]:
Project: The next project.
"""
response = self._client.get("projects")
initial_url = response.url

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

if projects["next"] is None:
next_page = get_next_url(self._client.api_url, initial_url, projects)

if next_page is None:
break
else:
response = self._client.get(projects["next"])
response = self._client.get(next_page)

def create(self, project: Project) -> Project:
"""Create a new project
Expand Down
Loading

0 comments on commit cbe5036

Please sign in to comment.