Skip to content

Commit 2eb8b74

Browse files
Merge pull request #4 from VH-Lab/ndi-cloud-api-documents-structure-10462111104353685010
Restructure ndi.cloud.api.documents
2 parents 031b9a8 + 2488c17 commit 2eb8b74

9 files changed

Lines changed: 384 additions & 0 deletions

File tree

src/ndi/cloud/api/documents.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from .implementation.documents.add_document import AddDocument as AddDocumentImpl
2+
from .implementation.documents.get_document import GetDocument as GetDocumentImpl
3+
from .implementation.documents.update_document import UpdateDocument as UpdateDocumentImpl
4+
from .implementation.documents.delete_document import DeleteDocument as DeleteDocumentImpl
5+
from .implementation.documents.list_dataset_documents import ListDatasetDocuments as ListDatasetDocumentsImpl
6+
7+
def add_document(dataset_id, document_info):
8+
"""
9+
Adds a document to a dataset.
10+
11+
Args:
12+
dataset_id (str): The ID of the dataset.
13+
document_info (dict): The document information to add.
14+
15+
Returns:
16+
tuple: (success, answer, response, url)
17+
"""
18+
api_call = AddDocumentImpl(dataset_id, document_info)
19+
return api_call.execute()
20+
21+
def get_document(dataset_id, document_id):
22+
"""
23+
Gets a document from a dataset.
24+
25+
Args:
26+
dataset_id (str): The ID of the dataset.
27+
document_id (str): The ID of the document.
28+
29+
Returns:
30+
tuple: (success, answer, response, url)
31+
"""
32+
api_call = GetDocumentImpl(dataset_id, document_id)
33+
return api_call.execute()
34+
35+
def update_document(dataset_id, document_id, document_info):
36+
"""
37+
Updates a document in a dataset.
38+
39+
Args:
40+
dataset_id (str): The ID of the dataset.
41+
document_id (str): The ID of the document.
42+
document_info (dict): The updated document information.
43+
44+
Returns:
45+
tuple: (success, answer, response, url)
46+
"""
47+
api_call = UpdateDocumentImpl(dataset_id, document_id, document_info)
48+
return api_call.execute()
49+
50+
def delete_document(dataset_id, document_id):
51+
"""
52+
Deletes a document from a dataset.
53+
54+
Args:
55+
dataset_id (str): The ID of the dataset.
56+
document_id (str): The ID of the document.
57+
58+
Returns:
59+
tuple: (success, answer, response, url)
60+
"""
61+
api_call = DeleteDocumentImpl(dataset_id, document_id)
62+
return api_call.execute()
63+
64+
def list_dataset_documents(dataset_id, page=1, page_size=20):
65+
"""
66+
Lists documents in a dataset.
67+
68+
Args:
69+
dataset_id (str): The ID of the dataset.
70+
page (int, optional): The page number. Defaults to 1.
71+
page_size (int, optional): The number of documents per page. Defaults to 20.
72+
73+
Returns:
74+
tuple: (success, answer, response, url)
75+
"""
76+
api_call = ListDatasetDocumentsImpl(dataset_id, page, page_size)
77+
return api_call.execute()

src/ndi/cloud/api/implementation/__init__.py

Whitespace-only changes.

src/ndi/cloud/api/implementation/documents/__init__.py

