10
10
import ndjson
11
11
import requests
12
12
from pydantic import BaseModel , validator
13
+ from requests .api import request
13
14
from typing_extensions import Literal
14
15
from typing import (Any , List , Optional , BinaryIO , Dict , Iterable , Tuple , Union ,
15
16
Type , Set )
@@ -115,28 +116,41 @@ class BulkImportRequest(DbObject):
115
116
created_by = Relationship .ToOne ("User" , False , "created_by" )
116
117
117
118
@property
118
- def inputs (self ) -> Optional [ List [Dict [str , str ] ]]:
119
+ def inputs (self ) -> List [Dict [str , Any ]]:
119
120
"""
120
121
Inputs for each individual annotation uploaded.
121
122
This should match the ndjson annotations that you have uploaded.
122
123
123
124
Returns:
124
- Uploaded ndjsons .
125
+ Uploaded ndjson .
125
126
126
127
* This information will expire after 24 hours.
127
128
"""
128
129
return self ._fetch_remote_ndjson (self .input_file_url )
129
130
130
131
@property
131
- def errors (self ) -> Optional [ List [Dict [str , str ] ]]:
132
+ def errors (self ) -> List [Dict [str , Any ]]:
132
133
"""
133
- Errors for each individual annotation uploaded.
134
+ Errors for each individual annotation uploaded. This is a subset of statuses
134
135
135
136
Returns:
136
- Empty list if there are no errors and None if the update is still running.
137
- If there are errors, and the job has completed then a list of dicts containing the error messages will be returned.
138
- See below table for detailed response fields.
137
+ List of dicts containing error messages. Empty list means there were no errors
138
+ See `BulkImportRequest.statuses` for more details.
139
139
140
+ * This information will expire after 24 hours.
141
+ """
142
+ self .wait_until_done ()
143
+ return self ._fetch_remote_ndjson (self .error_file_url )
144
+
145
+ @property
146
+ def statuses (self ) -> List [Dict [str , Any ]]:
147
+ """
148
+ Status for each individual annotation uploaded.
149
+
150
+ Returns:
151
+ A status for each annotation if the upload is done running.
152
+ See below table for more details
153
+
140
154
.. list-table::
141
155
:widths: 15 150
142
156
:header-rows: 1
@@ -150,39 +164,29 @@ def errors(self) -> Optional[List[Dict[str, str]]]:
150
164
* - status
151
165
- Indicates SUCCESS or FAILURE.
152
166
* - errors
153
- - An array of error messages included when status is FAILURE. Each error has a name, message and optional (can be null) additional_info.
154
-
155
- * This information will expire after 24 hours.
156
- """
157
- return self ._fetch_remote_ndjson (self .error_file_url )
158
-
159
- @property
160
- def statuses (self ) -> Optional [List [Dict [str , str ]]]:
161
- """
162
- Status for each individual annotation uploaded.
163
-
164
- Returns:
165
- A status for each annotation if the upload is done running and was successful. Otherwise it returns None.
167
+ - An array of error messages included when status is FAILURE. Each error has a name, message and optional (key might not exist) additional_info.
166
168
167
169
* This information will expire after 24 hours.
168
170
"""
171
+ self .wait_until_done ()
169
172
return self ._fetch_remote_ndjson (self .status_file_url )
170
173
171
174
@functools .lru_cache ()
172
- def _fetch_remote_ndjson (
173
- self , url : Optional [str ]) -> Optional [List [Dict [str , str ]]]:
175
+ def _fetch_remote_ndjson (self , url : str ) -> List [Dict [str , Any ]]:
174
176
"""
175
177
Fetches the remote ndjson file and caches the results.
176
178
177
179
Args:
178
- url (str): either the input_file_url, error_file_url, status_file_url, or None
179
- urls are None when the file is unavailable.
180
+ url (str): Can be any url pointing to an ndjson file.
180
181
Returns:
181
- None if the url is None or the ndjson as a list of dicts.
182
+ ndjson as a list of dicts.
182
183
"""
183
- if url is not None :
184
- return ndjson .loads (requests .get (url ).text )
185
- return None
184
+ if url is None :
185
+ raise ValueError ("Must provide valid ndjson url. Found `None`" )
186
+
187
+ response = requests .get (url )
188
+ response .raise_for_status ()
189
+ return ndjson .loads (response .text )
186
190
187
191
def refresh (self ) -> None :
188
192
"""Synchronizes values of all fields with the database.
0 commit comments