Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add postprocessing for email actions and include page_name property #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 32 additions & 2 deletions target_actionkit/sinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ContactsSink(ActionKitSink):
"""ActionKit target sink class."""

name = "Contacts"
endpoint = "user"
endpoint = "rest/v1/"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this intentional?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Without it I would not be able to add users to the action page

entity = "user"

def add_phone_numbers(self, user_id: str, record: dict):
Expand Down Expand Up @@ -49,6 +49,9 @@ def upsert_record(self, record: dict, context: dict):

if existing_users:
user_id = existing_users[0].get("id")
# TODO:I got {"errors": {"zip": ["Data too long! Please use a smaller value, see the schema for the maximum allowed input"]}}}
# I'm not sure if this is the correct way to handle this, but it's a temporary fix
record.pop("zip", None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this just mean you were providing an invalid zip?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a valid one and in the schema there wasn't any valuable information on how to handle it... If I let it updating the zip it would break things.

response = self.request_api(
"PATCH",
request_data=record,
Expand All @@ -59,7 +62,8 @@ def upsert_record(self, record: dict, context: dict):
if response.ok:
self.add_phone_numbers(user_id, record)
state_dict["success"] = True
state_dict["is_updated"] = True
state_dict["is_updated"] = True
self.postprocess_record(record, context)
return user_id, response.ok, state_dict

response = self.request_api(
Expand All @@ -74,6 +78,7 @@ def upsert_record(self, record: dict, context: dict):
id = response.headers['Location'].replace(f"https://{self.config.get('hostname')}.actionkit.com/rest/v1/user/", "")[:-1]
self.logger.info(id)
self.add_phone_numbers(id, record)
self.postprocess_record(record, context)
return id, response.ok, state_dict

return None, False, state_dict
Expand All @@ -99,3 +104,28 @@ def preprocess_record(self, record: dict, context: dict) -> dict:
break

return payload

def postprocess_record(self, record: dict, context: dict) -> dict:
if not self.config.get("page_name"):
return record
email = record.get("email")
if not email:
return record
try:
response = self.request_api(
"POST",
endpoint="action",
request_data={
"page": self.config.get("page_name"),
"email": email
},
headers=self.prepare_request_headers()
)
if response.ok:
self.logger.info(f"Action created for {response.json().get('user')} on {self.config.get('page_name')}")
return record
else:
raise Exception(f"Failed to create action for {email} on {self.config.get('page_name')}: {response.text}")
except Exception as e:
self.logger.error(f"Failed to create action for {email} on {self.config.get('page_name')}: {e}")
return record
1 change: 1 addition & 0 deletions target_actionkit/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(
th.Property("username", th.StringType, required=True),
th.Property("password", th.StringType, required=True),
th.Property("hostname", th.StringType, required=True),
th.Property("page_name", th.StringType),
).to_dict()


Expand Down