Best Python code snippet using localstack_python
parsers.py
Source:parsers.py
2 RestJSONParser, JSONParser, QueryParser, EC2QueryParser, \3 NoInitialResponseError, ResponseParserError, LOG, lowercase_dict4from .eventstream import AioEventStream5class AioRestXMLParser(RestXMLParser):6 def _create_event_stream(self, response, shape):7 parser = self._event_stream_parser8 name = response['context'].get('operation_name')9 return AioEventStream(response['body'], shape, parser, name)10class AioEC2QueryParser(EC2QueryParser):11 def _create_event_stream(self, response, shape):12 parser = self._event_stream_parser13 name = response['context'].get('operation_name')14 return AioEventStream(response['body'], shape, parser, name)15class AioQueryParser(QueryParser):16 def _create_event_stream(self, response, shape):17 parser = self._event_stream_parser18 name = response['context'].get('operation_name')19 return AioEventStream(response['body'], shape, parser, name)20class AioJSONParser(JSONParser):21 async def _do_parse(self, response, shape):22 parsed = {}23 if shape is not None:24 event_name = shape.event_stream_name25 if event_name:26 parsed = await self._handle_event_stream(response, shape, event_name)27 else:28 parsed = self._handle_json_body(response['body'], shape)29 self._inject_response_metadata(parsed, response['headers'])30 return parsed31 def _create_event_stream(self, response, shape):32 parser = self._event_stream_parser33 name = response['context'].get('operation_name')34 return AioEventStream(response['body'], shape, parser, name)35 async def _handle_event_stream(self, response, shape, event_name):36 event_stream_shape = shape.members[event_name]37 event_stream = self._create_event_stream(response, event_stream_shape)38 try:39 event = await event_stream.get_initial_response()40 except NoInitialResponseError:41 error_msg = 'First event was not of type initial-response'42 raise ResponseParserError(error_msg)43 parsed = self._handle_json_body(event.payload, shape)44 parsed[event_name] = event_stream45 return parsed46 # this is actually from ResponseParser however for now JSONParser is the47 # only class that needs this async48 async def parse(self, response, shape):49 LOG.debug('Response headers: %s', response['headers'])50 LOG.debug('Response body:\n%s', response['body'])51 if response['status_code'] >= 301:52 if self._is_generic_error_response(response):53 parsed = self._do_generic_error_parse(response)54 elif self._is_modeled_error_shape(shape):55 parsed = self._do_modeled_error_parse(response, shape)56 # We don't want to decorate the modeled fields with metadata57 return parsed58 else:59 parsed = self._do_error_parse(response, shape)60 else:61 parsed = await self._do_parse(response, shape)62 # We don't want to decorate event stream responses with metadata63 if shape and shape.serialization.get('eventstream'):64 return parsed65 # Add ResponseMetadata if it doesn't exist and inject the HTTP66 # status code and headers from the response.67 if isinstance(parsed, dict):68 response_metadata = parsed.get('ResponseMetadata', {})69 response_metadata['HTTPStatusCode'] = response['status_code']70 # Ensure that the http header keys are all lower cased. Older71 # versions of urllib3 (< 1.11) would unintentionally do this for us72 # (see urllib3#633). We need to do this conversion manually now.73 headers = response['headers']74 response_metadata['HTTPHeaders'] = lowercase_dict(headers)75 parsed['ResponseMetadata'] = response_metadata76 return parsed77class AioRestJSONParser(RestJSONParser):78 def _create_event_stream(self, response, shape):79 parser = self._event_stream_parser80 name = response['context'].get('operation_name')81 return AioEventStream(response['body'], shape, parser, name)82PROTOCOL_PARSERS = {83 'ec2': AioEC2QueryParser,84 'query': AioQueryParser,85 'json': AioJSONParser,86 'rest-json': AioRestJSONParser,87 'rest-xml': AioRestXMLParser,88}89class AioResponseParserFactory(ResponseParserFactory):90 def create_parser(self, protocol_name):91 parser_cls = PROTOCOL_PARSERS[protocol_name]92 return parser_cls(**self._defaults)...
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!!