Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/host mistmatch next page #161

Merged
merged 5 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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