Best Python code snippet using localstack_python
lambda_api.py
Source:lambda_api.py
...1212 "Id": "LambdaFuncAccess-%s" % sid,1213 "Statement": [new_statement],1214 }1215 return policy1216def cors_config_from_dict(cors: Dict):1217 return {1218 "Cors": {1219 "AllowCredentials": cors.get("AllowCredentials", ["*"]),1220 "AllowHeaders": cors.get("AllowHeaders", ["*"]),1221 "AllowMethods": cors.get("AllowMethods", ["*"]),1222 "AllowOrigins": cors.get("AllowOrigins", ["*"]),1223 "ExposeHeaders": cors.get("ExposeHeaders", []),1224 "MaxAge": cors.get("MaxAge", 0),1225 }1226 }1227@app.route("%s/functions/<function>/policy" % API_PATH_ROOT, methods=["POST"])1228def add_permission(function):1229 arn = func_arn(function)1230 qualifier = request.args.get("Qualifier")1231 q_arn = func_qualifier(function, qualifier)1232 result = add_permission_policy_statement(function, arn, q_arn, qualifier=qualifier)1233 return result, 2011234def correct_error_response_for_url_config(response):1235 response_data = json.loads(response.data)1236 response_data.update({"Message": response_data.get("message")})1237 response.set_data(json.dumps(response_data))1238 return response1239@app.route("%s/functions/<function>/url" % API_PATH_ROOT_2, methods=["POST"])1240def create_url_config(function):1241 arn = func_arn(function)1242 qualifier = request.args.get("Qualifier")1243 q_arn = func_qualifier(function, qualifier)1244 lambda_backend = LambdaRegion.get()1245 function = lambda_backend.lambdas.get(arn)1246 if function is None:1247 response = error_response("Function does not exist", 404, "ResourceNotFoundException")1248 return correct_error_response_for_url_config(response)1249 if qualifier and not function.qualifier_exists(qualifier=qualifier):1250 return not_found_error()1251 arn = q_arn or arn1252 lambda_backend = LambdaRegion.get()1253 if arn in lambda_backend.url_configs:1254 return error_response(1255 f"Failed to create function url config for [functionArn = {arn}]. Error message: FunctionUrlConfig exists for this Lambda function",1256 409,1257 "ResourceConflictException",1258 )1259 custom_id = md5(str(random()))1260 region = LambdaRegion.get_current_request_region()1261 url = f"http://{custom_id}.lambda-url.{region}.{LOCALHOST_HOSTNAME}:{config.EDGE_PORT_HTTP or config.EDGE_PORT}/"1262 # TODO: HTTPS support1263 data = json.loads(to_str(request.data))1264 url_config = {1265 "AuthType": data.get("AuthType"),1266 "FunctionArn": arn,1267 "FunctionUrl": url,1268 "CreationTime": timestamp(format=TIMESTAMP_FORMAT_MICROS),1269 "LastModifiedTime": timestamp(format=TIMESTAMP_FORMAT_MICROS),1270 "CustomId": custom_id,1271 }1272 if "Cors" in data:1273 url_config.update(cors_config_from_dict(data.get("Cors", {})))1274 lambda_backend.url_configs.update({arn: url_config})1275 response = url_config.copy()1276 response.pop("LastModifiedTime")1277 response.pop("CustomId")1278 return response, 2011279@app.route("%s/functions/<function>/url" % API_PATH_ROOT_2, methods=["GET"])1280def get_url_config(function):1281 # if there's a qualifier it *must* be an alias1282 qualifier = request.args.get("Qualifier")1283 arn = func_arn(function)1284 lambda_backend = LambdaRegion.get()1285 # function doesn't exist1286 fn = lambda_backend.lambdas.get(arn)1287 if not fn:1288 return correct_error_response_for_url_config(1289 error_response(1290 "The resource you requested does not exist.",1291 404,1292 error_type="ResourceNotFoundException",1293 )1294 )1295 # alias doesn't exist1296 if qualifier and not fn.aliases.get(qualifier):1297 return correct_error_response_for_url_config(1298 error_response(1299 "The resource you requested does not exist.",1300 404,1301 error_type="ResourceNotFoundException",1302 )1303 )1304 # function url doesn't exit1305 url_config = lambda_backend.url_configs.get(arn)1306 if not url_config:1307 return correct_error_response_for_url_config(1308 error_response(1309 "The resource you requested does not exist.",1310 404,1311 error_type="ResourceNotFoundException",1312 )1313 )1314 response = url_config.copy()1315 response.pop("CustomId")1316 return response1317@app.route("%s/functions/<function>/url" % API_PATH_ROOT_2, methods=["PUT"])1318def update_url_config(function):1319 arn = func_arn(function)1320 qualifier = request.args.get("Qualifier")1321 q_arn = func_qualifier(function, qualifier)1322 arn = q_arn or arn1323 lambda_backend = LambdaRegion.get()1324 prev_url_config = lambda_backend.url_configs.get(arn)1325 if prev_url_config is None:1326 return not_found_error()1327 data = json.loads(to_str(request.data))1328 new_url_config = {1329 "AuthType": data.get("AuthType"),1330 "LastModifiedTime": timestamp(format=TIMESTAMP_FORMAT_MICROS),1331 }1332 if "Cors" in data:1333 new_url_config.update(cors_config_from_dict(data.get("Cors", {})))1334 prev_url_config.update(new_url_config)1335 response = prev_url_config.copy()1336 response.pop("CustomId")1337 return response1338@app.route("%s/functions/<function>/url" % API_PATH_ROOT_2, methods=["DELETE"])1339def delete_url_config(function):1340 arn = func_arn(function)1341 qualifier = request.args.get("Qualifier")1342 q_arn = func_qualifier(function, qualifier)1343 arn = q_arn or arn1344 lambda_backend = LambdaRegion.get()1345 if arn not in lambda_backend.url_configs:1346 response = error_response("Function does not exist", 404, "ResourceNotFoundException")1347 return response...
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!!