Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Jun 25, 2024
1 parent bbc4658 commit e528588
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 37 deletions.
6 changes: 3 additions & 3 deletions temba/channels/android/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def sync(request, channel_id):
if phone and text:
try:
msg_id = mailroom.get_client().android_message(
channel.org_id, channel.id, phone, text, received_on=date
channel.org, channel, phone, text, received_on=date
)
extra = dict(msg_id=msg_id)
except mailroom.URNValidationException:
Expand All @@ -162,8 +162,8 @@ def sync(request, channel_id):
if phone and call_tuple not in unique_calls and ChannelEvent.is_valid_type(cmd["type"]):
try:
mailroom.get_client().android_event(
channel.org_id,
channel.id,
channel.org,
channel,
phone,
cmd["type"],
extra={"duration": duration},
Expand Down
44 changes: 23 additions & 21 deletions temba/mailroom/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,30 @@ def __init__(self, base_url, auth_token):
def version(self):
return self._request("", post=False).get("version")

def android_event(self, org_id: int, channel_id: int, phone: str, event_type: str, extra: dict, occurred_on):
payload = {
"org_id": org_id,
"channel_id": channel_id,
"phone": phone,
"event_type": event_type,
"extra": extra,
"occurred_on": occurred_on.isoformat(),
}

return self._request("android/event", payload)

def android_message(self, org_id: int, channel_id: int, phone: str, text: str, received_on):
payload = {
"org_id": org_id,
"channel_id": channel_id,
"phone": phone,
"text": text,
"received_on": received_on.isoformat(),
}
def android_event(self, org, channel, phone: str, event_type: str, extra: dict, occurred_on):
return self._request(
"android/event",
{
"org_id": org.id,
"channel_id": channel.id,
"phone": phone,
"event_type": event_type,
"extra": extra,
"occurred_on": occurred_on.isoformat(),
},
)

return self._request("android/message", payload)
def android_message(self, org, channel, phone: str, text: str, received_on):
return self._request(
"android/message",
{
"org_id": org.id,
"channel_id": channel.id,
"phone": phone,
"text": text,
"received_on": received_on.isoformat(),
},
)

def contact_create(self, org_id: int, user_id: int, contact: ContactSpec):
payload = {"org_id": org_id, "user_id": user_id, "contact": asdict(contact)}
Expand Down
12 changes: 6 additions & 6 deletions temba/mailroom/client/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def test_version(self):
def test_android_event(self, mock_post):
mock_post.return_value = MockJsonResponse(200, {"id": 12345})
response = self.client.android_event(
org_id=self.org.id,
channel_id=12,
org=self.org,
channel=self.channel,
phone="+1234567890",
event_type="mo_miss",
extra={"duration": 45},
Expand All @@ -52,7 +52,7 @@ def test_android_event(self, mock_post):
headers={"User-Agent": "Temba", "Authorization": "Token sesame"},
json={
"org_id": self.org.id,
"channel_id": 12,
"channel_id": self.channel.id,
"phone": "+1234567890",
"event_type": "mo_miss",
"extra": {"duration": 45},
Expand All @@ -64,8 +64,8 @@ def test_android_event(self, mock_post):
def test_android_message(self, mock_post):
mock_post.return_value = MockJsonResponse(200, {"id": 12345})
response = self.client.android_message(
org_id=self.org.id,
channel_id=12,
org=self.org,
channel=self.channel,
phone="+1234567890",
text="hello",
received_on=datetime(2024, 4, 1, 16, 28, 30, 0, tzone.utc),
Expand All @@ -78,7 +78,7 @@ def test_android_message(self, mock_post):
headers={"User-Agent": "Temba", "Authorization": "Token sesame"},
json={
"org_id": self.org.id,
"channel_id": 12,
"channel_id": self.channel.id,
"phone": "+1234567890",
"text": "hello",
"received_on": "2024-04-01T16:28:30+00:00",
Expand Down
10 changes: 3 additions & 7 deletions temba/tests/mailroom.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from temba import mailroom
from temba.campaigns.models import CampaignEvent, EventFire
from temba.channels.models import Channel, ChannelEvent
from temba.channels.models import ChannelEvent
from temba.contacts.models import URN, Contact, ContactField, ContactGroup, ContactURN
from temba.flows.models import FlowRun, FlowSession
from temba.locations.models import AdminBoundary
Expand Down Expand Up @@ -137,9 +137,7 @@ def __init__(self, mocks: Mocks):

super().__init__(settings.MAILROOM_URL, settings.MAILROOM_AUTH_TOKEN)

def android_event(self, org_id: int, channel_id: int, phone: str, event_type: str, extra: dict, occurred_on):
org = Org.objects.get(id=org_id)
channel = Channel.objects.get(id=channel_id)
def android_event(self, org, channel, phone: str, event_type: str, extra: dict, occurred_on):
contact, contact_urn = contact_resolve(org, phone)

event = ChannelEvent.objects.create(
Expand All @@ -153,9 +151,7 @@ def android_event(self, org_id: int, channel_id: int, phone: str, event_type: st
)
return {"id": event.id}

def android_message(self, org_id: int, channel_id: int, phone: str, text: str, received_on):
org = Org.objects.get(id=org_id)
channel = Channel.objects.get(id=channel_id)
def android_message(self, org, channel, phone: str, text: str, received_on):
contact, contact_urn = contact_resolve(org, phone)
text = text[: Msg.MAX_TEXT_LEN]

Expand Down

0 comments on commit e528588

Please sign in to comment.