def test_get_device_id(self): test_input = self._create_handler_input( request=self.test_launch_request) self.assertEqual( get_device_id(handler_input=test_input), self.test_device_id, "get_device_id method returned incorrect " "device id from input request")
def test_get_device_id_returns_none_for_missing_device_info(self): self.test_request_envelope.context.system.device = None test_input = self._create_handler_input( request=self.test_launch_request) self.assertEqual( get_device_id(handler_input=test_input), None, "get_device_id method didn't return None when input request " "doesn't have device information")
def handle(self, handler_input): # type: (HandlerInput) -> Response logger.info("In HayBicisIntentHandler") service_client_fact = handler_input.service_client_factory response_builder = handler_input.response_builder if not (get_api_access_token(handler_input)): logger.info("no api access token") response_builder.speak(NOTIFY_MISSING_PERMISSIONS) response_builder.set_card( AskForPermissionsConsentCard(permissions=PERMISSIONS)) return response_builder.response try: device_id = get_device_id(handler_input) device_addr_client = service_client_fact.get_device_address_service( ) addr = device_addr_client.get_full_address(device_id) logger.info(addr) logger.info( 'Location API response retrieved, now building response') if not addr.city.lower() == "barcelona": return handler_input.response_builder.speak( NO_ADDRESS).response else: geolocator = Nominatim(user_agent="hay-bicis") address = "{}, Barcelona, {}".format(addr.address_line1, addr.postal_code) logger.info(address) coordinates = geolocator.geocode(address) closest = self.get_closest_distance(coordinates.latitude, coordinates.longitude) bikes_available = self.get_bikes(closest) return ( handler_input.response_builder.speak(bikes_available).response) except ServiceException as e: logger.error("error reported by device location service") raise e except Exception as e: logger.error(e, exc_info=True) return handler_input.response_builder.speak(ERROR)
def handle(self, handler_input): # type: (HandlerInput) -> Response logger.info("In GetAddressIntentHandler") service_client_fact = handler_input.service_client_factory response_builder = handler_input.response_builder if not (get_api_access_token(handler_input)): logger.info("no api access token") response_builder.speak(NOTIFY_MISSING_PERMISSIONS) response_builder.set_card( AskForPermissionsConsentCard(permissions=PERMISSIONS)) return response_builder.response try: device_id = get_device_id(handler_input) device_addr_client = service_client_fact.get_device_address_service( ) addr = device_addr_client.get_full_address(device_id) logger.info( 'Location API response retrieved, now building response') if addr.address_line1 is None and addr.state_or_region is None: response_builder.speak(NO_ADDRESS) else: response_builder.speak( ADDRESS_AVAILABLE.format(addr.address_line1, addr.state_or_region, addr.postal_code)) return response_builder.response except ServiceException as e: logger.error("error reported by device location service") raise e except Exception as e: logger.error(e, exc_info=True) return handler_input.response_builder.speak(ERROR)