"""Interface for the ows-notifications microservice.""" from ddtrace import tracer from oto import response from owsrequest import request from notifications_delivery import config from notifications_delivery.connectors import sentry from notifications_delivery.constants import error, ows_services @tracer.wrap(service='ows-notifications') def unsubscribe_user(user_info, feed_group): """Unsubscribe user from GetStream feed group (ows-notifications). Args: user_info (dict): the alw user info feed_group (str): the GetStream feed group Returns: None """ path = '/unsubscribe' user_id = user_info['user_id'] vendor_id = user_info['vendor_id'] headers = { 'Orchard-User-Id': 'alw:{user_id}'.format(user_id=user_id), 'Grass-Account-Id': str(vendor_id), 'Grass-Account-Type': 'vendor' } body = { 'user_feed_name': 'user_email_notifications', 'feed_name': feed_group, 'feed_id': 'vendor_{vendor_id}'.format(vendor_id=vendor_id)} result = request.process( headers=headers, application=ows_services.APPLICATION_NAME, environment=config.ENVIRONMENT, method=ows_services.METHOD_POST, service_name=ows_services.OWS_NOTIFICATIONS, path=path, json=body) if result.status_code != 200: sentry.send_response_to_sentry( result, error.ERROR_CODE_OWS_NOTIFICATIONS_REQUEST) return @tracer.wrap(service='ows-notifications') def get_vendor_subscriptions(vendor_id, feed_group): """Get subscriptions from GetStream (ows-notifications). Args: vendor_id (int): the vendor ID feed_group (str): the GetStream feed group Returns: response.Response: containing the subscriptions. """ path = ('/feed/subscribers?feed_name={feed_group}' '&feed_id=vendor_{feed_id}').format( feed_group=feed_group, feed_id=vendor_id) result = request.process( application=ows_services.APPLICATION_NAME, environment=config.ENVIRONMENT, method=ows_services.METHOD_GET, service_name=ows_services.OWS_NOTIFICATIONS, path=path) if result.status_code != 200: return response.create_error_response( status=result.status_code, code=error.ERROR_CODE_OWS_NOTIFICATIONS_REQUEST, message=result.text) return response.Response(result.json()) @tracer.wrap(service='ows-notifications') def user_subscription_toggle_off(identity_id, subscription_name): """Unsubscribe subscriptions for a given identity. This is equivalent to turning the toggle OFF from settings app. Args: identity_id (str): identity uuid subscription_name (str): name of the subscription Returns: None """ path = f'/identity/{identity_id}/subscriptions/{subscription_name}' headers = {'Orchard-Identity-Id': identity_id} body = {'follow_all_resources': False} result = request.process( headers=headers, application=ows_services.APPLICATION_NAME, environment=config.ENVIRONMENT, method=ows_services.METHOD_PUT, service_name=ows_services.OWS_NOTIFICATIONS, path=path, json=body) if result.status_code != 200: sentry.send_response_to_sentry( result, error.ERROR_CODE_OWS_NOTIFICATIONS_REQUEST) return