"""Get Sales Goals and Marketing Highlights.""" import json from oto import response from owsrequest import request from daemonJwtHandler import daemon_handler from requests.exceptions import ConnectionError from salessheets import config from salessheets.connectors.sentry import sentry_capture_message, sentry_capture_exception from salessheets.constants import ows_services def get_marketing_highlights_for_project(project_id): """Get marketing highlights for project.""" service = ows_services.OWS_SALES_GOALS method = 'get' path_template = '/marketing_highlights/{project_id}' path = path_template.format(project_id=project_id) try: authorization = daemon_handler.daemon_handler( ows_services.OWS_SALES_GOALS) sales_goals_response = request.process( application=ows_services.APPLICATION_NAME, environment=config.ENVIRONMENT, service_name=service, method=method, path=path, authorization_header=authorization, isDaemon=True) except ConnectionError as ce: if sentry_capture_exception: sentry_capture_exception() return response.create_fatal_response(str(ce)) status = sales_goals_response.status_code try: message = sales_goals_response.json() except ValueError: message = None if status not in [200, 404] or not message: error_template = ( '{service} {method} {path} returned status: {status}' ' message: {message}') error_message = error_template.format( service=service, method='GET', path=path, status=status, message=json.dumps(message or sales_goals_response.text)) if sentry_capture_message: sentry_capture_message(error_message) return response.create_fatal_response(error_message) return response.Response(status=status, message=message)