Skip to content
This repository was archived by the owner on Sep 5, 2023. It is now read-only.

Commit 79d9bdd

Browse files
fixes
1 parent d9335bf commit 79d9bdd

File tree

4 files changed

+44
-52
lines changed

4 files changed

+44
-52
lines changed

here_location_services/destination_weather_api.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
""" # noqa E501
66

77
import time
8-
from datetime import datetime
9-
from typing import Any, Dict, List, Optional
8+
from datetime import date, datetime
9+
from typing import Any, Dict, List, Optional, Union
10+
11+
from geojson import Feature, FeatureCollection, LineString, MultiPolygon, Point, Polygon
1012

1113
from here_location_services.platform.auth import Auth
1214

@@ -33,7 +35,7 @@ def get_dest_weather(
3335
at: Optional[List] = None,
3436
query: Optional[str] = None,
3537
zipcode: Optional[str] = None,
36-
hourly_date: Optional[Any] = None,
38+
hourly_date: Optional[Union[date, datetime]] = None,
3739
one_observation: Optional[bool] = None,
3840
language: Optional[str] = None,
3941
units: Optional[str] = None,
@@ -49,7 +51,8 @@ def get_dest_weather(
4951
:param query: Free text query. Examples: "125, Berliner, berlin", "Beacon, Boston"
5052
:param zipcode: ZIP code of the location. This parameter is supported only for locations in
5153
the United States of America.
52-
:param hourly_date: Date for which hourly forecasts are to be retrieved.
54+
:param hourly_date: Date for which hourly forecasts are to be retrieved. Can be either a `date` or
55+
`datetime` object
5356
:param one_observation: Boolean, if set to true, the response only includes the closest
5457
location. Only available when the `product` parameter is set to
5558
`DEST_WEATHER_PRODUCT.observation`.
@@ -71,7 +74,10 @@ def get_dest_weather(
7174
if zipcode:
7275
params["zipCode"] = zipcode
7376
if hourly_date:
74-
params["hourlyDate"] = hourly_date.strftime("%Y-%m-%dT%H:%M:%S")
77+
if type(hourly_date) is datetime.date:
78+
params["hourlyDate"] = hourly_date.strftime("%Y-%m-%d")
79+
else:
80+
params["hourlyDate"] = hourly_date.strftime("%Y-%m-%dT%H:%M:%S")
7581
if one_observation:
7682
params["oneObservation"] = "true" if one_observation else "false"
7783
if language:
@@ -87,9 +93,7 @@ def get_dest_weather(
8793

8894
def get_weather_alerts(
8995
self,
90-
feature_type: str,
91-
geometry_type: str,
92-
geometry_coordinates: List,
96+
geometry: Union[Point, LineString, Polygon, MultiPolygon],
9397
start_time: datetime,
9498
id: Optional[str] = None,
9599
weather_severity: Optional[int] = None,
@@ -102,9 +106,8 @@ def get_weather_alerts(
102106
103107
See further information `Here Destination Weather API <https://developer.here.com/documentation/destination-weather/dev_guide/topics/overview.html>_`.
104108
105-
:param feature_type: String to define feature type
106-
:param geometry_type: Point or LineString or Polygon or MultiPolygon
107-
:param geometry_coordinates: Array of coordinates corressponding to type provided
109+
:param geometry: Point or LineString or Polygon or MultiPolygon defining the route or
110+
a single location
108111
:param start_time: Start time of the event
109112
:param id: Unique weather alert id.
110113
:param weather_severity: Defines the severity of the weather event as defined
@@ -122,17 +125,6 @@ def get_weather_alerts(
122125

123126
path = "v3/alerts"
124127
url = f"{self._base_url}/{path}"
125-
data: Dict[str, Any] = {
126-
"type": "FeatureCollection",
127-
}
128-
features: Dict[str, Any] = {
129-
"type": feature_type,
130-
}
131-
132-
if id:
133-
features["id"] = id
134-
135-
geometry: Dict[str, Any] = {"type": geometry_type, "coordinates": geometry_coordinates}
136128

137129
properties: Dict[str, Any] = {
138130
"width": width,
@@ -151,10 +143,19 @@ def get_weather_alerts(
151143
weather_warnings["endTime"] = time.mktime(end_time.timetuple())
152144

153145
properties["warnings"] = [weather_warnings]
154-
features["properties"] = properties
155-
features["geometry"] = geometry
156-
data["features"] = [features]
157-
resp = self.post(url, data=data)
146+
147+
f = Feature(
148+
geometry=geometry,
149+
properties=properties,
150+
)
151+
152+
if id:
153+
f.id = id
154+
155+
feature_collection = FeatureCollection([])
156+
feature_collection.features.append(f)
157+
158+
resp = self.post(url, data=feature_collection)
158159
if resp.status_code == 200:
159160
return resp
160161
else:

here_location_services/ls.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import os
77
import urllib
88
import urllib.request
9-
from datetime import datetime
9+
from datetime import date, datetime
1010
from time import sleep
11-
from typing import Any, Dict, List, Optional, Tuple, Union
11+
from typing import Dict, List, Optional, Tuple, Union
12+
13+
from geojson import LineString, MultiPolygon, Point, Polygon
1214

1315
from here_location_services.config.routing_config import Scooter, Via
1416
from here_location_services.platform.apis.aaa_oauth2_api import AAAOauth2Api
@@ -320,7 +322,7 @@ def get_dest_weather(
320322
at: Optional[List] = None,
321323
query: Optional[str] = None,
322324
zipcode: Optional[str] = None,
323-
hourly_date: Optional[Any] = None,
325+
hourly_date: Optional[Union[date, datetime]] = None,
324326
one_observation: Optional[bool] = None,
325327
language: Optional[str] = None,
326328
units: Optional[str] = None,
@@ -335,7 +337,8 @@ def get_dest_weather(
335337
:param query: Free text query. Examples: "125, Berliner, berlin", "Beacon, Boston"
336338
:param zipcode: ZIP code of the location. This parameter is supported only for locations in
337339
the United States of America.
338-
:param hourly_date: Date for which hourly forecasts are to be retrieved.
340+
:param hourly_date: Date for which hourly forecasts are to be retrieved. Can be either a
341+
`date` or `datetime` object
339342
:param one_observation: Boolean, if set to true, the response only includes the closest
340343
location. Only available when the `product` parameter is set to
341344
`DEST_WEATHER_PRODUCT.observation`.
@@ -370,9 +373,7 @@ def get_dest_weather(
370373

371374
def get_weather_alerts(
372375
self,
373-
feature_type: str,
374-
geometry_type: str,
375-
geometry_coordinates: List,
376+
geometry: Union[Point, LineString, Polygon, MultiPolygon],
376377
start_time: datetime,
377378
id: Optional[str] = None,
378379
weather_severity: Optional[int] = None,
@@ -384,9 +385,8 @@ def get_weather_alerts(
384385
"""Retrieves weather reports, weather forecasts, severe weather alerts
385386
and moon and sun rise and set information.
386387
387-
:param feature_type: String to define feature type
388-
:param geometry_type: Point or LineString or Polygon or MultiPolygon
389-
:param geometry_coordinates: Array of coordinates corressponding to type provided
388+
:param geometry: Point or LineString or Polygon or MultiPolygon defining the route or
389+
a single location
390390
:param start_time: Start time of the event
391391
:param id: Unique weather alert id.
392392
:param weather_severity: Defines the severity of the weather event as defined
@@ -402,9 +402,7 @@ def get_weather_alerts(
402402
"""
403403

404404
resp = self.destination_weather_api.get_weather_alerts(
405-
feature_type=feature_type,
406-
geometry_type=geometry_type,
407-
geometry_coordinates=geometry_coordinates,
405+
geometry=geometry,
408406
id=id,
409407
weather_severity=weather_severity,
410408
weather_type=weather_type,

tests/test_ls.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pandas as pd
88
import pytest
99
import pytz
10-
from geojson import FeatureCollection
10+
from geojson import FeatureCollection, Point
1111

1212
from here_location_services import LS
1313
from here_location_services.config.autosuggest_config import POLITICAL_VIEW, SHOW, SearchCircle
@@ -63,18 +63,14 @@ def test_ls_weather_alerts():
6363
"""Test weather alerts endpoint of destination weather api."""
6464
ls = LS(api_key=LS_API_KEY)
6565
resp = ls.get_weather_alerts(
66-
feature_type="Feature",
67-
geometry_type="Point",
68-
geometry_coordinates=[15.256, 23.456],
66+
geometry=Point(coordinates=[15.256, 23.456]),
6967
start_time=datetime.now(),
7068
width=3000,
7169
)
7270
assert resp
7371

7472
resp2 = ls.get_weather_alerts(
75-
feature_type="Feature",
76-
geometry_type="Point",
77-
geometry_coordinates=[15.256, 23.456],
73+
geometry=Point(coordinates=[15.256, 23.456]),
7874
start_time=datetime.now(),
7975
weather_type=WEATHER_TYPE.ice,
8076
weather_severity=WEATHER_SEVERITY.high,
@@ -86,9 +82,7 @@ def test_ls_weather_alerts():
8682
with pytest.raises(ApiError):
8783
ls2 = LS(api_key="dummy")
8884
ls2.get_weather_alerts(
89-
feature_type="Feature",
90-
geometry_type="Point",
91-
geometry_coordinates=[15.256, 23.456],
85+
geometry=Point(coordinates=[15.256, 23.456]),
9286
start_time=datetime.now(),
9387
)
9488

tests/test_ls_apis.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import pytest
88
import requests
9+
from geojson import Point
910

1011
from here_location_services.config.dest_weather_config import DEST_WEATHER_PRODUCT
1112
from here_location_services.config.isoline_routing_config import (
@@ -34,9 +35,7 @@ def test_destination_weather(destination_weather_api):
3435
def test_weather_alerts(destination_weather_api):
3536
"""Test Destination Weather api."""
3637
resp = destination_weather_api.get_weather_alerts(
37-
feature_type="Feature",
38-
geometry_type="Point",
39-
geometry_coordinates=[15.256, 23.456],
38+
geometry=Point(coordinates=[15.256, 23.456]),
4039
start_time=datetime.now(),
4140
)
4241
assert type(resp) == requests.Response

0 commit comments

Comments
 (0)