Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
irfanuddinahmad committed Mar 9, 2021
1 parent db7b813 commit f7791f1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 42 deletions.
75 changes: 38 additions & 37 deletions ecommerce_worker/braze/v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import absolute_import

import json
import requests

from urllib.parse import urlencode, urljoin
Expand Down Expand Up @@ -150,8 +151,9 @@ def __create_post_request(self, body, endpoint):
Returns:
response (dict): The response object
"""
message_body = json.dumps(body)
response = {'errors': []}
r = self._post_request(body, endpoint)
r = self._post_request(message_body, endpoint)
response.update(r.json())
response['status_code'] = r.status_code

Expand All @@ -177,7 +179,7 @@ def _post_request(self, body, endpoint):
self.session.headers.update(
{"Authorization": u"Bearer {}".format(self.rest_api_key), "Content-Type": "application/json"}
)
r = self.session.post(urljoin(self.rest_api_url, endpoint), json=body, timeout=2)
r = self.session.post(urljoin(self.rest_api_url, endpoint), data=body, timeout=2)
if r.status_code == 429:
reset_epoch_s = float(r.headers.get("X-RateLimit-Reset", 0))
raise BrazeRateLimitError(reset_epoch_s)
Expand Down Expand Up @@ -258,27 +260,27 @@ def create_braze_alias(self, recipient_emails):
for recipient_email in recipient_emails:
if not self.get_braze_external_id(recipient_email):
user_alias = {
"alias_name": "Enterprise",
"alias_label": recipient_email
'alias_name': 'Enterprise',
'alias_label': recipient_email
}
user_aliases.append(user_alias)
attribute = {
"user_alias": {
"alias_name": "Enterprise",
"alias_label": recipient_email
'user_alias': {
'alias_name': 'Enterprise',
'alias_label': recipient_email
},
"email": recipient_email,
"is_enterprise_learner": "true"
'email': recipient_email,
'is_enterprise_learner': 'true'
}
attributes.append(attribute)

alias_message = {
"user_aliases": user_aliases,
'user_aliases': user_aliases,
}
self.__create_post_request(alias_message, self.new_alias_endpoint)

attribute_message = {
"attributes": attributes
'attributes': attributes
}
self.__create_post_request(attribute_message, self.users_track_endpoint)

Expand Down Expand Up @@ -332,21 +334,21 @@ def send_message(
external_ids.append(str(external_id))
else:
user_alias = {
"alias_name": "Enterprise",
"alias_label": email_id
'alias_name': 'Enterprise',
'alias_label': email_id
}
user_aliases.append(user_alias)
email = {
"app_id": self.webapp_api_key,
"subject": subject,
"from": sender_alias + self.from_email,
"body": body,
'app_id': self.webapp_api_key,
'subject': subject,
'from': sender_alias + self.from_email,
'body': body,
}
message = {
"user_aliases": user_aliases,
"external_user_ids": external_ids,
"messages": {
"email": email
'user_aliases': user_aliases,
'external_user_ids': external_ids,
'messages': {
'email': email
}
}
return self.__create_post_request(message, self.messages_send_endpoint)
Expand All @@ -367,7 +369,7 @@ def did_email_bounce(
if not email_id:
raise BrazeClientError('Missing parameters for Braze email')
parameters = {
"email": email_id
'email': email_id
}

response = self.__create_get_request(parameters, self.email_bounce_endpoint)
Expand Down Expand Up @@ -431,21 +433,21 @@ def send_campaign_message(
raise BrazeClientError('Missing parameters for Braze email')
result = {}
for email_id in email_ids:
send_to_existing_only = "true" if self.get_braze_external_id(email_id) else "false"
send_to_existing_only = True if self.get_braze_external_id(email_id) else False
message = {
"campaign_id": campaign_id,
"recipients": [
'campaign_id': campaign_id,
'recipients': [
{
"external_user_id": "Enterprise-{}".format(email_id),
"trigger_properties": {
"sender_alias": sender_alias,
"subject": subject,
"body": body
'external_user_id': 'Enterprise-{}'.format(email_id),
'trigger_properties': {
'sender_alias': sender_alias,
'subject': subject,
'body': body
},
"send_to_existing_only": send_to_existing_only,
"attributes":
'send_to_existing_only': send_to_existing_only,
'attributes':
{
"email": email_id
'email': email_id
}
}
]
Expand All @@ -470,12 +472,11 @@ def get_braze_external_id(
if not email_id:
raise BrazeClientError('Missing parameters for Braze account check.')
message = {
"email_address": email_id,
"fields_to_export": ["external_id", "email"]
'email_address': email_id,
'fields_to_export': ['external_id']
}
response = self.__create_post_request(message, self.export_id_endpoint)

if response["users"]:
if response["users"] and 'external_id' in response["users"][0]:
return response["users"][0]["external_id"]

return None
2 changes: 1 addition & 1 deletion ecommerce_worker/configuration/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
'EXPORT_ID_ENDPOINT': '/users/export/ids',
'CAMPAIGN_SEND_ENDPOINT': '/campaigns/trigger/send',
'FROM_EMAIL': '<edx-for-business-no-reply@info.edx.org>',
'OFFER_CAMPAIGN_ID': '',
'ENTERPRISE_CAMPAIGN_ID': '',
# Retry settings for Braze celery tasks
'BRAZE_RETRY_SECONDS': 3600,
'BRAZE_RETRY_ATTEMPTS': 6,
Expand Down
12 changes: 8 additions & 4 deletions ecommerce_worker/sailthru/v1/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,10 @@ def _send_offer_assignment_email_via_braze(self, user_email, offer_assignment_id
"""
config = get_braze_configuration(site_code)
try:
user_emails = [user_email]
braze_client = get_braze_client(site_code)
response = braze_client.send_message(
email_ids=list(user_email),
email_ids=user_emails,
subject=subject,
body=email_body,
sender_alias=sender_alias
Expand Down Expand Up @@ -532,9 +533,10 @@ def _send_offer_update_email_via_braze(self, user_email, subject, email_body, se
"""
config = get_braze_configuration(site_code)
try:
user_emails = [user_email]
braze_client = get_braze_client(site_code)
braze_client.send_message(
email_ids=list(user_email),
email_ids=user_emails,
subject=subject,
body=email_body,
sender_alias=sender_alias
Expand Down Expand Up @@ -613,9 +615,10 @@ def _send_offer_usage_email_via_braze(self, emails, subject, email_body, site_co
"""
config = get_braze_configuration(site_code)
try:
user_emails = list(emails.strip().split(","))
braze_client = get_braze_client(site_code)
braze_client.send_message(
email_ids=list(emails),
email_ids=user_emails,
subject=subject,
body=email_body
)
Expand Down Expand Up @@ -691,9 +694,10 @@ def _send_code_assignment_nudge_email_via_braze(self, email, subject, email_body
"""
config = get_braze_configuration(site_code)
try:
user_emails = [email]
braze_client = get_braze_client(site_code)
braze_client.send_message(
email_ids=list(email),
email_ids=user_emails,
subject=subject,
body=email_body,
sender_alias=sender_alias
Expand Down

0 comments on commit f7791f1

Please sign in to comment.