"""ows-account connector.""" import stringcase from flask import g from owsrequest import flask_request from owsresponse import response from permissions import config from permissions.connectors import sentry from permissions.constants import constants from permissions.types import Tenant def get_enabled_tenant_applications(tenant: Tenant) -> list[dict]: """Get all applications for a tenant. Args: tenant (Tenant): Tenant object to get type and uuid. Returns: Response: containing dict of applications and roles """ tenant_type = stringcase.spinalcase(tenant.tenant_type) tenant_uuid = tenant.tenant_uuid if tenant_type == constants.ACCOUNT_TENANT_TYPE: # TODO: allow that endpoint to accept /account//applications tenant_type = constants.VENDOR_RESOURCE_TYPE.lower() resource_url = f'/{tenant_type}/{tenant_uuid}/applications' headers = { 'content-type': 'application/json', 'Orchard-Profile-Type': g.request_context.profile_type, 'Orchard-Profile-Id': g.request_context.profile_id, 'Orchard-Identity-Id': g.request_context.jwt_identity_id, } if config.ENVIRONMENT == config.DEV_ENVIRONMENT: headers['authorization'] = g.request_context.authorization res = flask_request.process( application=config.SERVICE_NAME, environment=config.ENVIRONMENT, method='GET', service_name='ows-account', path=resource_url, uwsgi_cache_enabled=True, headers=headers, ) if res.status_code != 200: g.log.error( 'Unable to get enabled tenant applications.', resources={'tenant_type': tenant_type, 'tenant_uuid': tenant_uuid}, ) error = {'error': res.raise_for_status()} sentry.send_response_to_sentry( response.create_error_response( status=res.status_code, code='Unable to get enabled tenant applications.', message=error, ), res.status_code, ) return res.json().get('applications', [])