Whitespace-only changes.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from ...call import Call
2+
from ... import url
3+
from ....authenticate import authenticate
4+
import requests
5+
import json
6+
7+
class AddDocument(Call):
8+
"""
9+
Implementation class for adding a new document to a dataset.
10+
"""
11+
12+
def __init__(self, dataset_id, document_info):
13+
"""
14+
Creates a new AddDocument API call object.
15+
16+
Args:
17+
dataset_id (str): The ID of the dataset.
18+
document_info (dict): The document information to add.
19+
"""
20+
self.dataset_id = dataset_id
21+
self.document_info = document_info
22+
self.endpoint_name = 'add_document'
23+
24+
def execute(self):
25+
"""
26+
Performs the API call to add a document.
27+
"""
28+
token = authenticate()
29+
30+
api_url = url.get_url(self.endpoint_name, dataset_id=self.dataset_id)
31+
32+
headers = {
33+
'Accept': 'application/json',
34+
'Content-Type': 'application/json',
35+
'Authorization': f'Bearer {token}'
36+
}
37+
38+
response = requests.post(api_url, headers=headers, json=self.document_info)
39+
40+
if response.status_code in [200, 201]:
41+
return True, response.json(), response, api_url
42+
else:
43+
try:
44+
answer = response.json()
45+
except json.JSONDecodeError:
46+
answer = response.text
47+
return False, answer, response, api_url
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from ...call import Call
2+
from ... import url
3+
from ....authenticate import authenticate
4+
import requests
5+
import json
6+
7+
class DeleteDocument(Call):
8+
"""
9+
Implementation class for deleting a document from a dataset.
10+
"""
11+
12+
def __init__(self, dataset_id, document_id):
13+
"""
14+
Creates a new DeleteDocument API call object.
15+
16+
Args:
17+
dataset_id (str): The ID of the dataset.
18+
document_id (str): The ID of the document.
19+
"""
20+
self.dataset_id = dataset_id
21+
self.document_id = document_id
22+
self.endpoint_name = 'delete_document'
23+
24+
def execute(self):
25+
"""
26+
Performs the API call to delete a document.
27+
"""
28+
token = authenticate()
29+
30+
api_url = url.get_url(self.endpoint_name, dataset_id=self.dataset_id, document_id=self.document_id)
31+
32+
headers = {
33+
'Accept': 'application/json',
34+
'Authorization': f'Bearer {token}'
35+
}
36+
37+
response = requests.delete(api_url, headers=headers)
38+
39+
if response.status_code == 200:
40+
return True, response.json(), response, api_url
41+
else:
42+
try:
43+
answer = response.json()
44+
except json.JSONDecodeError:
45+
answer = response.text
46+
return False, answer, response, api_url
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from ...call import Call
2+
from ... import url
3+
from ....authenticate import authenticate
4+
import requests
5+
import json
6+
7+
class GetDocument(Call):
8+
"""
9+
Implementation class for getting a document from a dataset.
10+
"""
11+
12+
def __init__(self, dataset_id, document_id):
13+
"""
14+
Creates a new GetDocument API call object.
15+
16+
Args:
17+
dataset_id (str): The ID of the dataset.
18+
document_id (str): The ID of the document.
19+
"""
20+
self.dataset_id = dataset_id
21+
self.document_id = document_id
22+
self.endpoint_name = 'get_document'
23+
24+
def execute(self):
25+
"""
26+
Performs the API call to get a document.
27+
"""
28+
token = authenticate()
29+
30+
api_url = url.get_url(self.endpoint_name, dataset_id=self.dataset_id, document_id=self.document_id)
31+
32+
headers = {
33+
'Accept': 'application/json',
34+
'Authorization': f'Bearer {token}'
35+
}
36+
37+
response = requests.get(api_url, headers=headers)
38+
39+
if response.status_code == 200:
40+
return True, response.json(), response, api_url
41+
else:
42+
try:
43+
answer = response.json()
44+
except json.JSONDecodeError:
45+
answer = response.text
46+
return False, answer, response, api_url
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from ...call import Call
2+
from ... import url
3+
from ....authenticate import authenticate
4+
import requests
5+
import json
6+
7+
class ListDatasetDocuments(Call):
8+
"""
9+
Implementation class for listing documents in a dataset.
10+
"""
11+
12+
def __init__(self, dataset_id, page=1, page_size=20):
13+
"""
14+
Creates a new ListDatasetDocuments API call object.
15+
16+
Args:
17+
dataset_id (str): The ID of the dataset.
18+
page (int, optional): The page number. Defaults to 1.
19+
page_size (int, optional): The number of documents per page. Defaults to 20.
20+
"""
21+
self.dataset_id = dataset_id
22+
self.page = page
23+
self.page_size = page_size
24+
self.endpoint_name = 'list_dataset_documents'
25+
26+
def execute(self):
27+
"""
28+
Performs the API call to list documents.
29+
"""
30+
token = authenticate()
31+
32+
api_url = url.get_url(self.endpoint_name, dataset_id=self.dataset_id, page=self.page, page_size=self.page_size)
33+
34+
headers = {
35+
'Accept': 'application/json',
36+
'Authorization': f'Bearer {token}'
37+
}
38+
39+
response = requests.get(api_url, headers=headers)
40+
41+
if response.status_code == 200:
42+
return True, response.json(), response, api_url
43+
else:
44+
try:
45+
answer = response.json()
46+
except json.JSONDecodeError:
47+
answer = response.text
48+
return False, answer, response, api_url
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from ...call import Call
2+
from ... import url
3+
from ....authenticate import authenticate
4+
import requests
5+
import json
6+
7+
class UpdateDocument(Call):
8+
"""
9+
Implementation class for updating a document in a dataset.
10+
"""
11+
12+
def __init__(self, dataset_id, document_id, document_info):
13+
"""
14+
Creates a new UpdateDocument API call object.
15+
16+
Args:
17+
dataset_id (str): The ID of the dataset.
18+
document_id (str): The ID of the document.
19+
document_info (dict): The updated document information.
20+
"""
21+
self.dataset_id = dataset_id
22+
self.document_id = document_id
23+
self.document_info = document_info
24+
self.endpoint_name = 'update_document'
25+
26+
def execute(self):
27+
"""
28+
Performs the API call to update a document.
29+
"""
30+
token = authenticate()
31+
32+
api_url = url.get_url(self.endpoint_name, dataset_id=self.dataset_id, document_id=self.document_id)
33+
34+
headers = {
35+
'Accept': 'application/json',
36+
'Content-Type': 'application/json',
37+
'Authorization': f'Bearer {token}'
38+
}
39+
40+
response = requests.put(api_url, headers=headers, json=self.document_info)
41+
42+
if response.status_code == 200:
43+
return True, response.json(), response, api_url
44+
else:
45+
try:
46+
answer = response.json()
47+
except json.JSONDecodeError:
48+
answer = response.text
49+
return False, answer, response, api_url

