File tree 5 files changed +150
-187
lines changed
doccano_client/repositories
5 files changed +150
-187
lines changed Original file line number Diff line number Diff line change 8
8
from doccano_client .exceptions import DoccanoAPIError
9
9
10
10
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
+
11
37
def verbose_raise_for_status (response : Response ) -> Response :
12
38
"""Output a bad response's text before raising for verbosity, return response otherwise.
13
39
Original file line number Diff line number Diff line change 3
3
from typing import Any , Dict , Iterator
4
4
5
5
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
7
7
8
8
9
9
class ProjectRepository :
@@ -56,16 +56,19 @@ def list(self) -> Iterator[Project]:
56
56
Project: The next project.
57
57
"""
58
58
response = self ._client .get ("projects" )
59
+ initial_url = response .url
59
60
60
61
while True :
61
62
projects = response .json ()
62
63
for project in projects ["results" ]:
63
64
yield self ._to_domain (project )
64
65
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 :
66
69
break
67
70
else :
68
- response = self ._client .get (projects [ "next" ] )
71
+ response = self ._client .get (next_page )
69
72
70
73
def create (self , project : Project ) -> Project :
71
74
"""Create a new project
You can’t perform that action at this time.
0 commit comments