def __execute(self, func, **kwargs): try: path_ext = kwargs.pop('path_extension') if path_ext: endpoint_url = '{}/{}'.format(self.url.rstrip('/'), path_ext.lstrip('/')) else: endpoint_url = self.url response = func(endpoint_url, **kwargs) if 200 <= response.status_code <= 299: try: response_object = response.json() except ValueError: response_object = {'result': response.text} if '_type' in response_object and response_object.get('_type') not in ['OperationResult', 'ErrorMessage']: type_class = _get_custom_class(response_object.pop('_type')) return response.status_code, Model.from_dict(response_object, type_class) else: return response.status_code, response_object except Exception as exc: raise RequestHandlingException(500, str(exc)) else: content = response.json() if '_type' in content and content.get('_type') == MessageType.ErrorMessage.name: msg = content.get('message') upstream = content.get('upstream_service', self.url.rstrip('/').split('/').pop()) exc = RequestHandlingException(response.status_code, msg) exc.upstream_service = upstream raise exc else: raise RequestHandlingException(response.status_code, 'Error while calling service.')
def post(self, request_object: Model, stream: bool = False, timeout: int = 3): headers = {} if request: auth_header = request.headers.get('Authorization') if auth_header: headers.update(Authorization=auth_header) accept_lang = request.accept_languages if request else 'en' headers['Accept-Language'] = accept_lang.best response = requests.post(self.url, data=request_object.dumps(), stream=stream, headers=headers, timeout=timeout) return response.status_code, Model.to_dict(response.json())
def test_put(): adapter.register_uri('PUT', 'mock://test.com/orders/12345', status_code=200, text=create_order().dumps()) rsp_code, order = client.orders.put(create_operation_result(), path_extension='12345') print(f' received order {rsp_code} >>> {order.dumps(pretty_print=True)}') assert hasattr(order, 'delivery_address') assert hasattr(order, 'payment_method') assert '_type' in Model.to_dict(order)
def test_service_get(): adapter.register_uri('GET', 'mock://test.com/orders/12345', status_code=200, text=create_order().dumps()) rsp_code, rcvd_order = client.orders.get(path_extension='12345') print( f' received order {rsp_code} >>> {rcvd_order.dumps(pretty_print=True)}' ) assert hasattr(rcvd_order, 'delivery_address') assert hasattr(rcvd_order, 'payment_method') assert '_type' in Model.to_dict(rcvd_order) assert len(rcvd_order.products) > 0
def check_portfolio(portfolio): portfolio_dict = Model.to_dict(portfolio, convert_id=True) stocks = portfolio_dict.get('stocks') assert len(stocks) == 2 msft_stock = list( [stock for stock in stocks if stock.get('code') == 'MSFT'])[0] msft_stock_history = msft_stock.get('history') assert len(msft_stock_history) assert 120 in msft_stock_history assert 120.5 in msft_stock_history assert 123.9 in msft_stock_history assert msft_stock.get('open') == 123.7 assert '_id' in portfolio_dict assert 'id' not in portfolio_dict assert '_id' in portfolio_dict.get('owner') assert 'id' not in portfolio_dict.get('owner')
def __init__(self, **kwargs): Model.init_model(self, **kwargs)