def encode_and_send(self, container_name, access_key, raise_for_status, encoder, encoder_args, output=None): # get request params with the encoder method, path, headers, body = encoder(container_name, access_key, encoder_args) # call the encoder to get the response http_response = self._http_request(method, path, headers, body) # create a response response = v3io.dataplane.response.Response(output, http_response.status_code, headers, http_response.text) # if user didn't specify never to raise, raise for the given statuses if raise_for_status != RaiseForStatus.never: response.raise_for_status(raise_for_status) return response
def wait_response(self, request, raise_for_status=None, num_retries=1): connection_idx = request.transport.connection_idx connection = self._connections[connection_idx] while True: try: # read the response response = connection.getresponse() response_body = response.read() status_code, headers = self._get_status_and_headers(response) response = v3io.dataplane.response.Response( request.output, status_code, headers, response_body) # enforce raise for status response.raise_for_status(request.raise_for_status or raise_for_status) # return the response return response except self._remote_disconnect_exception as e: if num_retries == 0: raise e num_retries -= 1 # create a connection connection = self._recreate_connection_at_index(connection_idx) # re-send the request on the connection request = self._send_request_on_connection( request, connection_idx)
def wait_response(self, request, raise_for_status=None, num_retries=1): connection = request.transport.connection_used while True: try: # read the response response = connection.getresponse() response_body = response.read() status_code, headers = self._get_status_and_headers(response) self.log('Rx', connection=connection, status_code=status_code, body=response_body) response = v3io.dataplane.response.Response( request.output, status_code, headers, response_body) # enforce raise for status response.raise_for_status(request.raise_for_status or raise_for_status) # return the response return response except self._wait_response_exceptions as e: if num_retries == 0: self._logger.warn_with( 'Remote disconnected while waiting for response and ran out of retries', e=type(e), connection=connection) raise e self._logger.debug_with( 'Remote disconnected while waiting for response', retries_left=num_retries, connection=connection) num_retries -= 1 # make sure connections is closed (connection.connect is called automaticly when connection is closed) connection.close() # re-send the request on the connection request = self._send_request_on_connection(request, connection) except v3io.dataplane.response.HttpResponseError as response_error: self._logger.warn_with('Response error: {}'.format( str(response_error))) raise response_error except BaseException as e: self._logger.warn_with( 'Unhandled exception while waiting for response', e=type(e), connection=connection) raise e finally: self._free_connections.put(connection, block=True)
def wait_response(self, request, raise_for_status=None, num_retries=1): connection_idx = request.transport.connection_idx connection = self._connections[connection_idx] while True: try: # read the response response = connection.getresponse() response_body = response.read() status_code, headers = self._get_status_and_headers(response) self.log('Rx', connection_idx=connection_idx, status_code=status_code, body=response_body) response = v3io.dataplane.response.Response( request.output, status_code, headers, response_body) # enforce raise for status response.raise_for_status(request.raise_for_status or raise_for_status) # return the response return response except self._wait_response_exceptions as e: if num_retries == 0: self._logger.warn_with( 'Remote disconnected while waiting for response and ran out of retries', e=type(e), connection_idx=connection_idx) raise e self._logger.debug_with( 'Remote disconnected while waiting for response', retries_left=num_retries, connection_idx=connection_idx) num_retries -= 1 # create a connection connection = self._recreate_connection_at_index(connection_idx) # re-send the request on the connection request = self._send_request_on_connection( request, connection_idx) except BaseException as e: self._logger.warn_with( 'Unhandled exception while waiting for response', e=type(e), connection_idx=connection_idx) raise e
async def request(self, container, access_key, raise_for_status, encoder, encoder_args, output=None): # allocate a request request = v3io.dataplane.request.Request(container, access_key, raise_for_status, encoder, encoder_args, output) path = request.encode_path() self.log('Tx', method=request.method, path=path, headers=request.headers, body=request.body) client_os_error_retry_counter = 0 while (True): try: # call the encoder to get the response async with self._client_session.request(request.method, self._endpoint + '/' + path, headers=request.headers, data=request.body, ssl=False) as http_response: # get contents contents = await http_response.content.read() # create a response response = v3io.dataplane.response.Response(output, http_response.status, http_response.headers, contents) # enforce raise for status response.raise_for_status(request.raise_for_status or raise_for_status) self.log('Rx', status_code=response.status_code, headers=response.headers, body=contents) return response except v3io.dataplane.response.HttpResponseError as response_error: self._logger.warn_with('Response error: {}'.format(str(response_error))) raise response_error except aiohttp.ClientOSError: client_os_error_retry_counter+=1 if (client_os_error_retry_counter == len(self.retry_intervals)): raise await asyncio.sleep(self.retry_intervals[client_os_error_retry_counter])
def wait_response(self, request, raise_for_status=None): # create a response response = v3io.dataplane.response.Response( request.output, request.transport.http_response.status_code, request.headers, request.transport.http_response.content) # enforce raise for status response.raise_for_status(request.raise_for_status or raise_for_status) return response
async def request(self, container, access_key, raise_for_status, encoder, encoder_args, output=None): # allocate a request request = v3io.dataplane.request.Request(container, access_key, raise_for_status, encoder, encoder_args, output) path = request.encode_path() self.log('Tx', method=request.method, path=path, headers=request.headers, body=request.body) # call the encoder to get the response async with self._client_session.request(request.method, self._endpoint + '/' + path, headers=request.headers, data=request.body, ssl=False) as http_response: # get contents contents = await http_response.content.read() # create a response response = v3io.dataplane.response.Response(output, http_response.status, http_response.headers, contents) # enforce raise for status response.raise_for_status(request.raise_for_status or raise_for_status) self.log('Rx', status_code=response.status_code, headers=response.headers, body=contents) return response