tests/test_cloud_documents.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import unittest
2+
from unittest.mock import patch, Mock
3+
from ndi.cloud.api.documents import add_document, get_document, update_document, delete_document, list_dataset_documents
4+
5+
class TestCloudDocuments(unittest.TestCase):
6+
7+
@patch('ndi.cloud.api.implementation.documents.add_document.authenticate')
8+
@patch('requests.post')
9+
def test_add_document(self, mock_post, mock_authenticate):
10+
mock_authenticate.return_value = 'fake_token'
11+
mock_response = Mock()
12+
mock_response.status_code = 200
13+
mock_response.json.return_value = {'id': 'doc1'}
14+
mock_post.return_value = mock_response
15+
16+
success, answer, _, _ = add_document('ds1', {'name': 'doc'})
17+
self.assertTrue(success)
18+
self.assertEqual(answer['id'], 'doc1')
19+
20+
@patch('ndi.cloud.api.implementation.documents.get_document.authenticate')
21+
@patch('requests.get')
22+
def test_get_document(self, mock_get, mock_authenticate):
23+
mock_authenticate.return_value = 'fake_token'
24+
mock_response = Mock()
25+
mock_response.status_code = 200
26+
mock_response.json.return_value = {'id': 'doc1'}
27+
mock_get.return_value = mock_response
28+
29+
success, answer, _, _ = get_document('ds1', 'doc1')
30+
self.assertTrue(success)
31+
self.assertEqual(answer['id'], 'doc1')
32+
33+
@patch('ndi.cloud.api.implementation.documents.update_document.authenticate')
34+
@patch('requests.put')
35+
def test_update_document(self, mock_put, mock_authenticate):
36+
mock_authenticate.return_value = 'fake_token'
37+
mock_response = Mock()
38+
mock_response.status_code = 200
39+
mock_response.json.return_value = {'id': 'doc1'}
40+
mock_put.return_value = mock_response
41+
42+
success, answer, _, _ = update_document('ds1', 'doc1', {'name': 'new'})
43+
self.assertTrue(success)
44+
45+
@patch('ndi.cloud.api.implementation.documents.delete_document.authenticate')
46+
@patch('requests.delete')
47+
def test_delete_document(self, mock_delete, mock_authenticate):
48+
mock_authenticate.return_value = 'fake_token'
49+
mock_response = Mock()
50+
mock_response.status_code = 200
51+
mock_response.json.return_value = {'status': 'deleted'}
52+
mock_delete.return_value = mock_response
53+
54+
success, answer, _, _ = delete_document('ds1', 'doc1')
55+
self.assertTrue(success)
56+
57+
@patch('ndi.cloud.api.implementation.documents.list_dataset_documents.authenticate')
58+
@patch('requests.get')
59+
def test_list_dataset_documents(self, mock_get, mock_authenticate):
60+
mock_authenticate.return_value = 'fake_token'
61+
mock_response = Mock()
62+
mock_response.status_code = 200
63+
mock_response.json.return_value = [{'id': 'doc1'}]
64+
mock_get.return_value = mock_response
65+
66+
success, answer, _, _ = list_dataset_documents('ds1')
67+
self.assertTrue(success)
68+
self.assertEqual(len(answer), 1)
69+
70+
if __name__ == '__main__':
71+
unittest.main()

0 commit comments

Comments
 (0)