Skip to content

Commit

Permalink
maint: return native data types as received from the REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
katarina-vardic-4ss authored Feb 16, 2023
1 parent 7987e74 commit 7fe5e24
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 335 deletions.
139 changes: 37 additions & 102 deletions fourinsight/campaigns/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import pandas as pd

import fourinsight.campaigns as fc


Expand Down Expand Up @@ -33,7 +31,6 @@ def _dict_rename(dict_org, dict_map):
dict_new = {}

for key_org, value_org in dict_org.items():

label_mapper = dict(dict_map.keys())
try:
key_new = label_mapper[key_org.lower()]
Expand All @@ -58,86 +55,27 @@ def _dict_rename(dict_org, dict_map):
return dict_new


class JSONSpecialParse:
def __init__(self, datetime_keys=(), location_keys=(), float_keys=(), int_keys=()):
self._datetime_keys = [key.lower() for key in datetime_keys]
self._location_keys = [key.lower() for key in location_keys]

# Remove when endpoints start returning native values
self._float_keys = [key.lower() for key in float_keys]
self._int_keys = [key.lower() for key in int_keys]
def _loc_to_float(value):
"""
Attempt to cast "location" string to float. Also converts "null" to ``None``.
"""
try:
value = float(value)
except (TypeError, ValueError):
value = None if value == "null" else value
finally:
return value

def __call__(self, dct):
dct_update = {
key: pd.to_datetime(value)
for key, value in dct.items()
if key.lower() in self._datetime_keys
}
dct.update(dct_update)

dct_update = {}
for key, value in dct.items():
if key.lower() in self._location_keys:
try:
val1, val2 = value.split("#", 1)
except (AttributeError, ValueError):
dct_update[key] = value
else:
dct_update[key] = (self._float(val1), self._float(val2))
dct.update(dct_update)

# Remove when endpoints start returning native values
dct_update = {
key: None if value is None else float(value)
for key, value in dct.items()
if key.lower() in self._float_keys
}
dct.update(dct_update)

dct_update = {
key: None if value is None else int(value)
for key, value in dct.items()
if key.lower() in self._int_keys
}
dct.update(dct_update)
return dct
def _location_convert(value):
try:
val1, val2 = value.split("#", 1)
except (AttributeError, ValueError):
converted_value = value
else:
converted_value = (_loc_to_float(val1), _loc_to_float(val2))

@staticmethod
def _float(value):
"""
Attempt to cast "location" string to float. Also converts "null" to ``None``.
"""
try:
value = float(value)
except (TypeError, ValueError):
value = None if value == "null" else value
finally:
return value


json_special_hook = JSONSpecialParse(
datetime_keys=(
"start",
"stop",
"startdate",
"enddate",
"stopdate",
"attached",
"detached",
"dashboardclosedate",
),
location_keys=("location", "geolocation"),
float_keys=(
"distancefromwellhead",
"samplingrate",
"mass",
"submergedweight",
"height",
"addedmasscoefficient",
"alpha",
"waterdepth",
),
)
return converted_value


class CampaignsAPI:
Expand Down Expand Up @@ -226,7 +164,13 @@ def get_campaigns(self):
("wdtimeseriesid", "Wd Timeseries ID"): None,
}

response = self._get_payload(self._url(""), object_hook=json_special_hook)
response = self._get_payload(self._url(""))

location_keys = ["location", "geolocation"]
for campaign_item in response:
for key in campaign_item.keys():
if key.lower() in location_keys:
campaign_item[key] = _location_convert(campaign_item[key])

response_out = [
_dict_rename(campaign_item, response_map) for campaign_item in response
Expand Down Expand Up @@ -266,9 +210,12 @@ def get_campaign(self, campaign_id):
# change to v1.1 when available
response = self._get_payload_legacy(
self._url(f"/{campaign_id}", api_version="v1.0"),
object_hook=json_special_hook,
)

for key in response.keys():
if key.lower() in "location":
response[key] = _location_convert(response[key])

response_out = _dict_rename(response, response_map)
return response_out

Expand All @@ -295,8 +242,7 @@ def get_geotrack(self, campaign_id):

# change to v1.1 when available
response = self._get_payload_legacy(
self._url(f"/{campaign_id}", api_version="v1.0"),
object_hook=json_special_hook,
self._url(f"/{campaign_id}", api_version="v1.0")
)

response_out = _dict_rename(response, response_map)
Expand All @@ -323,9 +269,7 @@ def get_events(self, campaign_id):
("comment", "Comment"): None,
}

response = self._get_payload(
self._url(f"/{campaign_id}/Events"), object_hook=json_special_hook
)
response = self._get_payload(self._url(f"/{campaign_id}/Events"))

response_out = [
_dict_rename(event_item, response_map) for event_item in response
Expand Down Expand Up @@ -359,9 +303,7 @@ def _get_sensors(self, campaign_id):
("detached", "Detached Time"): None,
}

response = self._get_payload(
self._url(f"/{campaign_id}/Sensors"), object_hook=json_special_hook
)
response = self._get_payload(self._url(f"/{campaign_id}/Sensors"))

response_out = [
_dict_rename(sensor_item, response_map) for sensor_item in response
Expand Down Expand Up @@ -392,8 +334,7 @@ def _get_channels(self, campaign_id, sensor_id):
}

response = self._get_payload(
self._url(f"/{campaign_id}/Sensors/{sensor_id}/channels"),
object_hook=json_special_hook,
self._url(f"/{campaign_id}/Sensors/{sensor_id}/channels")
)

response_out = [
Expand Down Expand Up @@ -449,8 +390,7 @@ def get_lowerstack(self, campaign_id):

# change to v1.1 when available
response = self._get_payload_legacy(
self._url(f"/{campaign_id}/LowerStack", api_version="v1.0"),
object_hook=json_special_hook,
self._url(f"/{campaign_id}/LowerStack", api_version="v1.0")
)

response_out = _dict_rename(response, response_map)
Expand Down Expand Up @@ -488,9 +428,7 @@ def get_swimops_campaign(self, campaign_id):
("servicesavailable", "Services Available"): None,
}

response = self._get_payload(
self._url(f"/{campaign_id}/Swimops"), object_hook=json_special_hook
)
response = self._get_payload(self._url(f"/{campaign_id}/Swimops"))

response_out = _dict_rename(response[0], response_map)
return response_out
Expand Down Expand Up @@ -522,9 +460,7 @@ def get_swimops(self):
("servicesavailable", "Services Available"): None,
}

response = self._get_payload(
self._url("/Swimops"), object_hook=json_special_hook
)
response = self._get_payload(self._url("/Swimops"))
response_out = [
_dict_rename(swim_ops_item, response_map) for swim_ops_item in response
]
Expand All @@ -549,8 +485,7 @@ def get_campaign_type(self, campaign_id):
}
# change to v1.1 when available
response = self._get_payload_legacy(
self._url(f"/{campaign_id}", api_version="v1.0"),
object_hook=json_special_hook,
self._url(f"/{campaign_id}", api_version="v1.0")
)
response = _dict_rename(response, response_map)
return response["CampaignType"].lower()
Loading

0 comments on commit 7fe5e24

Please sign in to comment.