Skip to content

Commit

Permalink
add decorator to data services
Browse files Browse the repository at this point in the history
  • Loading branch information
longshuicy committed Jan 10, 2024
1 parent d409498 commit bdd1944
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion pyincore/dataservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import pyincore.globals as pyglobals
from pyincore import IncoreClient
from pyincore.decorators import forbid_offline
from pyincore.utils import return_http_response
from urllib.parse import urljoin
import requests
Expand All @@ -36,7 +37,7 @@ def __init__(self, client: IncoreClient):
self.base_earthquake_url = urljoin(client.service_url, 'hazard/api/earthquakes/')
self.base_tornado_url = urljoin(client.service_url, 'hazard/api/tornadoes/')


@forbid_offline
def get_dataset_metadata(self, dataset_id: str, timeout=(30, 600), **kwargs):
"""Retrieve metadata from data service. Dataset API endpoint is called.
Expand All @@ -53,6 +54,7 @@ def get_dataset_metadata(self, dataset_id: str, timeout=(30, 600), **kwargs):
r = self.client.get(url, timeout=timeout, **kwargs)
return return_http_response(r).json()

@forbid_offline
def get_dataset_files_metadata(self, dataset_id: str, timeout=(30, 600), **kwargs):
"""Retrieve metadata of all files associated with the dataset. Files API endpoint is called.
Expand All @@ -69,6 +71,7 @@ def get_dataset_files_metadata(self, dataset_id: str, timeout=(30, 600), **kwarg
r = self.client.get(url, timeout=timeout, **kwargs)
return return_http_response(r).json()

@forbid_offline
def get_dataset_file_metadata(self, dataset_id: str, file_id: str, timeout=(30, 600), **kwargs):
"""Retrieve metadata of all files associated with the dataset. Files API endpoint is called.
Expand All @@ -87,6 +90,7 @@ def get_dataset_file_metadata(self, dataset_id: str, file_id: str, timeout=(30,
r = self.client.get(url, timeout=timeout, **kwargs)
return return_http_response(r).json()

@forbid_offline
def get_dataset_blob(self, dataset_id: str, join=None, timeout=(30, 600), **kwargs):
"""Retrieve a blob of the dataset. Blob API endpoint is called.
Expand Down Expand Up @@ -128,6 +132,7 @@ def get_dataset_blob(self, dataset_id: str, join=None, timeout=(30, 600), **kwar
else:
return local_filename

@forbid_offline
def download_dataset_blob(self, cache_data_dir: str, dataset_id: str, join=None, timeout=(30, 600), **kwargs):
# construct url for file download
url = urljoin(self.base_url, dataset_id + '/blob')
Expand Down Expand Up @@ -155,6 +160,7 @@ def download_dataset_blob(self, cache_data_dir: str, dataset_id: str, join=None,

return local_filename

@forbid_offline
def get_datasets(self, datatype: str = None, title: str = None, creator: str = None, skip: int = None,
limit: int = None, space: str = None, timeout=(30, 600), **kwargs):
"""Function to get datasets. Blob API endpoint is called.
Expand Down Expand Up @@ -193,6 +199,7 @@ def get_datasets(self, datatype: str = None, title: str = None, creator: str = N
# need to handle there is no datasets
return return_http_response(r).json()

@forbid_offline
def create_dataset(self, properties: dict, timeout=(30, 600), **kwargs):
"""Create datasets. Post API endpoint is called.
Expand All @@ -210,6 +217,7 @@ def create_dataset(self, properties: dict, timeout=(30, 600), **kwargs):
r = self.client.post(url, timeout=timeout, **kwargs)
return return_http_response(r).json()

@forbid_offline
def update_dataset(self, dataset_id, property_name: str,
property_value: str, timeout=(30, 600), **kwargs):
"""Update dataset. Put API endpoint is called.
Expand All @@ -232,6 +240,7 @@ def update_dataset(self, dataset_id, property_name: str,
r = self.client.put(url, timeout=timeout, **kwargs)
return return_http_response(r).json()

@forbid_offline
def add_files_to_dataset(self, dataset_id: str, filepaths: list, timeout=(30, 600), **kwargs):
"""Add files to the dataset. Post API endpoint is called.
Expand Down Expand Up @@ -260,6 +269,7 @@ def add_files_to_dataset(self, dataset_id: str, filepaths: list, timeout=(30, 60

return return_http_response(r).json()

@forbid_offline
def add_files_to_network_dataset(self, dataset_id: str, filepaths: list,
nodename: str, linkname: str, graphname: str, timeout=(30, 600), **kwargs):
"""Add files to the network dataset. Post API endpoint is called.
Expand Down Expand Up @@ -303,6 +313,7 @@ def add_files_to_network_dataset(self, dataset_id: str, filepaths: list,

return return_http_response(r).json()

@forbid_offline
def delete_dataset(self, dataset_id: str, timeout=(30, 600), **kwargs):
"""Delete dataset. Delete API endpoint is called.
Expand All @@ -319,6 +330,7 @@ def delete_dataset(self, dataset_id: str, timeout=(30, 600), **kwargs):
r = self.client.delete(url, timeout=timeout, **kwargs)
return return_http_response(r).json()

@forbid_offline
def get_files(self, timeout=(30, 600), **kwargs):
"""Get all files. Files API endpoint is called.
Args:
Expand All @@ -334,6 +346,7 @@ def get_files(self, timeout=(30, 600), **kwargs):
r = self.client.get(url, timeout=timeout, **kwargs)
return return_http_response(r).json()

@forbid_offline
def get_file_metadata(self, file_id: str, timeout=(30, 600), **kwargs):
"""Function to retrieve metadata of a file defined by id. Files API endpoint is called.
Expand All @@ -350,6 +363,7 @@ def get_file_metadata(self, file_id: str, timeout=(30, 600), **kwargs):
r = self.client.get(url, timeout=timeout, **kwargs)
return return_http_response(r).json()

@forbid_offline
def get_file_blob(self, file_id: str, timeout=(30, 600), **kwargs):
"""Function to retrieve a blob of the file. Blob API endpoint is called.
Expand Down Expand Up @@ -412,6 +426,7 @@ def unzip_dataset(self, local_filename: str):
zip_ref.close()
return foldername

@forbid_offline
def get_shpfile_from_service(self, fileid, dirname, timeout=(30, 600), **kwargs):
"""Function to obtain a shape file from Data service.
Expand Down Expand Up @@ -443,6 +458,7 @@ def get_shpfile_from_service(self, fileid, dirname, timeout=(30, 600), **kwargs)

return filename

@forbid_offline
def get_tornado_dataset_id_from_service(self, fileid, timeout=(30, 600), **kwargs):
"""Function to obtain a tornado dataset Id from Data service.
Expand All @@ -461,6 +477,7 @@ def get_tornado_dataset_id_from_service(self, fileid, timeout=(30, 600), **kwarg

return return_http_response(r).json()["tornadoDatasetId"]

@forbid_offline
def search_datasets(self, text: str, skip: int = None, limit: int = None, timeout=(30, 600), **kwargs):
"""Function to search datasets.
Expand Down

0 comments on commit bdd1944

Please sign in to comment.