Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate rest_api submodule #100

Merged
merged 20 commits into from
May 23, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions datareservoirio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import logging

import pkg_resources

from . import globalsettings # wierd bug. must be called last?
from .authenticate import UserAuthenticator as Authenticator
from .client import Client
Expand Down
132 changes: 92 additions & 40 deletions datareservoirio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from concurrent.futures import ThreadPoolExecutor
from functools import wraps
from operator import itemgetter
from uuid import uuid4

import pandas as pd
import requests
from opencensus.ext.azure.log_exporter import AzureLogHandler

from .globalsettings import environment
from .rest_api import FilesAPI, MetadataAPI, TimeSeriesAPI
from .storage import Storage

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -48,9 +48,6 @@ class Client:

def __init__(self, auth, cache=True, cache_opt=None):
self._auth_session = auth
self._timeseries_api = TimeSeriesAPI(self._auth_session, cache=cache)
self._files_api = FilesAPI(self._auth_session)
self._metadata_api = MetadataAPI(self._auth_session)

# TODO: Remove after 2023-08-15
if cache:
Expand All @@ -71,7 +68,9 @@ def ping(self):
Test that you have a working connection to DataReservoir.io.

"""
return self._files_api.ping()
response = self._auth_session.get(environment.api_base_url + "ping")
response.raise_for_status()
return response.json()

def create(self, series=None, wait_on_verification=True):
"""
Expand Down Expand Up @@ -101,8 +100,11 @@ def create(self, series=None, wait_on_verification=True):
newly created series.
"""
if series is None:
response = self._timeseries_api.create()
return response
response = self._auth_session.put(
environment.api_base_url + f"timeseries/{str(uuid4())}"
)
response.raise_for_status()
return response.json()

df = self._verify_and_prepare_series(series)

Expand All @@ -124,8 +126,11 @@ def create(self, series=None, wait_on_verification=True):
if status == "Failed":
return status

response = self._timeseries_api.create_with_data(file_id)
return response
response = self._auth_session.post(
environment.api_base_url + "timeseries/create", data={"FileId": file_id}
)
response.raise_for_status()
return response.json()

def append(self, series, series_id, wait_on_verification=True):
"""
Expand Down Expand Up @@ -174,8 +179,12 @@ def append(self, series, series_id, wait_on_verification=True):
if status == "Failed":
return status

response = self._timeseries_api.add(series_id, file_id)
return response
response = self._auth_session.post(
environment.api_base_url + "timeseries/add",
data={"TimeSeriesId": series_id, "FileId": file_id},
)
response.raise_for_status()
return response.json()

def info(self, series_id):
"""
Expand All @@ -186,7 +195,11 @@ def info(self, series_id):
dict
Available information about the series. None if series not found.
"""
return self._timeseries_api.info(series_id)
response = self._auth_session.get(
environment.api_base_url + f"timeseries/{series_id}"
)
response.raise_for_status()
return response.json()

def search(self, namespace, key=None, name=None, value=None):
"""
Expand Down Expand Up @@ -220,15 +233,20 @@ def search(self, namespace, key=None, name=None, value=None):
returned -> ``{TimeSeriesId: metadata}``.

"""
args_ = [namespace, key, name, value]
none_count = args_.count(None)

if args_[-none_count:].count(None) < none_count:
warnings.warn(
"Warning: You have provided argument(s) following a None argument, they are ignored by the search!"
)
args = [namespace, key, name, value]
if None in args:
none_count = args.count(None)
if args[-none_count:].count(None) < none_count:
warnings.warn(
"Warning: You have provided argument(s) following a None argument, they are ignored by the search!"
)
args = args[: args.index(None)]

return self._timeseries_api.search(namespace, key, name, value)
response = self._auth_session.get(
environment.api_base_url + f"timeseries/search/{'/'.join(args)}"
)
response.raise_for_status()
return response.json()

def delete(self, series_id):
"""
Expand All @@ -240,7 +258,9 @@ def delete(self, series_id):
The id of the series to delete.

"""
return self._timeseries_api.delete(series_id)
return self._auth_session.delete(
environment.api_base_url + f"timeseries/{series_id}"
)

