-
-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add job to send events to webhook (#2282)
* Add job to send events to webhook * Apply comments --------- Co-authored-by: Carlos Quintana <74399022+cquintana92@users.noreply.github.com>
- Loading branch information
1 parent
9646f84
commit ccd687f
Showing
5 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from __future__ import annotations | ||
|
||
import base64 | ||
from typing import Optional | ||
|
||
import arrow | ||
|
||
from app import config | ||
from app.errors import ProtonPartnerNotSetUp | ||
from app.events.generated import event_pb2 | ||
from app.events.generated.event_pb2 import EventContent | ||
from app.models import ( | ||
User, | ||
Job, | ||
PartnerUser, | ||
) | ||
from app.proton.utils import get_proton_partner | ||
from events.event_sink import EventSink | ||
|
||
|
||
class SendEventToWebhookJob: | ||
def __init__(self, user: User, event: EventContent): | ||
self._user: User = user | ||
self._event: EventContent = event | ||
|
||
def run(self, sink: EventSink) -> bool: | ||
# Check if the current user has a partner_id | ||
try: | ||
proton_partner_id = get_proton_partner().id | ||
except ProtonPartnerNotSetUp: | ||
return False | ||
|
||
# It has. Retrieve the information for the PartnerUser | ||
partner_user = PartnerUser.get_by( | ||
user_id=self._user.id, partner_id=proton_partner_id | ||
) | ||
if partner_user is None: | ||
return True | ||
event = event_pb2.Event( | ||
user_id=self._user.id, | ||
external_user_id=partner_user.external_user_id, | ||
partner_id=partner_user.partner_id, | ||
content=self._event, | ||
) | ||
|
||
serialized = event.SerializeToString() | ||
return sink.send_data_to_webhook(serialized) | ||
|
||
@staticmethod | ||
def create_from_job(job: Job) -> Optional[SendEventToWebhookJob]: | ||
user = User.get(job.payload["user_id"]) | ||
if not user: | ||
return None | ||
event_data = base64.b64decode(job.payload["event"]) | ||
event = event_pb2.EventContent() | ||
event.ParseFromString(event_data) | ||
|
||
return SendEventToWebhookJob(user=user, event=event) | ||
|
||
def store_job_in_db(self, run_at: Optional[arrow.Arrow]) -> Job: | ||
stub = self._event.SerializeToString() | ||
return Job.create( | ||
name=config.JOB_SEND_EVENT_TO_WEBHOOK, | ||
payload={ | ||
"user_id": self._user.id, | ||
"event": base64.b64encode(stub).decode("utf-8"), | ||
}, | ||
run_at=run_at if run_at is not None else arrow.now(), | ||
commit=True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import arrow | ||
|
||
from app import config | ||
from app.events.generated.event_pb2 import EventContent, AliasDeleted | ||
from app.jobs.send_event_job import SendEventToWebhookJob | ||
from app.models import PartnerUser | ||
from app.proton.utils import get_proton_partner | ||
from events.event_sink import ConsoleEventSink | ||
from tests.utils import create_new_user, random_token | ||
|
||
|
||
def test_serialize_and_deserialize_job(): | ||
user = create_new_user() | ||
alias_id = 34 | ||
alias_email = "a@b.c" | ||
event = EventContent(alias_deleted=AliasDeleted(id=alias_id, email=alias_email)) | ||
run_at = arrow.now().shift(hours=10) | ||
db_job = SendEventToWebhookJob(user, event).store_job_in_db(run_at=run_at) | ||
assert db_job.run_at == run_at | ||
assert db_job.name == config.JOB_SEND_EVENT_TO_WEBHOOK | ||
job = SendEventToWebhookJob.create_from_job(db_job) | ||
assert job._user.id == user.id | ||
assert job._event.alias_deleted.id == alias_id | ||
assert job._event.alias_deleted.email == alias_email | ||
|
||
|
||
def test_send_event_to_webhook(): | ||
user = create_new_user() | ||
PartnerUser.create( | ||
user_id=user.id, | ||
partner_id=get_proton_partner().id, | ||
external_user_id=random_token(10), | ||
flush=True, | ||
) | ||
alias_id = 34 | ||
alias_email = "a@b.c" | ||
event = EventContent(alias_deleted=AliasDeleted(id=alias_id, email=alias_email)) | ||
job = SendEventToWebhookJob(user, event) | ||
sink = ConsoleEventSink() | ||
assert job.run(sink) |