Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 7 additions & 28 deletions api/functions/external_graphql_api_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.")
4 changes: 2 additions & 2 deletions api/schemas/dmarc_report_summary_table/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 5 additions & 7 deletions api/schemas/dmarc_report_summary_table/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ def resolve_dmarc_report_summary_table(self, info, **kwargs):
thirtyDays: $thirtyDays
) {
period {
startDate
endDate
categoryTotals {
fullPass
passSpfOnly
Expand All @@ -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)
Expand All @@ -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,
)

Expand All @@ -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,
)
10 changes: 5 additions & 5 deletions api/tests/test_dmarc_report_summary_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down