How to use the googlemaps.exceptions.ApiError function in googlemaps

To help you get started, we’ve selected a few googlemaps 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 googlemaps / google-maps-services-python / test / test_client.py View on Github external
def test_no_retry_over_query_limit(self):
        responses.add(responses.GET,
                      "https://maps.googleapis.com/foo",
                      body='{"status":"OVER_QUERY_LIMIT"}',
                      status=200,
                      content_type="application/json")

        client = googlemaps.Client(key="AIzaasdf",
                                   retry_over_query_limit=False)

        with self.assertRaises(googlemaps.exceptions.ApiError):
            client._request("/foo", {})

        self.assertEqual(1, len(responses.calls))
github googlemaps / google-maps-services-python / test / test_directions.py View on Github external
def test_transit_without_time(self):
        # With mode of transit, we need a departure_time or an
        # arrival_time specified
        with self.assertRaises(googlemaps.exceptions.ApiError):
            self.client.directions("Sydney Town Hall", "Parramatta, NSW",
                                  mode="transit")
github googlemaps / google-maps-services-python / test / test_elevation.py View on Github external
def test_elevation_along_path_single(self):
        with self.assertRaises(googlemaps.exceptions.ApiError):
            results = self.client.elevation_along_path(
                    [(40.714728, -73.998672)], 5)
github tehp / OpenPoGoBot / pokemongo_bot / mapper.py View on Github external
# If we have been given a string of coordinates
        if location.count(',') == 1:
            try:
                parts = location.split(',')

                pos_lat = float(parts[0])
                pos_lng = float(parts[1])

                # we need to ask google for the altitude
                response = self.google_maps.elevation((pos_lat, pos_lng))

                if response is not None and len(response) and "elevation" in response[0]:
                    return pos_lat, pos_lng, response[0]["elevation"]
                else:
                    raise ValueError
            except ApiError:
                self._log("Could not fetch altitude from google. Trying geolocator.", color='yellow')
            except ValueError:
                self._log("Location was not Lat/Lng. Trying geolocator.", color='yellow')

        # Fallback to geolocation if no Lat/Lng can be found
        loc = self.google_maps.geocode(location)

        return loc.latitude, loc.longitude, loc.altitude
github hzlmn / aiogmaps / aiogmaps / client.py View on Github external
async def _get_body(self, response):
        if response.status != 200:
            raise googlemaps.exceptions.HTTPError(response.status)

        body = await response.json()

        api_status = body['status']
        if api_status == 'OK' or api_status == 'ZERO_RESULTS':
            return body

        if api_status == 'OVER_QUERY_LIMIT':
            raise googlemaps.exceptions._OverQueryLimit(
                api_status, body.get('error_message'))

        raise googlemaps.exceptions.ApiError(api_status,
                                             body.get('error_message'))
github home-assistant / home-assistant / homeassistant / components / sensor / google_travel_time.py View on Github external
# Check if location is a trackable entity
        if origin.split('.', 1)[0] in TRACKABLE_DOMAINS:
            self._origin_entity_id = origin
        else:
            self._origin = origin

        if destination.split('.', 1)[0] in TRACKABLE_DOMAINS:
            self._destination_entity_id = destination
        else:
            self._destination = destination

        import googlemaps
        self._client = googlemaps.Client(api_key, timeout=10)
        try:
            self.update()
        except googlemaps.exceptions.ApiError as exp:
            _LOGGER .error(exp)
            self.valid_api_connection = False
            return
github googlemaps / google-maps-services-python / googlemaps / geolocation.py View on Github external
def _geolocation_extract(response):
    """
    Mimics the exception handling logic in ``client._get_body``, but
    for geolocation which uses a different response format.
    """
    body = response.json()
    if response.status_code in (200, 404):
        return body
    elif response.status_code == 403:
        raise exceptions._RetriableRequest()
    else:
        try:
            error = body["error"]["errors"][0]["reason"]
        except KeyError:
            error = None
        raise exceptions.ApiError(response.status_code, error)
github googlemaps / google-maps-services-python / googlemaps / client.py View on Github external
if resp.status_code != 200:
            raise googlemaps.exceptions.HTTPError(resp.status_code)

        body = resp.json()

        api_status = body["status"]
        if api_status == "OK" or api_status == "ZERO_RESULTS":
            return body

        if api_status == "OVER_QUERY_LIMIT":
            # Retry request.
            return self._get(url, params, first_request_time, retry_counter + 1)

        if "error_message" in body:
            raise googlemaps.exceptions.ApiError(api_status,
                    body["error_message"])
        else:
            raise googlemaps.exceptions.ApiError(api_status)