"""Interface for the ows-notifications microservice.""" from owsrequest import request as requests from owsrequest.constants import headers as header_constants from owsresponse import response from users.constants import CONTENT_TYPE, ERROR_CODE_OWS_NOTIFICATIONS_REQUEST, OWS_NOTIFICATIONS def create_notification_subscription( profile_id, profile_type, notification_type, feed_type, undelete, correlation_id ): """Create subscription for notifications. Args: profile_id (int): Profile to add subscription to profile_type (str): Type of profile notification_type (str): Notification method feed_type (str): Category of notification undelete (bool): Re-activate deleted subscription correlation_id (str): the correlation ID. """ path = '/notifications/subscribe' headers = { header_constants.ORCHARD_PROFILE_ID: str(profile_id), header_constants.ORCHARD_PROFILE_TYPE: profile_type, 'Content-Type': CONTENT_TYPE, } result = requests.post( OWS_NOTIFICATIONS, path, params={'undelete': str(undelete).lower()}, json={'feed_type': feed_type, 'notification_type': notification_type}, headers=headers, correlation_id=correlation_id, ) if result.status_code in [404, 409, 201, 200]: return response.Response(status=200) return response.create_error_response( code=ERROR_CODE_OWS_NOTIFICATIONS_REQUEST, message=f'Unexpected HTTP code {result.status_code} creating profile subscription {profile_type}:{profile_id}', # noqa: E501, ) def unsubscribe_label(profile_id, profile_type, vendor_id, subaccount_id=None): """Unsubscribe from HAS_FOLLOWED relationship. Args: profile_id (int): Profile to add subscription to profile_type (str): Type of profile vendor_id (int): Vendor id subaccount_id (int): Subaccount id """ entity_type = 'sub_account' if subaccount_id else 'vendor' entity_id = subaccount_id if subaccount_id else vendor_id headers = { header_constants.ORCHARD_PROFILE_ID: str(profile_id), header_constants.ORCHARD_PROFILE_TYPE: profile_type, 'Content-Type': CONTENT_TYPE, } result = requests.delete( OWS_NOTIFICATIONS, f'/subscription/{entity_type}/{entity_id}/relationship/HAS_FOLLOWED', headers=headers, ) if result.status_code in [404, 409, 201, 200]: return response.Response(status=200) return response.create_error_response( code=ERROR_CODE_OWS_NOTIFICATIONS_REQUEST, message=f'Error {result.status_code} unsubscribeLabel {profile_type}:{profile_id}', ) def subscribe_label(profile_id, profile_type, vendor_id, subaccount_id=None, automatic=False): """Subscribe via HAS_FOLLOWED relationship. Args: profile_id (int): Profile to add subscription to profile_type (str): Type of profile vendor_id (int): Vendor id subaccount_id (int): Subaccount id automatic (bool): automatic subscription """ entity_type = 'sub_account' if subaccount_id else 'vendor' entity_id = subaccount_id if subaccount_id else vendor_id params = {'id': entity_id, 'relationship': 'HAS_FOLLOWED', 'automatic': automatic} headers = { header_constants.ORCHARD_PROFILE_ID: str(profile_id), header_constants.ORCHARD_PROFILE_TYPE: profile_type, 'Content-Type': CONTENT_TYPE, } result = requests.post( OWS_NOTIFICATIONS, f'/subscription/{entity_type}', json=params, headers=headers, ) if result.status_code in [404, 409, 201, 200]: return response.Response(status=200) return response.create_error_response( code=ERROR_CODE_OWS_NOTIFICATIONS_REQUEST, message=f'Error {result.status_code} subscribeLabel {profile_type}:{profile_id}', )