Skip to content

Commit bc91a91

Browse files
committed
Text-analytics: new get_analytics get_annotated functions
1 parent e3f338f commit bc91a91

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ api = RPApi(api_key="YOUR_API_KEY")
3333
To create a dataset you can call the `create_dataset` method of the API with a Dataset instance.
3434

3535
```python
36+
from ravenpackapi import Dataset
37+
3638
ds = api.create_dataset(
3739
Dataset(
3840
name="New Dataset",

ravenpackapi/upload/models.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def save_original(self, filename):
7373

7474
@api_method
7575
def save_analytics(self, filename, output_format='application/json'):
76+
self.wait_for_completion()
7677
response = self.api.request('%s/files/%s/analytics' % (self.api._UPLOAD_BASE_URL, self.file_id,),
7778
headers=dict(
7879
Accept=output_format,
@@ -83,14 +84,34 @@ def save_analytics(self, filename, output_format='application/json'):
8384
for chunk in response.iter_content(chunk_size=self.api._CHUNK_SIZE):
8485
f.write(chunk)
8586

87+
@api_method
88+
def get_analytics(self, output_format='application/json'):
89+
self.wait_for_completion()
90+
response = self.api.request('%s/files/%s/analytics' % (self.api._UPLOAD_BASE_URL, self.file_id,),
91+
headers=dict(
92+
Accept=output_format,
93+
**self.api.headers
94+
))
95+
if output_format == 'application/json':
96+
return response.json()
97+
else:
98+
return response.text
99+
86100
@api_method
87101
def save_annotated(self, filename):
102+
self.wait_for_completion()
88103
response = self.api.request('%s/files/%s/annotated' % (self.api._UPLOAD_BASE_URL, self.file_id),
89104
stream=True)
90105
with open(filename, 'wb') as f:
91106
for chunk in response.iter_content(chunk_size=self.api._CHUNK_SIZE):
92107
f.write(chunk)
93108

109+
@api_method
110+
def get_annotated(self):
111+
self.wait_for_completion()
112+
response = self.api.request('%s/files/%s/annotated' % (self.api._UPLOAD_BASE_URL, self.file_id))
113+
return response.text
114+
94115
@api_method
95116
def delete(self):
96117
response = self.api.request('%s/files/%s' % (self.api._UPLOAD_BASE_URL, self.file_id),
@@ -114,7 +135,7 @@ def set_metadata(self,
114135
method='patch')
115136

116137
def wait_for_completion(self):
117-
while self.status not in {"COMPLETED", "DELETED"}:
138+
while self.status not in {"COMPLETED", "DELETED", "FAILED"}:
118139
sleep(1)
119140
self.get_status()
120141

0 commit comments

Comments
 (0)