Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/ndi/cloud/api/datasets/get_dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from ..implementation.datasets.get_dataset import GetDataset as GetDatasetImpl

def get_dataset(dataset_id):
"""
User-facing wrapper to get dataset details.

Args:
dataset_id (str): The ID of the dataset.

Returns:
tuple: (success, answer, response, url)
"""
api_call = GetDatasetImpl(dataset_id)
return api_call.execute()
30 changes: 30 additions & 0 deletions src/ndi/cloud/api/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .implementation.documents.update_document import UpdateDocument as UpdateDocumentImpl
from .implementation.documents.delete_document import DeleteDocument as DeleteDocumentImpl
from .implementation.documents.list_dataset_documents import ListDatasetDocuments as ListDatasetDocumentsImpl
from .implementation.documents.list_dataset_documents_all import ListDatasetDocumentsAll as ListDatasetDocumentsAllImpl
from .implementation.documents.get_bulk_download_url import GetBulkDownloadURL as GetBulkDownloadURLImpl

def add_document(dataset_id, document_info):
"""
Expand Down Expand Up @@ -75,3 +77,31 @@ def list_dataset_documents(dataset_id, page=1, page_size=20):
"""
api_call = ListDatasetDocumentsImpl(dataset_id, page, page_size)
return api_call.execute()

def list_dataset_documents_all(dataset_id, page_size=20):
"""
Lists all documents in a dataset.

Args:
dataset_id (str): The ID of the dataset.
page_size (int, optional): The number of documents per page. Defaults to 20.

Returns:
tuple: (success, answer, response, url)
"""
api_call = ListDatasetDocumentsAllImpl(dataset_id, page_size)
return api_call.execute()

def get_bulk_download_url(dataset_id, document_ids=None):
"""
Retrieves a pre-signed URL for bulk document download.

Args:
dataset_id (str): The ID of the dataset.
document_ids (list of str, optional): List of cloud document IDs to download.

Returns:
tuple: (success, answer, response, url)
"""
api_call = GetBulkDownloadURLImpl(dataset_id, document_ids)
return api_call.execute()
Empty file.
15 changes: 15 additions & 0 deletions src/ndi/cloud/api/files/get_file_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from ..implementation.files.get_file_details import GetFileDetails as GetFileDetailsImpl

def get_file_details(dataset_id, file_uid):
"""
User-facing wrapper to get file details.

Args:
dataset_id (str): The ID of the dataset.
file_uid (str): The UID of the file.

Returns:
tuple: (success, answer, response, url)
"""
api_call = GetFileDetailsImpl(dataset_id, file_uid)
return api_call.execute()
43 changes: 43 additions & 0 deletions src/ndi/cloud/api/implementation/datasets/get_dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from ...call import Call
from ... import url
from ....authenticate import authenticate
import requests
import json

class GetDataset(Call):
"""
Implementation class for getting dataset details.
"""

def __init__(self, dataset_id):
"""
Creates a new GetDataset API call object.

Args:
dataset_id (str): The ID of the dataset.
"""
self.dataset_id = dataset_id
self.endpoint_name = 'get_dataset'

def execute(self):
"""
Performs the API call.
"""
token = authenticate()
api_url = url.get_url(self.endpoint_name, dataset_id=self.dataset_id)

headers = {
'Accept': 'application/json',
'Authorization': f'Bearer {token}'
}

response = requests.get(api_url, headers=headers)

if response.status_code == 200:
return True, response.json(), response, api_url
else:
try:
answer = response.json()
except json.JSONDecodeError:
answer = response.text
return False, answer, response, api_url
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from ...call import Call
from ... import url
from ....authenticate import authenticate
import requests
import json

class GetBulkDownloadURL(Call):
"""
Implementation class for getting a bulk download URL.
"""

def __init__(self, dataset_id, document_ids=None):
"""
Creates a new GetBulkDownloadURL API call object.

Args:
dataset_id (str): The ID of the dataset.
document_ids (list of str, optional): List of cloud document IDs to download.
If None or empty, all documents are included.
"""
self.dataset_id = dataset_id
self.document_ids = document_ids if document_ids is not None else []
self.endpoint_name = 'bulk_download_documents'

def execute(self):
"""
Performs the API call.
"""
token = authenticate()
api_url = url.get_url(self.endpoint_name, dataset_id=self.dataset_id)

headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
}

# The body specifies which document IDs to include
data = {'documentIds': self.document_ids}

response = requests.post(api_url, headers=headers, json=data)

if response.status_code in [200, 201]:
try:
answer = response.json().get('url')
return True, answer, response, api_url
except json.JSONDecodeError:
return False, response.text, response, api_url
else:
try:
answer = response.json()
except json.JSONDecodeError:
answer = response.text
return False, answer, response, api_url
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from ...call import Call
from .list_dataset_documents import ListDatasetDocuments

class ListDatasetDocumentsAll(Call):
"""
Implementation class for listing all documents in a dataset.
"""

def __init__(self, dataset_id, page_size=20):
"""
Creates a new ListDatasetDocumentsAll API call object.

Args:
dataset_id (str): The ID of the dataset.
page_size (int, optional): The number of documents per page. Defaults to 20.
"""
self.dataset_id = dataset_id
self.page_size = page_size
self.endpoint_name = 'list_dataset_documents'

def execute(self):
"""
Performs the API call to list all documents.
"""
all_documents = []
page = 1
last_response = None
last_url = None

while True:
api_call = ListDatasetDocuments(self.dataset_id, page=page, page_size=self.page_size)
success, answer, response, url = api_call.execute()

last_response = response
last_url = url

if not success:
return False, answer, response, url

if not isinstance(answer, list):
return False, "Unexpected response format: expected a list", response, url

all_documents.extend(answer)

if len(answer) < self.page_size:
break

page += 1

return True, all_documents, last_response, last_url
45 changes: 45 additions & 0 deletions src/ndi/cloud/api/implementation/files/get_file_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from ...call import Call
from ... import url
from ....authenticate import authenticate
import requests
import json

class GetFileDetails(Call):
"""
Implementation class for getting file details.
"""

def __init__(self, dataset_id, file_uid):
"""
Creates a new GetFileDetails API call object.

Args:
dataset_id (str): The ID of the dataset.
file_uid (str): The UID of the file.
"""
self.dataset_id = dataset_id
self.file_uid = file_uid
self.endpoint_name = 'get_file_details'

def execute(self):
"""
Performs the API call.
"""
token = authenticate()
api_url = url.get_url(self.endpoint_name, dataset_id=self.dataset_id, file_uid=self.file_uid)

headers = {
'Accept': 'application/json',
'Authorization': f'Bearer {token}'
}

response = requests.get(api_url, headers=headers)

if response.status_code == 200:
return True, response.json(), response, api_url
else:
try:
answer = response.json()
except json.JSONDecodeError:
answer = response.text
return False, answer, response, api_url
1 change: 1 addition & 0 deletions src/ndi/cloud/download/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .download_utils import download_document_collection, download_dataset_files
Loading