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

Parameter wrapper #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
116 changes: 116 additions & 0 deletions pygeostreams/parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import logging
import time
from typing import Union, Tuple
from pygeostreams.client import GeostreamsClient


class ParametersAPI(object):
"""
API to manage the REST CRUD endpoints for Parameters.
"""

def __init__(self, client=None, host=None, username=None, password=None):
"""Set client if provided otherwise create new one"""
if client:
self.api_client = client
else:
self.client = GeostreamsClient(host=host, username=username, password=password)

def parameters_get(self, timeout: Union[int, Tuple[int, int]] = (125, 605)):
"""
Get the list of all available parameters.

:param timeout: Number of seconds Requests will wait to establish a connection.
Specify a Tuple if connect and read timeouts should be different (with the first element being
the connection timeout, and the second being the read timeout.
:return: Full list of parameters.
:rtype: `requests.Response`
"""
logging.debug("Getting all parameters")
try:
return self.client.get("/parameters", timeout)
except RequestException as e:
logging.error(f"Error retrieving parameter list: {e}")
raise e

def parameter_create_json(self, parameter_name, parameter_title, parameter_unit, categories, search_view=True, explore_view=True,
scale_names=None, scale_colors=None):
"""
Create a json definition for parameters

:param categories: List of categories in format [[category_name,category_detail_type]...]
:return: return JSON that could be used in function parameter_post

"""
data = {
"parameter":
{
"name": parameter_name,
"title": parameter_title,
"unit": parameter_unit,
"search_view": search_view,
"explore_view": explore_view,
"scale_names": scale_names,
"scale_colors": scale_colors
},
"categories": []
}
for category in categories:
data["categories"].append({"name": category[0], "detail_type": category[1]})

return data

def parameter_post(self, parameter, timeout: Union[int, Tuple[int, int]] = (125, 605)):
"""
Create parameter.

:param parameter: parameter json
:param timeout: Number of seconds Requests will wait to establish a connection.
Specify a Tuple if connect and read timeouts should be different (with the first element being
the connection timeout, and the second being the read timeout.
:return: parameter json.
:rtype: `requests.Response`
"""
logging.debug("Adding parameter")
try:
return self.client.post("/parameters", parameter, timeout)
except RequestException as e:
logging.error(f"Error adding parameter {parameter['name']}: {e}")
raise e

def parameter_delete(self, parameter_id, timeout: Union[int, Tuple[int, int]] = (125, 605)):
"""
Delete a specific parameter by id.

:param parameter_id:
:param timeout: Number of seconds Requests will wait to establish a connection.
Specify a Tuple if connect and read timeouts should be different (with the first element being
the connection timeout, and the second being the read timeout.
:return: If successful or not.
:rtype: `requests.Response`
"""
logging.debug(f"Deleting parameter {parameter_id}")
try:
return self.client.delete("/parameters/%s" % parameter_id, timeout)
except RequestException as e:
logging.error(f"Error deleting parameter {parameter_id}: {e}")
raise e

def parameter_delete_by_name(self, parameter_name, timeout: Union[int, Tuple[int, int]] = (125, 605)):
"""
Delete a specific parameter by name.

:param parameter_name:
:param timeout: Number of seconds Requests will wait to establish a connection.
Specify a Tuple if connect and read timeouts should be different (with the first element being
the connection timeout, and the second being the read timeout.
:return: If successful or not.
:rtype: `requests.Response`
"""

logging.debug(f"Deleting parameter named {parameter_name}")
try:
return self.client.delete("/parameters/name/%s" % parameter_name, timeout)
except RequestException as e:
logging.error(f"Error deleting parameter named {parameter_name}: {e}")
raise e
8 changes: 4 additions & 4 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

from requests.exceptions import ConnectionError

from pygeotemporal.client import GeostreamsClient
from pygeotemporal.datapoints import DatapointsApi
from pygeotemporal.sensors import SensorsApi
from pygeotemporal.streams import StreamsApi
from pygeostreams.client import GeostreamsClient
from pygeostreams.datapoints import DatapointsApi
from pygeostreams.sensors import SensorsApi
from pygeostreams.streams import StreamsApi


@pytest.fixture
Expand Down
6 changes: 3 additions & 3 deletions tests/test_datapoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import pytest

from pygeotemporal.sensors import SensorsApi
from pygeotemporal.streams import StreamsApi
from pygeotemporal.datapoints import DatapointsApi
from pygeostreams.sensors import SensorsApi
from pygeostreams.streams import StreamsApi
from pygeostreams.datapoints import DatapointsApi

sensor_id = ""
stream_id = ""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_geostreams.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import pytest
from requests import HTTPError

from pygeotemporal.client import GeostreamsClient
from pygeotemporal.sensors import SensorsApi
from pygeostreams.client import GeostreamsClient
from pygeostreams.sensors import SensorsApi


def test_get_sensors(caplog, host, username, password):
Expand Down
8 changes: 3 additions & 5 deletions tests/test_sensors.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import logging

from pygeotemporal.sensors import SensorsApi
from pygeostreams.sensors import SensorsApi
import pytest
import json


# testing post, get, and delete here so we don't populate db with test sensors
def test_sensor_endpoint(caplog, host, username, password):
caplog.setLevel(logging.DEBUG)
def test_sensor_endpoint( host, username, password):
client = SensorsApi(host=host, username=username, password=password)

# test create sensor
Expand Down Expand Up @@ -36,8 +35,7 @@ def test_sensor_endpoint(caplog, host, username, password):


@pytest.mark.skip(reason="need to manually set the sensor id")
def test_delete_sensor(caplog, host, username, password):
caplog.setLevel(logging.DEBUG)
def test_delete_sensor( host, username, password):
client = SensorsApi(host=host, username=username, password=password)

sensor_name = "USGS-07029150"
Expand Down
4 changes: 2 additions & 2 deletions tests/test_streams.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

from pygeotemporal.streams import StreamsApi
from pygeotemporal.sensors import SensorsApi
from pygeostreams.streams import StreamsApi
from pygeostreams.sensors import SensorsApi

sensor_id = ""
stream_id = ""
Expand Down