Example #1
0
 def test_invalid_datetime(self):
     date_str = "2015-06-0407T03:43Z"
     data_str = '{{"dateTime": "{}"}}'.format(date_str)
     data = json_deserialize(data_str)
     utc_date = data.get("dateTime")
     assert utc_date is not None
     assert utc_date == date_str
Example #2
0
 def test_only_date(self):
     date_str = "0001-01-01"
     data_str = '{{"dateTime": "{}"}}'.format(date_str)
     data = json_deserialize(data_str)
     utc_date = data.get("dateTime")
     assert utc_date is not None
     check_date = datetime(1, 1, 1, 0, 0)
     assert utc_date.year == check_date.year
     assert utc_date.month == check_date.month
     assert utc_date.day == check_date.day
Example #3
0
 def test_top_level_datetime(self):
     data_str = '{"dateTime": "2015-06-04T07:03:43Z"}'
     data = json_deserialize(data_str)
     utc_date = data.get("dateTime")
     assert utc_date is not None
     check_date = datetime(2015, 6, 4, 7, 3, 43)
     assert utc_date.year == check_date.year
     assert utc_date.month == check_date.month
     assert utc_date.day == check_date.day
     assert utc_date.hour == check_date.hour
     assert utc_date.minute == check_date.minute
     assert utc_date.second == check_date.second
Example #4
0
 def test_second_level_datetime(self):
     data_str = '[{"group": {"dateTime": "2015-06-04T07:03:43Z"}}]'
     data = json_deserialize(data_str)
     assert len(data) == 1
     group = data[0].get("group")
     assert group is not None
     utc_date = group.get("dateTime")
     assert utc_date is not None
     check_date = datetime(2015, 6, 4, 7, 3, 43)
     assert utc_date.year == check_date.year
     assert utc_date.month == check_date.month
     assert utc_date.day == check_date.day
     assert utc_date.hour == check_date.hour
     assert utc_date.minute == check_date.minute
     assert utc_date.second == check_date.second
Example #5
0
async def _query(server,
                 method,
                 parameters,
                 timeout=DEFAULT_TIMEOUT,
                 verify_ssl=True):
    """Formats and performs the asynchronous query against the API

    :param server: The server to query.
    :param method: The method name.
    :param parameters: A dict of parameters to send
    :param timeout: The timeout to make the call, in seconds. By default, this is 300 seconds (or 5 minutes).
    :param verify_ssl: Whether or not to verify SSL connections
    :return: The JSON-decoded result from the server
    :raise MyGeotabException: Raises when an exception occurs on the MyGeotab server
    :raise TimeoutException: Raises when the request does not respond after some time.
    :raise aiohttp.ClientResponseError: Raises when there is an HTTP status code that indicates failure.
    """
    api_endpoint = api.get_api_url(server)
    params = dict(id=-1, method=method, params=parameters)
    headers = get_headers()
    conn = aiohttp.TCPConnector(
        ssl=ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) if verify_ssl else False)
    try:
        async with aiohttp.ClientSession(connector=conn) as session:
            response = await session.post(api_endpoint,
                                          data=json_serialize(params),
                                          headers=headers,
                                          timeout=timeout,
                                          allow_redirects=True)
            response.raise_for_status()
            content_type = response.headers.get("Content-Type")
            body = await response.text()
    except TimeoutError:
        raise TimeoutException(server)
    if content_type and "application/json" not in content_type.lower():
        return body
    return api._process(json_deserialize(body))