Skip to content

Commit

Permalink
fixed start and end param
Browse files Browse the repository at this point in the history
  • Loading branch information
paulokuong committed Feb 1, 2019
1 parent 8345bf7 commit a0c563d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
71 changes: 43 additions & 28 deletions noaa_sdk/noaa.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ def stations_observations(self, station_id, **params):
Response in this method should not be modified.
In this way, we can keep track of changes made by NOAA through
functional tests @todo(paulokuong) later on.
(*Note: There is a delay on NOAA's side for "unpopular" stations which
causes start and end params not enable to query anything sometimes.)
Args:
station_id (str): station id.
Expand All @@ -293,43 +295,56 @@ def stations_observations(self, station_id, **params):
Returns:
json: json response from api.
"""

if not station_id:
raise Exception("'station_id' is required.")
if 'recordId' in params and 'current' in params:
raise Exception("Cannot have both 'current' and 'recordId'")
if 'start' in params:
start = params['start']
self.parse_param_timestamp(start)
if len(start) < 19:
start = '{}T00:00:00Z'.format(start[:10])
elif len(params['start']) < 20:
start = start.replace(' ', 'T')
start = '{}Z'.format(start)
params['start'] = start
if 'end' in params:
end = params['end']
self.parse_param_timestamp(end)
if len(end) < 19:
end = '{}T23:59:59Z'.format(end[:10])
elif len(params['end']) < 20:
end = end.replace(' ', 'T')
end = '{}Z'.format(end)
params['end'] = end

request_uri = "/stations/{stationId}/observations".format(
stationId=station_id)

if len(params) > 0:
if 'recordId' in params:
return self.make_get_request(
'/stations/{stationId}/observations/{recordId}'.format(
stationId=station_id, recordId=params['recordId']),
'{old_request_uri}/{recordId}'.format(
old_request_uri=request_uri,
recordId=params['recordId']),
end_point=self.DEFAULT_END_POINT)
if 'current' in params:
return self.make_get_request(
'/stations/{stationId}/observations/current'.format(
stationId=station_id),
'{old_request_uri}/current'.format(
old_request_uri=request_uri),
end_point=self.DEFAULT_END_POINT)

observations = self.make_get_request(
"/stations/{stationId}/observations".format(
stationId=station_id),
end_point=self.DEFAULT_END_POINT)
if len(params) > 1:
request_uri = '{old_request_uri}?{query_string}'.format(
old_request_uri=request_uri,
query_string=urlencode(params))

observations = observations['features']

# There is an issue on NOAA's side which causes start and end params
# not enable to filter anything (return empty results). So,
# for now, there is no choice but to fetch everything and filter
# the data below.

if 'start' in params:
observations = [
i for i in observations
if self.parse_response_timestamp(
i['properties']['timestamp']) >= self.parse_param_timestamp(
params['start'])]
if 'end' in params:
observations = [
i for i in observations
if self.parse_response_timestamp(
i['properties']['timestamp']) <= self.parse_param_timestamp(
params['end'])]
return observations
observations = self.make_get_request(
request_uri, end_point=self.DEFAULT_END_POINT)
if 'features' not in observations:
raise Exception(observations)
return observations['features']

return self.make_get_request(
"/stations/{stationId}/observations".format(stationId=station_id),
Expand Down
2 changes: 1 addition & 1 deletion noaa_sdk/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def wrapper(*args, **kargs):
fib_num_b = 1

while status_code == '' or (retry <= max_retries and (
status_code == '' or status_code == 500)):
status_code == '' or status_code != 200)):
response = request(*args, **kargs)
status_code = response.status_code
new_interval = fib_num_b + fib_num_a
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
long_description = file.read()

setup(name='noaa-sdk',
version='0.1.16',
version='0.1.17',
description='NOAA API (V3) Python 3 SDK.',
install_requires=[
'httplib2==0.10.3',
Expand Down

0 comments on commit a0c563d

Please sign in to comment.