Skip to content

Commit

Permalink
dotdigital settings
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jun 14, 2024
1 parent 63adc8b commit 5bc14d3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
27 changes: 23 additions & 4 deletions codeforlife/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
Dotdigital helpers.
"""

import os
import json
import logging
import typing as t
from dataclasses import dataclass

import requests
from django.conf import settings

from .types import JsonDict

Expand Down Expand Up @@ -72,7 +74,7 @@ def add_contact(
# pylint: enable=line-too-long

if auth is None:
auth = os.environ["DOTDIGITAL_AUTH"]
auth = settings.DOTDIGITAL_AUTH

contact: JsonDict = {"email": email.lower()}
if opt_in_type is not None:
Expand Down Expand Up @@ -123,6 +125,12 @@ def add_contact(
for preference in preferences
]

if not settings.DOTDIGITAL_ENABLED:
logging.info(
"Added contact to DotDigital:\n%s", json.dumps(body, indent=2)
)
return

response = requests.post(
# pylint: disable-next=line-too-long
url=f"https://{region}-api.dotdigital.com/v2/contacts/with-consent-and-preferences",
Expand Down Expand Up @@ -166,8 +174,12 @@ def remove_contact(
"""
# pylint: enable=line-too-long

if not settings.DOTDIGITAL_ENABLED:
logging.info("Removed contact from DotDigital: %s", contact_identifier)
return

if auth is None:
auth = os.environ["DOTDIGITAL_AUTH"]
auth = settings.DOTDIGITAL_AUTH

response = requests.get(
# pylint: disable-next=line-too-long
Expand Down Expand Up @@ -250,7 +262,7 @@ def send_mail(
# pylint: enable=line-too-long

if auth is None:
auth = os.environ["DOTDIGITAL_AUTH"]
auth = settings.DOTDIGITAL_AUTH

body = {
"campaignId": campaign_id,
Expand Down Expand Up @@ -282,6 +294,13 @@ def send_mail(
for attachment in attachments
]

if not settings.DOTDIGITAL_ENABLED:
logging.info(
"Sent a triggered email with DotDigital:\n%s",
json.dumps(body, indent=2),
)
return

response = requests.post(
url=f"https://{region}-api.dotdigital.com/v2/email/triggered-campaign",
json=body,
Expand Down
7 changes: 7 additions & 0 deletions codeforlife/settings/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@

# The api url of the current service.
SERVICE_API_URL = f"{SERVICE_BASE_URL}/api"

# The authorization bearer token used to authenticate with Dotdigital.
DOTDIGITAL_AUTH = os.getenv("DOTDIGITAL_AUTH", "REPLACE_ME")

# A global flag used to enabled/disable calls to Dotdigital's API.
# If disabled, API calls will be logged to the console instead.
DOTDIGITAL_ENABLED = bool(int(os.getenv("DOTDIGITAL_ENABLED", "0")))

0 comments on commit 5bc14d3

Please sign in to comment.