Skip to content

Commit

Permalink
Improve integration of booking service
Browse files Browse the repository at this point in the history
  • Loading branch information
sysang committed Aug 29, 2022
1 parent 523bdfe commit 9b24389
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
13 changes: 12 additions & 1 deletion botserver-action/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
47 changes: 28 additions & 19 deletions botserver-action/booking_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand All @@ -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()


Expand All @@ -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()


Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 9b24389

Please sign in to comment.