def get(event, context): try: req_zipcode = event['pathParameters']['id'] zipcode_db_dir = os.path.join(os.path.dirname(__file__), ".uszipcode") search = SearchEngine(simple_zipcode=True, db_file_dir=zipcode_db_dir) zipcode = search.by_zipcode(req_zipcode) search.close() if zipcode.to_dict()['zipcode'] is not None: rlt = zipcode.to_dict() print(rlt) else: rlt = '' statusCode = 200 except Exception as e: print('[Error] ' + str(e)) statusCode = 400 rlt = '' response = { "statusCode": statusCode, "body": json.dumps(rlt) } return response
def _get_zipcode_globals() -> Tuple[SearchEngine, sessionmaker]: global _ZIP_CODE_SEARCH_ENGINE, _SESSION_MAKER if _ZIP_CODE_SEARCH_ENGINE is None or _SESSION_MAKER is None: _ZIP_CODE_SEARCH_ENGINE = SearchEngine(simple_zipcode=True) # Close the initial session that it creates, we will make our own later. _ZIP_CODE_SEARCH_ENGINE.close() _ZIP_CODE_SEARCH_ENGINE.ses = None # Create a sessionmaker to use for creating our own sessions since SearchEngine threw its away. _SESSION_MAKER = sessionmaker(bind=_ZIP_CODE_SEARCH_ENGINE.engine) return _ZIP_CODE_SEARCH_ENGINE, _SESSION_MAKER