""" Interface to the ows-contracts microservice. The model is responsible to make a call to a ows-contracts microservice. It makes a request to ows-contracts endpoints ('/booked/vendor/', methods=['GET']) or ('/booked/subaccount/', methods=['GET']) and returns a result of the request. """ from urllib.parse import urlencode from owsrequest import request from ows_accounting import response from ows_accounting.constants import error from ows_accounting.constants import header from ows_accounting.utils import format CONTRACTS_SERVICE = 'ows-contracts' CONTRACTS_BOOKED_ACCOUNT_RESOURCE = ( '/booked/{account_type}/{account_id}?{params}') def get_booked_contracts( account_type, account_id, page_offset=None, page_limit=None, sort_order=None): """Get account booked contracts by account id from ows-contracts. Args: account_type (str): Account type (vendor or subaccount) account_id (int): Account Id page_offset (int): Count of contract records to skip Returns: response.Response """ account_type_error = account_type not in [ header.GRASS_ACCOUNT_TYPE_VENDOR, header.GRASS_ACCOUNT_TYPE_SUBACCOUNT] if account_type_error: return response.create_error_response( error.ERROR_CODE_INVALID_REQUEST, error.ERROR_MESSAGE_INVALID_ACCOUNT) params_dict = { 'page_offset': page_offset, 'page_limit': page_limit, 'sort_order': sort_order } params = urlencode({k: v for k, v in params_dict.items() if v is not None}) resource = CONTRACTS_BOOKED_ACCOUNT_RESOURCE.format( account_type=account_type, account_id=account_id, params=params) contracts_response = request.get(CONTRACTS_SERVICE, resource) message = format.get_response_json(contracts_response) if (contracts_response.status_code == 200): return response.Response(message=message) return response.create_error_response( code=error.ERROR_CODE_OWS_CONTRACTS, message=message, status=contracts_response.status_code)