Skip to content

Add function for interrupting URNs #83

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

Merged
merged 2 commits into from
Apr 27, 2020
Merged
Changes from 1 commit
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
32 changes: 31 additions & 1 deletion rapid_pro_tools/rapid_pro_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,24 @@ def get_raw_messages(self, created_after_inclusive=None, created_before_exclusiv

return raw_messages

def send_message_to_urn(self, message, target_urn):
def send_message_to_urn(self, message, target_urn, interrupt=False):
"""
Sends a message to the given URN.

:param message: Text of the message to send.
:type message: str
:param target_urn: URN to send the message to.
:type target_urn: str
:param interrupt: Whether to interrupt the target_urn from flows before sending the message.
:type interrupt: bool
:return: Id of the Rapid Pro broadcast created for this send request.
This id may be used to check on the status of the broadcast by making further requests to Rapid Pro.
Note that this is a broadcast (to one person) because Rapid Pro does not support unicasting.
:rtype: int
"""
if interrupt:
self.interrupt_urns([target_urn])

log.info("Sending a message to an individual...")
log.debug(f"Sending to '{target_urn}' the message '{message}'...")
response: Broadcast = self.rapid_pro.create_broadcast(message, urns=[target_urn])
Expand All @@ -286,6 +291,31 @@ def get_broadcast_for_broadcast_id(self, broadcast_id):
f"(expected exactly 1)"
return matching_broadcasts[0]

def interrupt_urns(self, urns):
"""
Interrupts the given URNs from the flows they are currently in, if any.

Makes 1 request for every 100 URNs.
Copy link
Member

Choose a reason for hiding this comment

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

I misunderstood this comment on first read to say that it was going to buffer requests and so wouldn't do anything until it had received 100 URNs.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed to "If the list of URNs contains more than 100 items, requests will be made in batches of 100 URNs at a time."


:param urns: URNs to interrupt
:type urns: list of str
"""
log.info(f"Interrupting {len(urns)} URNs...")
log.debug(f"Interrupting {urns}...")
batch = []
interrupted = 0
for urn in urns:
batch.append(urn)
if len(batch) >= 100: # limit of 100 imposed by Rapid Pro's API
self.rapid_pro.bulk_interrupt_contacts(batch)
interrupted += len(batch)
log.info(f"Interrupted {interrupted} / {len(urns)} URNs")
batch = []
if len(batch) > 0:
self.rapid_pro.bulk_interrupt_contacts(batch)
interrupted += len(batch)
log.info(f"Interrupted {interrupted} / {len(urns)} URNs")

def _get_archived_runs_for_flow_id(self, flow_id, last_modified_after_inclusive=None,
last_modified_before_exclusive=None):
"""
Expand Down