Best Python code snippet using localstack_python
helpers.py
Source:helpers.py
...292 """Return URL for inbound API gateway for given API ID, stage name, and path with custom dns293 format"""294 pattern = "http://{endpoint}{stage}{path}"295 stage = stage_name and f"/{stage_name}" or ""296 return pattern.format(endpoint=get_execute_api_endpoint(rest_api_id), stage=stage, path=path)297def get_execute_api_endpoint(api_id: str, protocol: str = "") -> str:298 port = config.get_edge_port_http()299 return f"{protocol}{api_id}.execute-api.{LOCALHOST_HOSTNAME}:{port}"300def tokenize_path(path):301 return path.lstrip("/").split("/")302def extract_path_params(path: str, extracted_path: str) -> Dict[str, str]:303 tokenized_extracted_path = tokenize_path(extracted_path)304 # Looks for '{' in the tokenized extracted path305 path_params_list = [(i, v) for i, v in enumerate(tokenized_extracted_path) if "{" in v]306 tokenized_path = tokenize_path(path)307 path_params = {}308 for param in path_params_list:309 path_param_name = param[1][1:-1]310 path_param_position = param[0]311 if path_param_name.endswith("+"):...
prepare_files.py
Source:prepare_files.py
...5# Used below in prepare_xray_file, that will replace it in the placeholder in xray_stack.py.6# If the address is not replaced with a valid one the XRay stack deployment will fail.7# receiver_email = 'the_receiver_email@that_provider.com'8receiver_email = os.environ.get('RECEIVER_EMAIL', None)9def get_execute_api_endpoint(api_name, region):10 client = boto3.client('apigateway', region_name=region)11 try:12 api_id = None13 execute_api_prod_endpoint = None14 response = client.get_rest_apis()15 for api in response['items']:16 if api['name'] == api_name:17 api_id = api['id']18 if api_id:19 execute_api_prod_endpoint = 'https://' + api_id + '.execute-api.' + region + '.amazonaws.com/prod'20 return execute_api_prod_endpoint21 except ClientError as e:22 raise e23def get_user_pool_id(user_pool_name, region):24 client = boto3.client('cognito-idp')25 try:26 user_pool_id = None27 response = client.list_user_pools(MaxResults=10)28 for user_pool in response['UserPools']:29 if user_pool['Name'] == user_pool_name:30 user_pool_id = user_pool['Id']31 return user_pool_id32 except ClientError as e:33 raise e34def get_user_pool_client_id(user_pool_client_name, user_pool_id, region):35 client = boto3.client('cognito-idp')36 try:37 user_pool_client_id = None38 response = client.list_user_pool_clients(UserPoolId=user_pool_id, MaxResults=10)39 for user_pool_client in response['UserPoolClients']:40 if user_pool_client['ClientName'] == user_pool_client_name:41 user_pool_client_id = user_pool_client['ClientId']42 return user_pool_client_id43 except ClientError as e:44 raise e45def replace_string_in_file(str_list, source_file, target_file):46 with open(source_file, 'r') as source:47 replace_string = source.read()48 for i, string_couple in enumerate(str_list):49 if string_couple[1]:50 replace_string = replace_string.replace(string_couple[0], string_couple[1])51 else:52 replace_string = replace_string.replace(string_couple[0], 'NOT_FOUND')53 with open(target_file, 'w') as newfile:54 newfile.write(replace_string)55 return56def prepare_web_files(region):57 api_endpoint = get_execute_api_endpoint('MysfitsApi', region)58 # If no api endpoint then the web site cannot work59 if not api_endpoint:60 print(f'Found no API MysfitsApi')61 sys.exit(1)62 click_processing_api_endpoint = get_execute_api_endpoint('ClickProcessingApi', region)63 # If no api endpoint then there will be no click stream processing64 if not api_endpoint:65 print(f'Found no API ClickProcessingApi')66 questions_api_endpoint = get_execute_api_endpoint('QuestionsAPI', region)67 # If no api endpoint then no questions will be collected68 if not api_endpoint:69 print(f'Found no API QuestionsAPI')70 recommendations_api_endpoint = get_execute_api_endpoint('RecommendationsAPI', region)71 # If no api endpoint then no questions will be collected72 if not api_endpoint:73 print(f'Found no API RecommendationsAPI')74 user_pool_id = get_user_pool_id('MysfitsUserPool', region)75 # If no user_pool_id then login management will not be possible76 if not user_pool_id:77 print('Found no user pool MysfitsUserPool')78 user_pool_client_id = get_user_pool_client_id('MysfitsUserPoolClient', user_pool_id, region)79 # If no user_pool_client_id then login management will not be possible80 if not user_pool_client_id:81 print('Found no user pool client MysfitsUserPoolClient')82 # for index.html83 str_list = [84 ('REPLACE_ME_mysfitsApiEndpoint', api_endpoint),85 ('REPLACE_ME_streamingApiEndpoint', click_processing_api_endpoint),86 ('REPLACE_ME_questionsApiEndpoint', questions_api_endpoint),87 ('REPLACE_ME_recommendationsApiEndpoint', recommendations_api_endpoint),88 ('REPLACE_ME_REGION', region),89 ('REPLACE_ME_USER_POOL_ID', user_pool_id),90 ('REPLACE_ME_USER_POOL_CLIENT_ID', user_pool_client_id)91 ]92 replace_string_in_file(str_list, 'web/index.html', 'web/index.html')93 # for register.html94 str_list = [95 ('REPLACE_ME_USER_POOL_ID', user_pool_id),96 ('REPLACE_ME_USER_POOL_CLIENT_ID', user_pool_client_id)97 ]98 replace_string_in_file(str_list, 'web/register.html', 'web/register.html')99 # for confirm.html100 str_list = [101 ('REPLACE_ME_USER_POOL_ID', user_pool_id),102 ('REPLACE_ME_USER_POOL_CLIENT_ID', user_pool_client_id)103 ]104 replace_string_in_file(str_list, 'web/confirm.html', 'web/confirm.html')105def prepare_kinesis_file(region):106 api_endpoint = get_execute_api_endpoint('MysfitsApi', region)107 if not api_endpoint:108 print('Found no API MysfitsApi')109 # for kinesis_firehose_stack.py110 str_list = [111 ('REPLACE_ME_API_URL', api_endpoint)112 ]113 replace_string_in_file(str_list, 'webgen/kinesis_firehose_stack.py', 'webgen/kinesis_firehose_stack.py')114def prepare_xray_file(region):115 # for xray_stack.py116 str_list = [117 ('REPLACE_ME_RECEIVER_EMAIL', receiver_email)118 ]119 replace_string_in_file(str_list, 'webgen/xray_stack.py', 'webgen/xray_stack.py')120def get_sagemaker_endpoint_name(prefix, region):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!