Best Python code snippet using localstack_python
apigateway_listener.py
Source: apigateway_listener.py
...43 if method == 'GET':44 return get_gateway_responses(api_id)45 if method == 'PUT':46 response_type = search_match.group(2).lstrip('/')47 return put_gateway_response(api_id, response_type, data)48 return True49 def return_response(self, method, path, data, headers, response):50 # fix backend issue (missing support for API documentation)51 if re.match(r'/restapis/[^/]+/documentation/versions', path):52 if response.status_code == 404:53 return requests_response({'position': '1', 'items': []})54 # publish event55 if method == 'POST' and path == '/restapis':56 content = json.loads(to_str(response.content))57 event_publisher.fire_event(event_publisher.EVENT_APIGW_CREATE_API,58 payload={'a': event_publisher.get_hash(content['id'])})59 api_regex = r'^/restapis/([a-zA-Z0-9\-]+)$'60 if method == 'DELETE' and re.match(api_regex, path):61 api_id = re.sub(api_regex, r'\1', path)62 event_publisher.fire_event(event_publisher.EVENT_APIGW_DELETE_API,63 payload={'a': event_publisher.get_hash(api_id)})64# ------------65# API METHODS66# ------------67def get_gateway_responses(api_id):68 result = GATEWAY_RESPONSES.get(api_id, [])69 base_path = '/restapis/%s/gatewayresponses' % api_id70 href = 'http://docs.aws.amazon.com/apigateway/latest/developerguide/restapi-gatewayresponse-{rel}.html'71 def item(i):72 i['_links'] = {73 'self': {74 'href': '%s/%s' % (base_path, i['responseType'])75 },76 'gatewayresponse:put': {77 'href': '%s/{response_type}' % base_path,78 'templated': True79 },80 'gatewayresponse:update': {81 'href': '%s/%s' % (base_path, i['responseType'])82 }83 }84 i['responseParameters'] = i.get('responseParameters', {})85 i['responseTemplates'] = i.get('responseTemplates', {})86 return i87 result = {88 '_links': {89 'curies': {90 'href': href,91 'name': 'gatewayresponse',92 'templated': True93 },94 'self': {'href': base_path},95 'first': {'href': base_path},96 'gatewayresponse:by-type': {97 'href': '%s/{response_type}' % base_path,98 'templated': True99 },100 'item': [{'href': '%s/%s' % (base_path, r['responseType'])} for r in result]101 },102 '_embedded': {103 'item': [item(i) for i in result]104 },105 # Note: Looks like the format required by aws CLI ("item" at top level) differs from the docs:106 # https://docs.aws.amazon.com/apigateway/api-reference/resource/gateway-responses/107 'item': [item(i) for i in result]108 }109 return result110def put_gateway_response(api_id, response_type, data):111 GATEWAY_RESPONSES[api_id] = GATEWAY_RESPONSES.get(api_id, [])112 data['responseType'] = response_type113 GATEWAY_RESPONSES[api_id].append(data)114 return data115def run_authorizer(api_id, headers, authorizer):116 # TODO implement authorizers117 pass118def authorize_invocation(api_id, headers):119 client = aws_stack.connect_to_service('apigateway')120 authorizers = client.get_authorizers(restApiId=api_id, limit=100).get('items', [])121 for authorizer in authorizers:122 run_authorizer(api_id, headers, authorizer)123def validate_api_key(api_key, stage):124 key = None...
GatewayHandler.py
Source: GatewayHandler.py
...132 if response['ResponseMetadata']['HTTPStatusCode'] != 201:133 raise RuntimeError134 return response135 def create_gateway_response(self, response_type, parameters):136 response = self.client.put_gateway_response(137 restApiId=self.aws['api_id'],138 responseType=response_type,139 responseParameters=parameters,140 responseTemplates={141 'application/json': '{"message":$context.error.messageString}'142 }143 )144 if response['ResponseMetadata']['HTTPStatusCode'] != 201:145 raise RuntimeError146 return response147 def delete_gateway_response(self, response_type):148 try:149 response = self.client.delete_gateway_response(150 restApiId=self.aws['api_id'],...
test_apigateway_gatewayresponses.py
...5@mock_apigateway6def test_put_gateway_response_minimal():7 client = boto3.client("apigateway", region_name="us-east-2")8 api_id = client.create_rest_api(name="my_api", description="d")["id"]9 resp = client.put_gateway_response(restApiId=api_id, responseType="DEFAULT_4XX")10 resp.should.have.key("responseType").equals("DEFAULT_4XX")11 resp.should.have.key("defaultResponse").equals(False)12@mock_apigateway13def test_put_gateway_response():14 client = boto3.client("apigateway", region_name="us-east-2")15 api_id = client.create_rest_api(name="my_api", description="d")["id"]16 resp = client.put_gateway_response(17 restApiId=api_id,18 responseType="DEFAULT_4XX",19 statusCode="401",20 responseParameters={"gatewayresponse.header.Authorization": "'Basic'"},21 responseTemplates={22 "application/xml": "#set($inputRoot = $input.path('$'))\n{ }"23 },24 )25 resp.should.have.key("responseType").equals("DEFAULT_4XX")26 resp.should.have.key("defaultResponse").equals(False)27 resp.should.have.key("statusCode").equals("401")28 resp.should.have.key("responseParameters").equals(29 {"gatewayresponse.header.Authorization": "'Basic'"}30 )31 resp.should.have.key("responseTemplates").equals(32 {"application/xml": "#set($inputRoot = $input.path('$'))\n{ }"}33 )34@mock_apigateway35def test_get_gateway_response_minimal():36 client = boto3.client("apigateway", region_name="ap-southeast-1")37 api_id = client.create_rest_api(name="my_api", description="d")["id"]38 client.put_gateway_response(restApiId=api_id, responseType="DEFAULT_4XX")39 resp = client.get_gateway_response(restApiId=api_id, responseType="DEFAULT_4XX")40 resp.should.have.key("responseType").equals("DEFAULT_4XX")41 resp.should.have.key("defaultResponse").equals(False)42@mock_apigateway43def test_get_gateway_response():44 client = boto3.client("apigateway", region_name="us-east-2")45 api_id = client.create_rest_api(name="my_api", description="d")["id"]46 client.put_gateway_response(47 restApiId=api_id,48 responseType="DEFAULT_4XX",49 statusCode="401",50 responseParameters={"gatewayresponse.header.Authorization": "'Basic'"},51 responseTemplates={52 "application/xml": "#set($inputRoot = $input.path('$'))\n{ }"53 },54 )55 resp = client.get_gateway_response(restApiId=api_id, responseType="DEFAULT_4XX")56 resp.should.have.key("responseType").equals("DEFAULT_4XX")57 resp.should.have.key("defaultResponse").equals(False)58 resp.should.have.key("statusCode").equals("401")59 resp.should.have.key("responseParameters").equals(60 {"gatewayresponse.header.Authorization": "'Basic'"}61 )62 resp.should.have.key("responseTemplates").equals(63 {"application/xml": "#set($inputRoot = $input.path('$'))\n{ }"}64 )65@mock_apigateway66def test_get_gateway_response_unknown():67 client = boto3.client("apigateway", region_name="us-east-2")68 api_id = client.create_rest_api(name="my_api", description="d")["id"]69 with pytest.raises(ClientError) as exc:70 client.get_gateway_response(restApiId=api_id, responseType="DEFAULT_4XX")71 err = exc.value.response["Error"]72 err["Code"].should.equal("NotFoundException")73@mock_apigateway74def test_get_gateway_responses_empty():75 client = boto3.client("apigateway", region_name="ap-southeast-1")76 api_id = client.create_rest_api(name="my_api", description="d")["id"]77 resp = client.get_gateway_responses(restApiId=api_id)78 resp.should.have.key("items").equals([])79@mock_apigateway80def test_get_gateway_responses():81 client = boto3.client("apigateway", region_name="ap-southeast-1")82 api_id = client.create_rest_api(name="my_api", description="d")["id"]83 client.put_gateway_response(restApiId=api_id, responseType="DEFAULT_4XX")84 client.put_gateway_response(85 restApiId=api_id, responseType="DEFAULT_5XX", statusCode="503"86 )87 resp = client.get_gateway_responses(restApiId=api_id)88 resp.should.have.key("items").length_of(2)89 resp["items"].should.contain(90 {"responseType": "DEFAULT_4XX", "defaultResponse": False}91 )92 resp["items"].should.contain(93 {"responseType": "DEFAULT_5XX", "defaultResponse": False, "statusCode": "503"}94 )95@mock_apigateway96def test_delete_gateway_response():97 client = boto3.client("apigateway", region_name="ap-southeast-1")98 api_id = client.create_rest_api(name="my_api", description="d")["id"]99 client.put_gateway_response(restApiId=api_id, responseType="DEFAULT_4XX")100 client.put_gateway_response(101 restApiId=api_id, responseType="DEFAULT_5XX", statusCode="503"102 )103 resp = client.get_gateway_responses(restApiId=api_id)104 resp.should.have.key("items").length_of(2)105 resp = client.delete_gateway_response(restApiId=api_id, responseType="DEFAULT_5XX")106 resp = client.get_gateway_responses(restApiId=api_id)107 resp.should.have.key("items").length_of(1)108 resp["items"].should.contain(109 {"responseType": "DEFAULT_4XX", "defaultResponse": False}...
Check out the latest blogs from LambdaTest on this topic:
The fact is not alien to us anymore that cross browser testing is imperative to enhance your application’s user experience. Enhanced knowledge of popular and highly acclaimed testing frameworks goes a long way in developing a new app. It holds more significance if you are a full-stack developer or expert programmer.
QA testers have a unique role and responsibility to serve the customer. Serving the customer in software testing means protecting customers from application defects, failures, and perceived failures from missing or misunderstood requirements. Testing for known requirements based on documentation or discussion is the core of the testing profession. One unique way QA testers can both differentiate themselves and be innovative occurs when senseshaping is used to improve the application user experience.
Having a good web design can empower business and make your brand stand out. According to a survey by Top Design Firms, 50% of users believe that website design is crucial to an organization’s overall brand. Therefore, businesses should prioritize website design to meet customer expectations and build their brand identity. Your website is the face of your business, so it’s important that it’s updated regularly as per the current web design trends.
Enterprise resource planning (ERP) is a form of business process management software—typically a suite of integrated applications—that assists a company in managing its operations, interpreting data, and automating various back-office processes. The introduction of a new ERP system is analogous to the introduction of a new product into the market. If the product is not handled appropriately, it will fail, resulting in significant losses for the business. Most significantly, the employees’ time, effort, and morale would suffer as a result of the procedure.
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!!