Skip to content

Commit

Permalink
GET /triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
csparpa committed Jun 29, 2018
1 parent af437b3 commit 357c2fd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
15 changes: 14 additions & 1 deletion pyowm/alertapi30/alert_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pyowm.constants import ALERT_API_VERSION
from pyowm.commons.http_client import HttpClient
from pyowm.alertapi30.parsers import TriggerParser
from pyowm.alertapi30.uris import TRIGGERS_URI


class AlertManager:
Expand All @@ -18,6 +20,7 @@ class AlertManager:
def __init__(self, API_key):
assert API_key is not None, 'You must provide a valid API Key'
self.API_key = API_key
self.trigger_parser = TriggerParser()
self.http_client = HttpClient()

def alert_api_version(self):
Expand All @@ -29,7 +32,17 @@ def create_trigger(self):
raise NotImplementedError()

def get_triggers(self):
raise NotImplementedError()
"""
Retrieves all of the user's triggers that are set on the Weather Alert API.
:returns: list of `pyowm.alertapi30.trigger.Trigger` objects
"""
status, data = self.http_client.get_json(
TRIGGERS_URI,
params={'appid': self.API_key},
headers={'Content-Type': 'application/json'})
return [self.trigger_parser.parse_dict(item) for item in data]

def get_trigger(self, trigger_id):
raise NotImplementedError()
Expand Down
33 changes: 32 additions & 1 deletion tests/unit/alertingapi30/test_alert_manager.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
import unittest
import json
from pyowm.alertapi30.alert_manager import AlertManager
from pyowm.alertapi30.trigger import Trigger
from pyowm.commons.http_client import HttpClient
from pyowm.constants import ALERT_API_VERSION


class MockHttpClient(HttpClient):
# 2 triggers
test_station_json = '''[ {"_id":"585280edbe54110025ea52bb","__v":0,"alerts":{},"area":[{"type":"Point",
"_id":"585280edbe54110025ea52bc","coordinates":[53,37]}],"conditions":[{"name":"temp","expression":"$lt",
"amount":273,"_id":"585280edbe54110025ea52bd"}],"time_period":{"end":{"amount":432000000,"expression":"exact"},
"start":{"amount":132000000,"expression":"exact"}}},{"_id":"5852816a9aaacb00153134a3","__v":0,"alerts":
{"8b48b2cd21c23d2894466caccba1ed1f":{"conditions":[{"current_value":{"min":263.576,"max":263.576},"condition":
{"name":"temp","expression":"$lt","amount":273,"_id":"5852816a9aaacb00153134a5"}}],"last_update":1481802090232,
"date":1482181200000,"coordinates":{"lon":37,"lat":53}}},"area":[{"type":"Point","_id":"5852816a9aaacb00153134a4",
"coordinates":[37,53]}],"conditions":[{"name":"temp","expression":"$lt","amount":273,"_id":"5852816a9aaacb00153134a5"}],
"time_period":{"end":{"amount":432000000,"expression":"exact"},"start":{"amount":132000000,"expression":"exact"}}}]'''

def get_json(self, uri, params=None, headers=None):
return 200, json.loads(self.test_station_json)


class TestAlertManager(unittest.TestCase):

def factory(self, _kls):
sm = AlertManager('APIKey')
sm.http_client = _kls()
return sm

def test_instantiation_fails_without_api_key(self):
self.assertRaises(AssertionError, AlertManager, None)

def test_get_alert_api_version(self):
instance = AlertManager('APIKey')
result = instance.alert_api_version()
self.assertIsInstance(result, tuple)
self.assertEqual(result, ALERT_API_VERSION)
self.assertEqual(result, ALERT_API_VERSION)

def test_get_triggers(self):
instance = self.factory(MockHttpClient)
results = instance.get_triggers()
self.assertEqual(2, len(results))
t = results[0]
self.assertIsInstance(t, Trigger)

0 comments on commit 357c2fd

Please sign in to comment.