def __call__(self, _token=None, **op_kwargs): """Invoke the actual HTTP request and return a future. :rtype: :class:`bravado.http_future.HTTPFuture` """ warn_for_deprecated_op(self.operation) # Apply request_options defaults request_options = op_kwargs.pop('_request_options', {}) request_config = RequestConfig(request_options, also_return_response_default=True) request_params = construct_request( self.operation, request_options, **op_kwargs) # config = self.operation.swagger_spec.config http_client = self.operation.swagger_spec.http_client # Per-request config overrides client wide config # also_return_response = request_options.get( # 'also_return_response', # config['also_return_response']) # Check if we need an authorization token and if so set it up if self.require_authorization and _token is None: raise ESIAuthorizationError('Missing required authorization token') return http_client.request( request_params, operation=self.operation, request_config=request_config, authorization_token=_token)
def mock_metadata(): return BravadoResponseMetadata( incoming_response=IncomingResponse(), swagger_result=None, start_time=5, request_end_time=6, handled_exception_info=None, request_config=RequestConfig({}, also_return_response_default=False), )
def __init__(self, future, response_adapter, operation=None, request_config=None): self._start_time = monotonic.monotonic() self.future = future self.response_adapter = response_adapter self.operation = operation self.request_config = request_config or RequestConfig( {}, also_return_response_default=False, )
def test_no_force_fallback_result_if_disabled(http_future, mock_operation, mock_incoming_response, fallback_result): http_future.request_config = RequestConfig( {'force_fallback_result': True}, also_return_response_default=False, ) mock_operation.swagger_spec.config = { 'bravado': bravado_config_from_config_dict({'disable_fallback_results': True}), } with mock.patch('bravado.http_future.unmarshal_response', autospec=True): response = http_future.response(fallback_result=lambda e: fallback_result) assert response.result == mock_incoming_response.swagger_result
def test_empty_request_config(): request_config = RequestConfig({}, also_return_response_default=False) _assert_request_config_equals( request_config, also_return_response=False, force_fallback_result=False, response_callbacks=[], connect_timeout=None, headers={}, use_msgpack=False, timeout=None, additional_properties={}, )
def test_response_metadata_times(): with mock.patch('monotonic.monotonic', return_value=11): metadata = BravadoResponseMetadata( incoming_response=None, swagger_result=None, start_time=5, request_end_time=10, handled_exception_info=None, request_config=RequestConfig({}, also_return_response_default=False), ) # type: BravadoResponseMetadata[None] assert metadata.elapsed_time == 6 assert metadata.request_elapsed_time == 5
def test_force_fallback_result(mock_operation, fallback_result, http_future): http_future.request_config = RequestConfig( {'force_fallback_result': True}, also_return_response_default=False, ) mock_operation.swagger_spec.config = { 'bravado': bravado_config_from_config_dict({}) } with mock.patch('bravado.http_future.unmarshal_response', autospec=True): response = http_future.response(fallback_result=lambda e: fallback_result) assert response.result == fallback_result assert response.metadata.is_fallback_result is True assert response.metadata.handled_exception_info[0] is ForcedFallbackResultError
def __init__(self, result, metadata=None): # type: (T, typing.Optional[BravadoResponseMetadata[T]]) -> None self._result = result if metadata: self._metadata = metadata else: self._metadata = BravadoResponseMetadata( incoming_response=IncomingResponseMock(status_code=200), swagger_result=self._result, start_time=1528733800, request_end_time=1528733801, handled_exception_info=None, request_config=RequestConfig( {}, also_return_response_default=False), )
def __init__( self, future, # type: FutureAdapter response_adapter, # type: typing.Callable[[typing.Any], IncomingResponse] operation=None, # type: typing.Optional[Operation] request_config=None, # type: typing.Optional[RequestConfig] ): # type: (...) -> None self._start_time = monotonic.monotonic() self.future = future self.response_adapter = response_adapter self.operation = operation self.request_config = request_config or RequestConfig( {}, also_return_response_default=False, )
def __init__(self, exception=BravadoTimeoutError(), metadata=None): # type: (BaseException, typing.Optional[BravadoResponseMetadata]) -> None self._exception = exception if metadata: self._metadata = metadata else: self._metadata = BravadoResponseMetadata( incoming_response=IncomingResponse(), swagger_result=None, # we're going to set it later start_time=1528733800, request_end_time=1528733801, handled_exception_info=[ self._exception.__class__, self._exception, 'Traceback' ], request_config=RequestConfig( {}, also_return_response_default=False), )
def test_also_return_response_true(_, mock_future_adapter): # Verify HTTPFuture(..., also_return_response=True).result() # returns the (swagger_result, http_response) and not just swagger_result response_adapter_instance = Mock(spec=IncomingResponse, status_code=200, swagger_result='hello world') response_adapter_type = Mock(return_value=response_adapter_instance) http_future = HttpFuture(future=mock_future_adapter, response_adapter=response_adapter_type, operation=Mock(spec=Operation), request_config=RequestConfig( {}, also_return_response_default=True)) swagger_result, http_response = http_future.result() assert http_response == response_adapter_instance assert swagger_result == 'hello world'
def test_request_config_all_values(): request_options = { 'also_return_response': True, 'force_fallback_result': True, 'response_callbacks': [lambda ir, op: None], 'connect_timeout': 0.2, 'headers': { 'X-Speed-Up': '1' }, 'use_msgpack': True, 'timeout': 2, 'http_client_option': 'a value', } request_config = RequestConfig(request_options, also_return_response_default=False) request_options['additional_properties'] = { 'http_client_option': request_options.pop('http_client_option') } _assert_request_config_equals(request_config, **request_options)
def __call__(self, **op_kwargs): """Invoke the actual HTTP request and return a future. :rtype: :class:`bravado.http_future.HTTPFuture` """ log.debug(u'%s(%s)', self.operation.operation_id, op_kwargs) warn_for_deprecated_op(self.operation) # Get per-request config request_options = op_kwargs.pop('_request_options', {}) request_config = RequestConfig(request_options, self.also_return_response) request_params = construct_request( self.operation, request_options, **op_kwargs) http_client = self.operation.swagger_spec.http_client return http_client.request( request_params, operation=self.operation, request_config=request_config, )