How to use the pyowm.commons.http_client.HttpClient function in pyowm

To help you get started, we’ve selected a few pyowm examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github csparpa / pyowm / tests / unit / commons / test_http_client.py View on Github external
def test_get_json(self):

        expected_data = '{"name": "james bond", "designation": "007"}'

        def monkey_patched_get(uri, params=None, headers=None, timeout=None,
                               verify=False):
            return MockResponse(200, expected_data)

        requests.get = monkey_patched_get
        status, data = HttpClient().get_json('http://anyurl.com')
        self.assertEqual(json.loads(expected_data), data)
        requests.get = self.requests_original_get
github csparpa / pyowm / tests / unit / pollutionapi30 / test_airpollution_client.py View on Github external
# -*- coding: utf-8 -*-

import unittest
from pyowm.pollutionapi30.airpollution_client import AirPollutionHttpClient
from pyowm.commons.http_client import HttpClient
from pyowm.caches.nullcache import NullCache
from pyowm.utils import timeformatutils


class TestAirPollutionHttpClient(unittest.TestCase):

    __test_cache = NullCache()
    __instance = AirPollutionHttpClient('xyz', HttpClient(cache=__test_cache))

    def test_trim_to(self):
        ts = timeformatutils.to_date(1463041620)  # 2016-05-12T08:27:00Z
        self.assertEquals(self.__instance._trim_to(ts, 'minute'),
                          '2016-05-12T08:27Z')
        self.assertEquals(self.__instance._trim_to(ts, 'hour'),
                          '2016-05-12T08Z')
        self.assertEquals(self.__instance._trim_to(ts, 'day'),
                          '2016-05-12Z')
        self.assertEquals(self.__instance._trim_to(ts, 'month'),
                          '2016-05Z')
        self.assertEquals(self.__instance._trim_to(ts, 'year'),
                          '2016Z')
        self.assertRaises(ValueError, self.__instance._trim_to,
                          ts, 'abcdef')
github csparpa / pyowm / tests / unit / commons / test_http_client.py View on Github external
def test_timeouts(self):
        timeout = 0.5
        def monkey_patched_get_timeouting(uri, params=None, headers=None,
                                          timeout=timeout, verify=False):
            raise requests.exceptions.Timeout()

        requests.get = monkey_patched_get_timeouting
        try:
            status, data = HttpClient(timeout=timeout).get_json('http://anyurl.com')
            self.fail()
        except api_call_error.APICallTimeoutError:
            requests.get = self.requests_original_get
github csparpa / pyowm / tests / unit / alertapi30 / test_alert_manager.py View on Github external
"time_period":{"end":{"amount":432000000,"expression":"after"},"start":{"amount":132000000,"expression":"after"}}}'''

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

    def post(self, uri, params=None, data=None, headers=None):
        return 200, json.loads(self.test_trigger_json)

    def put(self, uri, params=None, data=None, headers=None):
        return 200, [json.loads(self.test_trigger_json)]

    def delete(self, uri, params=None, data=None, headers=None):
        return 204, [json.loads(self.test_trigger_json)]


class MockHttpClientTwoTriggers(HttpClient):
    # 2 triggers
    test_triggers_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":"after"},
    "start":{"amount":132000000,"expression":"after"}}},{"_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":"after"},"start":{"amount":132000000,"expression":"after"}}}]'''

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


class MockHttpClientOneTrigger(HttpClient):
github csparpa / pyowm / tests / unit / alertapi30 / test_alert_manager.py View on Github external
# 2 triggers
    test_triggers_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":"after"},
    "start":{"amount":132000000,"expression":"after"}}},{"_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":"after"},"start":{"amount":132000000,"expression":"after"}}}]'''

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


class MockHttpClientOneTrigger(HttpClient):
    # 1 trigger
    test_trigger_json = '''{"_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":"after"},"start":{"amount":132000000,"expression":"after"}}}'''

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


class MockHttpClientTwoAlerts(HttpClient):
    # 2 alerts
    test_alerts_json = '''[{"_id": "5853dbe27416a400011b1b77","date": "2016-12-17T00:00:00.000Z","last_update": 
    "2016-12-16T11:19:46.352Z","triggerId": "5852816a9aaacb00153134a3","__v": 0,"conditions": [{"current_value": 
github csparpa / pyowm / tests / unit / commons / test_http_client.py View on Github external
return MockResponse(204, None)

        requests.delete = monkey_patched_delete
        status, data = HttpClient().delete('http://anyurl.com')
        self.assertIsNone(data)

        # in case a non-empty payload is returned
        expected_data = '{"message": "deleted"}'

        def monkey_patched_delete_returning_payload(uri, params=None, headers=None,
                                                    json=None, timeout=None,
                                                    verify=False):
            return MockResponse(204, expected_data)

        requests.delete = monkey_patched_delete_returning_payload
        status, data = HttpClient().delete('http://anyurl.com')
        self.assertEqual(json.loads(expected_data), data)

        requests.delete = self.requests_original_delete
github csparpa / pyowm / pyowm / commons / http_client.py View on Github external
def to_url(cls, API_endpoint_URL, API_key, subscription_type, use_ssl=False):
        # Add API Key to query params
        params = dict()
        if API_key is not None:
            params['APPID'] = API_key
        # Escape subscription subdomain if needed
        escaped_url = HttpClient._escape_subdomain(API_endpoint_URL, subscription_type)
        url = HttpClient._fix_schema(escaped_url, use_ssl)
        r = requests.Request('GET', url, params=params).prepare()
        return r.url
github csparpa / pyowm / pyowm / airpollutionapi30 / airpollution_manager.py View on Github external
def __init__(self, API_key, config):
        assert API_key is not None, 'You must provide a valid API Key'
        self.API_key = API_key
        assert isinstance(config, dict)
        self.ap_client = airpollution_client.AirPollutionHttpClient(
            API_key,
            HttpClient(API_key, config, ROOT_POLLUTION_API_URL))
github csparpa / pyowm / pyowm / weatherapi25 / weather_manager.py View on Github external
def __init__(self, API_key, config):
        assert isinstance(API_key, str), 'You must provide a valid API Key'
        self.API_key = API_key
        assert isinstance(config, dict)
        self.http_client = HttpClient(API_key, config, ROOT_WEATHER_API)
github csparpa / pyowm / pyowm / alertapi30 / alert_manager.py View on Github external
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.alert_parser = AlertParser()
        self.http_client = HttpClient()