def _timer(func):
"""Decorator used to log latency of the ``get`` method"""
Expand Down Expand Up @@ -393,10 +413,13 @@ def set_metadata(
if not key:
raise ValueError("key is mandatory when namespace is passed")
try:
response_create = self._metadata_api.put(
namespace, key, overwrite, **namevalues
response_create = self._auth_session.put(
environment.api_base_url
+ f"metadata/{namespace}/{key}?overwrite={'true' if overwrite else 'false'}",
json={"Value": namevalues},
)
metadata_id = response_create["Id"]
response_create.raise_for_status()
metadata_id = response_create.json()["Id"]
except requests.exceptions.HTTPError as err:
if err.response.status_code == 409:
raise ValueError(
Expand All @@ -406,8 +429,12 @@ def set_metadata(
else:
raise

response = self._timeseries_api.attach_metadata(series_id, [metadata_id])
return response
response = self._auth_session.put(
environment.api_base_url + f"timeseries/{series_id}/metadata",
json=[metadata_id],
)
response.raise_for_status()
return response.json()

def remove_metadata(self, series_id, metadata_id):
"""
Expand All @@ -425,9 +452,14 @@ def remove_metadata(self, series_id, metadata_id):
------
dict
response.json()

"""
response = self._timeseries_api.detach_metadata(series_id, [metadata_id])
return response
response = self._auth_session.delete(
environment.api_base_url + f"timeseries/{series_id}/metadata",
json=[metadata_id],
)
response.raise_for_status()
return response.json()

def metadata_set(self, namespace, key, **namevalues):
"""
Expand All @@ -450,8 +482,12 @@ def metadata_set(self, namespace, key, **namevalues):
The response from DataReservoir.io containing the unique id of the
new or updated metadata.
"""
response = self._metadata_api.put(namespace, key, True, **namevalues)
return response
response = self._auth_session.put(
environment.api_base_url + f"metadata/{namespace}/{key}?overwrite=true",
json={"Value": namevalues},
)
response.raise_for_status()
return response.json()

def metadata_get(self, metadata_id=None, namespace=None, key=None):
"""
Expand All @@ -473,15 +509,17 @@ def metadata_get(self, metadata_id=None, namespace=None, key=None):
Metadata entry.
"""
if metadata_id:
response = self._metadata_api.get_by_id(metadata_id)
uri_postfix = f"metadata/{metadata_id}"
elif namespace and key:
response = self._metadata_api.get(namespace, key)
uri_postfix = f"metadata/{namespace}/{key}"
else:
raise ValueError(
"Missing required input: either (metadata_id) or (namespace, key)"
)

return response
response = self._auth_session.get(environment.api_base_url + uri_postfix)
response.raise_for_status()
return response.json()

def metadata_browse(self, namespace=None):
"""
Expand All @@ -501,9 +539,13 @@ def metadata_browse(self, namespace=None):
"""

if not namespace:
return self._metadata_api.namespaces()
uri_postfix = "metadata/"
else:
return self._metadata_api.keys(namespace)
uri_postfix = f"metadata/{namespace}"

response = self._auth_session.get(environment.api_base_url + uri_postfix)
response.raise_for_status()
return sorted(response.json())

def metadata_search(self, namespace, key):
"""
Expand All @@ -521,8 +563,12 @@ def metadata_search(self, namespace, key):
Metadata entries that matches the search.

"""
response = self._metadata_api.search(namespace, key)
return response
response = self._auth_session.post(
environment.api_base_url + "metadata/search",
json={"Namespace": namespace, "Key": key, "Value": {}},
)
response.raise_for_status()
return response.json()

def metadata_delete(self, metadata_id):
"""
Expand All @@ -533,7 +579,10 @@ def metadata_delete(self, metadata_id):
metadata_id : str
id of metadata
"""
self._metadata_api.delete(metadata_id)
response = self._auth_session.delete(
environment.api_base_url + f"metadata/{metadata_id}"
)
response.raise_for_status()
return

def _verify_and_prepare_series(self, series):
Expand Down Expand Up @@ -568,8 +617,11 @@ def _wait_until_file_ready(self, file_id):
time.sleep(5)

def _get_file_status(self, file_id):
response = self._files_api.status(file_id)
return response["State"]
response = self._auth_session.get(
environment.api_base_url + f"files/{file_id}/status"
)
response.raise_for_status()
return response.json()["State"]


def _blob_sequence_days(response_json):
Expand Down
3 changes: 0 additions & 3 deletions datareservoirio/rest_api/__init__.py

This file was deleted.

87 changes: 0 additions & 87 deletions datareservoirio/rest_api/base.py

This file was deleted.