diff --git a/api/functions/external_graphql_api_request.py b/api/functions/external_graphql_api_request.py index b57e1d2c30..ba0bc30ba1 100644 --- a/api/functions/external_graphql_api_request.py +++ b/api/functions/external_graphql_api_request.py @@ -8,10 +8,7 @@ from app import logger -def create_transport( - api_domain=os.getenv("DMARC_REPORT_API_URL"), - auth_token=os.getenv("DMARC_REPORT_API_TOKEN"), -) -> RequestsHTTPTransport: +def create_transport(api_domain, auth_token) -> RequestsHTTPTransport: """ This function creates the transport object to send requests to an external API @@ -27,10 +24,7 @@ def create_transport( return transport -def create_client( - api_domain=os.getenv("DMARC_REPORT_API_URL"), - auth_token=os.getenv("DMARC_REPORT_API_TOKEN"), -) -> Client: +def create_client(api_domain, auth_token) -> Client: """ This function is used to create the client that will execute the query :param api_domain: External API URL used to create the transport object @@ -71,23 +65,8 @@ def send_request( return data except Exception as e: - # Make sure the below stays like so - # error_str = e.__str__().replace("\'", '\"') - # Black will try and change it and it will break !!! - error_str = e.__str__().replace("\'", '\"') - try: - error_dict = json.loads(error_str) - if error_dict.get("message", None): - logger.error( - f"Error occurred on the dmarc-report-api side: {str(error_dict.get('message'))}" - ) - if summary_table: - return {} - else: - raise GraphQLError("Error when querying dmarc-report-api.") - - except ValueError as ve: - logger.error( - f"Value Error occurred when receiving data from dmarc-report-api: {str(ve)}" - ) - raise GraphQLError("Error, when querying dmarc-report-api.") + logger.error(f"Error occurred on the dmarc-report-api side: {str(e)}") + if summary_table: + return {} + else: + raise GraphQLError("Error when querying dmarc-report-api.") diff --git a/api/schemas/dmarc_report_summary_table/__init__.py b/api/schemas/dmarc_report_summary_table/__init__.py index fd4437a960..3a6ed6fd06 100644 --- a/api/schemas/dmarc_report_summary_table/__init__.py +++ b/api/schemas/dmarc_report_summary_table/__init__.py @@ -31,12 +31,12 @@ period=graphene.Argument( PeriodEnums, description="The period in which the returned data is relevant to.", - required=False, + required=True, ), year=graphene.Argument( Year, description="The year in which the returned data is relevant to.", - required=False, + required=True, ), description="Query for creating domain summary table.", resolver=resolve_demo_dmarc_report_summary_table, diff --git a/api/schemas/dmarc_report_summary_table/resolver.py b/api/schemas/dmarc_report_summary_table/resolver.py index 640581b89d..75004b1182 100644 --- a/api/schemas/dmarc_report_summary_table/resolver.py +++ b/api/schemas/dmarc_report_summary_table/resolver.py @@ -89,8 +89,6 @@ def resolve_dmarc_report_summary_table(self, info, **kwargs): thirtyDays: $thirtyDays ) { period { - startDate - endDate categoryTotals { fullPass passSpfOnly @@ -105,7 +103,6 @@ def resolve_dmarc_report_summary_table(self, info, **kwargs): # Send request data = send_request(query=query, variables=variables, summary_table=True,) - temp_dict = data.get("getDmarcSummaryByPeriod", {}).get("period", {}) temp_dict.update({"domain": domain.domain}) data_list.append(temp_dict) @@ -121,11 +118,12 @@ def resolve_dmarc_report_summary_table(self, info, **kwargs): logger.info( f"User: {user_id} successfully retrieved the DmarcReportSummaryTable information for all their domains." ) + return DmarcReportSummaryTable( # Get Month Name - calendar.month_name[int(data_list[0].get("endDate")[5:7].lstrip("0"))], + datetime.strptime(period.lower(), "%b").strftime("%B"), # Get Year - data_list[0].get("endDate")[0:4].lstrip("0"), + year, data_list, ) @@ -146,8 +144,8 @@ def resolve_demo_dmarc_report_summary_table(self, info, **kwargs): return DmarcReportSummaryTable( # Get Month Name - calendar.month_name[int(data_list[0].get("endDate")[5:7].lstrip("0"))], + datetime.strptime(period.lower(), "%b").strftime("%B"), # Get Year - data_list[0].get("endDate")[0:4].lstrip("0"), + year, data_list, ) diff --git a/api/tests/test_dmarc_report_summary_table.py b/api/tests/test_dmarc_report_summary_table.py index f2e10c7010..9422121188 100644 --- a/api/tests/test_dmarc_report_summary_table.py +++ b/api/tests/test_dmarc_report_summary_table.py @@ -79,7 +79,7 @@ def test_valid_get_dmarc_report_summary_table_query_as_super_admin( query=""" query { dmarcReportSummaryTable ( - period: AUGUST + period: MAY year: "2020" ) { month @@ -170,7 +170,7 @@ def test_valid_get_dmarc_report_summary_table_query_as_org_admin(save, mocker, c query=""" query { dmarcReportSummaryTable ( - period: AUGUST + period: MAY year: "2020" ) { month @@ -253,7 +253,7 @@ def test_valid_get_dmarc_report_summary_table_query_as_user_write(save, mocker, query=""" query { dmarcReportSummaryTable ( - period: AUGUST + period: MAY year: "2020" ) { month @@ -336,7 +336,7 @@ def test_valid_get_dmarc_report_summary_table_query_as_user_read(save, mocker, c query=""" query { dmarcReportSummaryTable ( - period: AUGUST + period: MAY year: "2020" ) { month @@ -418,7 +418,7 @@ def test_dmarc_report_summary_to_ensure_error_occurs_when_no_domains_exist( query=""" query { dmarcReportSummaryTable ( - period: AUGUST + period: MAY year: "2020" ) { month