"""Interface to the ows-contracts microservice.""" from oto import response from owsrequest import request from backend.constants import error from backend.constants import services def is_mech_admin(account_type, account_id): """Check whether user with provided Grass credentials is mechadmin. ows-contracts returns 200 if user is mechadmin, 204 if not. Args: account_type (str): Grass-Account-Type header account_id (str): Grass-Account-Id header Returns: oto.response.Response """ mech_admin_url = '/{0}/{1}/mechadmin'.format(account_type, account_id) contracts_response = request.head(services.OWS_CONTRACTS, mech_admin_url) if contracts_response.status_code not in [200, 204]: try: errors_data = contracts_response.json() except Exception: errors_data = { 'code': error.OWS_CONTRACTS_ERROR_CODE, 'message': contracts_response.content.decode() } return response.create_error_response( status=contracts_response.status_code, **errors_data) mech_admin_status_codes = {200: True, 204: False} result = mech_admin_status_codes[contracts_response.status_code] return response.Response(message={'is_mech_admin': result})