Esempio n. 1
0
    def update_trigger(self, trigger):
        """
        Updates on the Alert API the trigger record having the ID of the specified Trigger object: the remote record is
        updated with data from the local Trigger object.

        :param trigger: the Trigger with updated data
        :type trigger: `pyowm.alertapi30.trigger.Trigger`
        :return: ``None`` if update is successful, an error otherwise
        """
        assert trigger is not None
        stringutils.assert_is_string_or_unicode(trigger.id)
        the_time_period = {
            "start": {
                "expression": "after",
                "amount": trigger.start_after_millis
            },
            "end": {
                "expression": "after",
                "amount": trigger.end_after_millis
            }
        }
        the_conditions = [
            dict(name=c.weather_param, expression=c.operator, amount=c.amount)
            for c in trigger.conditions
        ]
        the_area = [a.as_dict() for a in trigger.area]

        status, _ = self.http_client.put(
            NAMED_TRIGGER_URI % trigger.id,
            params={'appid': self.API_key},
            data=dict(time_period=the_time_period,
                      conditions=the_conditions,
                      area=the_area),
            headers={'Content-Type': 'application/json'})
Esempio n. 2
0
    def test_assert_is_string_or_unicode(self):
        a_string = 'test'
        a_non_string = 123
        stringutils.assert_is_string_or_unicode(a_string)
        self.assertRaises(AssertionError,
                          stringutils.assert_is_string_or_unicode,
                          a_non_string)

        try:  # only for Python 2
            unicode_value = unicode('test')
            stringutils.assert_is_string_or_unicode(unicode_value)
        except:
            pass
Esempio n. 3
0
 def delete_all_alerts_for(self, trigger):
     """
     Deletes all of the alert that were fired for the specified Trigger
     :param trigger: the trigger whose alerts are to be cleared
     :type trigger: `pyowm.alertapi30.trigger.Trigger`
     :return: `None` if deletion is successful, an exception otherwise
     """
     assert trigger is not None
     stringutils.assert_is_string_or_unicode(trigger.id)
     status, _ = self.http_client.delete(
         ALERTS_URI % trigger.id,
         params={'appid': self.API_key},
         headers={'Content-Type': 'application/json'})
Esempio n. 4
0
 def delete_alert(self, alert):
     """
     Deletes the specified alert from the Alert API
     :param alert: the alert to be deleted
     :type alert: pyowm.alertapi30.alert.Alert`
     :return: ``None`` if the deletion was successful, an error otherwise
     """
     assert alert is not None
     stringutils.assert_is_string_or_unicode(alert.id)
     stringutils.assert_is_string_or_unicode(alert.trigger_id)
     status, _ = self.http_client.delete(
         NAMED_ALERT_URI % (alert.trigger_id, alert.id),
         params={'appid': self.API_key},
         headers={'Content-Type': 'application/json'})
Esempio n. 5
0
 def get_alerts_for(self, trigger):
     """
     Retrieves all of the alerts that were fired for the specified Trigger
     :param trigger: the trigger
     :type trigger: `pyowm.alertapi30.trigger.Trigger`
     :return: list of `pyowm.alertapi30.alert.Alert` objects
     """
     assert trigger is not None
     stringutils.assert_is_string_or_unicode(trigger.id)
     status, data = self.http_client.get_json(
         ALERTS_URI % trigger.id,
         params={'appid': self.API_key},
         headers={'Content-Type': 'application/json'})
     return [self.alert_parser.parse_dict(item) for item in data]
Esempio n. 6
0
    def get_trigger(self, trigger_id):
        """
        Retrieves the named trigger from the Weather Alert API.

        :param trigger_id: the ID of the trigger
        :type trigger_id: str
        :return: a `pyowm.alertapi30.trigger.Trigger` instance
        """
        stringutils.assert_is_string_or_unicode(trigger_id)
        status, data = self.http_client.get_json(
            NAMED_TRIGGER_URI % trigger_id,
            params={'appid': self.API_key},
            headers={'Content-Type': 'application/json'})
        return self.trigger_parser.parse_dict(data)
Esempio n. 7
0
    def delete_trigger(self, trigger):
        """
        Deletes from the Alert API the trigger record identified by the ID of the provided
        `pyowm.alertapi30.trigger.Trigger`, along with all related alerts

        :param trigger: the `pyowm.alertapi30.trigger.Trigger` object to be deleted
        :type trigger: `pyowm.alertapi30.trigger.Trigger`
        :returns: `None` if deletion is successful, an exception otherwise
        """
        assert trigger is not None
        stringutils.assert_is_string_or_unicode(trigger.id)
        status, _ = self.http_client.delete(
            NAMED_TRIGGER_URI % trigger.id,
            params={'appid': self.API_key},
            headers={'Content-Type': 'application/json'})
Esempio n. 8
0
 def get_alert(self, alert_id, trigger):
     """
     Retrieves info about the alert record on the Alert API that has the specified ID and belongs to the specified
     parent Trigger object
     :param trigger: the parent trigger
     :type trigger: `pyowm.alertapi30.trigger.Trigger`
     :param alert_id: the ID of the alert
     :type alert_id `pyowm.alertapi30.alert.Alert`
     :return: an `pyowm.alertapi30.alert.Alert` instance
     """
     assert trigger is not None
     assert alert_id is not None
     stringutils.assert_is_string_or_unicode(alert_id)
     stringutils.assert_is_string_or_unicode(trigger.id)
     status, data = self.http_client.get_json(
         NAMED_ALERT_URI % (trigger.id, alert_id),
         params={'appid': self.API_key},
         headers={'Content-Type': 'application/json'})
     return self.alert_parser.parse_dict(data)
Esempio n. 9
0
    def __init__(self, id, trigger_id, met_conditions, coordinates, last_update=None):
        assert id is not None
        stringutils.assert_is_string_or_unicode(id)
        self.id = id

        assert trigger_id is not None
        stringutils.assert_is_string_or_unicode(trigger_id)
        self.trigger_id = trigger_id

        assert met_conditions is not None
        assert isinstance(met_conditions, list)
        self.met_conditions = met_conditions

        assert coordinates is not None
        assert isinstance(coordinates, dict)
        self.coordinates = coordinates

        if last_update is not None:
            assert isinstance(last_update, int)
        self.last_update = last_update