From b4dd737d4db5b6fa3479a9eea725021d03427064 Mon Sep 17 00:00:00 2001 From: JVH Date: Sat, 25 May 2024 16:16:14 +0200 Subject: [PATCH 1/2] Use client base url in example pagination --- doccano_client/repositories/example.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/doccano_client/repositories/example.py b/doccano_client/repositories/example.py index 30b7e9b..2936e08 100644 --- a/doccano_client/repositories/example.py +++ b/doccano_client/repositories/example.py @@ -1,6 +1,7 @@ from __future__ import annotations from typing import Iterator, List, Optional +from urllib.parse import urlparse from doccano_client.models.example import Example from doccano_client.repositories.base import BaseRepository @@ -60,7 +61,15 @@ def list(self, project_id: int, is_confirmed: Optional[bool] = None) -> Iterator if examples["next"] is None: break else: - response = self._client.get(examples["next"]) + next_url = examples["next"] + response = self._client.get(next_url) + # If the response we got is not json parseable, most likely the hostname was set wrongly. + # This can happen if doccano is hosted behind a reverse proxy. Thus, we use the client's + # base url + if not response.headers.get('content-type') == 'application/json': + client_netloc = urlparse(self._client._base_url).netloc + next_url = urlparse(examples["next"])._replace(netloc=client_netloc).get_url() + response = self._client.get(next_url) def create(self, project_id: int, example: Example) -> Example: """Create a new example From 8c25c13bc34fa94057edb78b4f4f6b0002840862 Mon Sep 17 00:00:00 2001 From: JVH Date: Sat, 25 May 2024 16:23:59 +0200 Subject: [PATCH 2/2] Also update scheme (http or https) --- doccano_client/repositories/example.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doccano_client/repositories/example.py b/doccano_client/repositories/example.py index 2936e08..2192295 100644 --- a/doccano_client/repositories/example.py +++ b/doccano_client/repositories/example.py @@ -67,8 +67,8 @@ def list(self, project_id: int, is_confirmed: Optional[bool] = None) -> Iterator # This can happen if doccano is hosted behind a reverse proxy. Thus, we use the client's # base url if not response.headers.get('content-type') == 'application/json': - client_netloc = urlparse(self._client._base_url).netloc - next_url = urlparse(examples["next"])._replace(netloc=client_netloc).get_url() + client_url_parsed = urlparse(self._client._base_url) + next_url = urlparse(examples["next"])._replace(scheme=client_url_parsed.scheme, netloc=client_url_parsed.netloc).geturl() response = self._client.get(next_url) def create(self, project_id: int, example: Example) -> Example: