diff --git a/botserver-action/actions.py b/botserver-action/actions.py index 4dc9b14..852fe05 100644 --- a/botserver-action/actions.py +++ b/botserver-action/actions.py @@ -207,6 +207,10 @@ async def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: pagination_intent = 'user_click_to_navigate_search_result' notes_bkinfo = slots.get('notes_bkinfo') bkinfo_orderby = slots.get('bkinfo_orderby', None) + bkinfo_area_type = slots.get('bkinfo_area_type', None) + bkinfo_district = slots.get('bkinfo_district', None) + bkinfo_region = slots.get('bkinfo_region', None) + bkinfo_country = slots.get('bkinfo_country', None) query_payload = slots.get('search_result_query', '') notes_search_result = slots.get('notes_search_result', None) botmemo_booking_progress = FSMBotmemeBookingProgress(slots) @@ -252,7 +256,14 @@ async def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: hotels = picklize_search_result(get_cache(notes_search_result)) logger.info('[INFO] Retrieve hotels from redis cache, key: notes_search_result.') else: - hotels = await search_rooms(bkinfo_orderby=bkinfo_orderby, **bkinfo) + hotels = await search_rooms( + **bkinfo, + bkinfo_orderby=bkinfo_orderby, + bkinfo_area_type=bkinfo_area_type, + bkinfo_district=bkinfo_district, + bkinfo_region=bkinfo_region, + bkinfo_country=bkinfo_country, + ) if not isinstance(hotels, dict): error_message = '[ERROR] in botacts_search_hotel_rooms action, search_rooms returns wrong data type: %s.' % (type(hotels)) diff --git a/botserver-action/booking_service.py b/botserver-action/booking_service.py index f5dc0aa..b896789 100644 --- a/botserver-action/booking_service.py +++ b/botserver-action/booking_service.py @@ -9,7 +9,7 @@ import copy from arrow import Arrow -from requests import get +from requests import get, RequestException from cachetools import cached, TTLCache from cachecontrol import CacheControl from cachecontrol import CacheControlAdapter @@ -85,6 +85,24 @@ requests_sess = CacheControl(sess=requests.Session(), cache=RedisCache(r), heuristic=ExpiresAfter(minutes=REQUESTS_CACHE_MINS)) +def make_request_to_bookingapi(url, headers, params): + + for i in range(3): + try: + response = requests_sess.get(url, headers=headers, params=params) + response.raise_for_status() + + return response + + except RequestException: + logger.error('[INFO] try to request_to_search_hotel, %s, API url: %s, HttpError: %s', 'RequestException', response.url, response.reason) + time.sleep(0.1) + + # TODO: propagate the error back to action to inform user something like + # "I got problem while communicating booking service, please try again after few minutes" + return None + + # @cached(cache=TTLCache(maxsize=128, ttl=60)) async def search_rooms( bkinfo_area, bkinfo_checkin_time, bkinfo_duration, @@ -346,7 +364,7 @@ def request_room_list_by_hotel(hotel_id, checkin_date, checkout_date, currency=C "units": UNITS, } - response = requests_sess.get(url, headers=headers, params=querystring) + response = make_request_to_bookingapi(url, headers=headers, params=querystring) logger.info('[INFO] request_room_list_by_hotel, API url: %s', response.url) if not response.ok: @@ -400,21 +418,15 @@ def request_to_search_hotel(dest_id, dest_type, checkin_date, checkout_date, ord "units": UNITS, } - for i in range(3): - try: - response = requests_sess.get(url, headers=headers, params=querystring) - response.raise_for_status() - - logger.info('[INFO] request_to_search_hotel, API url: %s', response.url) + response = make_request_to_bookingapi(url, headers=headers, params=querystring) - return response.json() - - except: - logger.info('[INFO] try to request_to_search_hotel, API url: %s, error: %s', response.url, i) - time.sleep(0.1) + logger.info('[INFO] request_to_search_hotel, API url: %s', response.url) + if response: + return response.json() return {} + def request_hotel_data(hotel_id): """ >>> details = response.json() @@ -434,11 +446,9 @@ def request_hotel_data(hotel_id): "locale": LOCALE, } - response = requests_sess.get(url, headers=headers, params=querystring) + response = make_request_to_bookingapi(url, headers=headers, params=querystring) logger.info('[INFO] request_hotel_data, API url: %s', response.url) - response.raise_for_status() - return response.json() @@ -457,11 +467,9 @@ def request_to_search_locations(name): "locale": LOCALE, } - response = requests_sess.get(url, headers=headers, params=querystring) + response = make_request_to_bookingapi(url, headers=headers, params=querystring) logger.info('[INFO] request_to_search_locations, API url: %s', response.url) - response.raise_for_status() - return response.json() @@ -500,6 +508,7 @@ def choose_location(bkinfo_area, bkinfo_area_type=None, bkinfo_district=None, bk bkinfo_country=bkinfo_country, ) + logger.info('[DEV] request_to_search_locations -> %s', name) locations = request_to_search_locations(name=name) logger.info('[INFO] request_to_search_locations, found %s location(s) in respective to query: %s', len(locations), name)