Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: poetry run black temba_client

- name: Run ruff
run: poetry run ruff temba_client
run: poetry run ruff check temba_client

- name: Run isort
run: poetry run isort temba_client
Expand Down
41 changes: 22 additions & 19 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ packages = [

[tool.poetry.group.dev.dependencies]
coverage = "^5.3"
ruff = "^0.0.171"
ruff = "^0.14.0"
isort = "^5.10.1"
black = "^24.3.0"
nose2 = "^0.12.0"
Expand All @@ -40,9 +40,11 @@ line-length = 119

[tool.ruff]
line-length = 120
fix = true

[tool.ruff.lint]
select = ["E", "F", "W"]
ignore = ["E501", "F405"]
fix = true

[tool.isort]
multi_line_output = 3
Expand Down
4 changes: 2 additions & 2 deletions temba_client/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ def deserialize(self, value):

class IntegerField(SimpleField):
def deserialize(self, value):
if value is not None and type(value) != int:
if value is not None and not isinstance(value, int):
raise TembaSerializationException("Value '%s' field is not an integer" % str(value))
return value


class ListField(SimpleField):
def deserialize(self, value):
if value is not None and type(value) != list:
if value is not None and not isinstance(value, list):
raise TembaSerializationException("Value '%s' field is not a list" % str(value))
return value

Expand Down
2 changes: 1 addition & 1 deletion temba_client/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def assertRequestURL(self, mock, method, url, **kwargs):
"User-Agent": "test/0.1 rapidpro-python/%s" % CLIENT_VERSION,
},
verify=None,
**kwargs
**kwargs,
)
mock.reset_mock()

Expand Down
26 changes: 16 additions & 10 deletions temba_client/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,15 @@ def get_fields(self, key=None):
"""
return self._get_query("fields", self._build_params(key=key), Field)

def get_flows(self, uuid=None):
def get_flows(self, uuid=None, type=None, archived=None):
"""
Gets all matching flows
:param uuid: flow UUID
:param str type: "message" or "voice"
:param bool archived: whether to include archived flows
:return: flow query
"""
return self._get_query("flows", self._build_params(uuid=uuid), Flow)
return self._get_query("flows", self._build_params(uuid=uuid, type=type, archived=archived), Flow)

def get_flow_starts(self, uuid=None):
"""
Expand All @@ -161,12 +163,13 @@ def get_flow_starts(self, uuid=None):
"""
return self._get_query("flow_starts", self._build_params(uuid=uuid), FlowStart)

def get_globals(self):
def get_globals(self, key=None):
"""
Gets all globals
:param str key: field key
:return: global query
"""
return self._get_query("globals", {}, Global)
return self._get_query("globals", self._build_params(key=key), Global)

def get_groups(self, uuid=None, name=None):
"""
Expand Down Expand Up @@ -232,9 +235,7 @@ def get_resthook_subscribers(self, id=None, resthook=None):
params = self._build_params(id=id, resthook=resthook)
return self._get_query("resthook_subscribers", params, ResthookSubscriber)

def get_runs(
self, uuid=None, flow=None, responded=None, before=None, after=None, reverse=None, paths=None
):
def get_runs(self, uuid=None, flow=None, responded=None, before=None, after=None, reverse=None, paths=None):
"""
Gets all matching flow runs
:param uuid: flow run UUID
Expand Down Expand Up @@ -293,7 +294,7 @@ def create_campaign_event(self, campaign, relative_to, offset, unit, delivery_ho
:param int delivery_hour:
:param str message:
:param * flow: flow object, UUID or name
:return: the new campaign
:return: the new campaign event
"""
payload = self._build_params(
campaign=campaign,
Expand Down Expand Up @@ -379,16 +380,21 @@ def create_label(self, name):
"""
return Label.deserialize(self._post("labels", None, self._build_params(name=name)))

def create_message(self, contact, text, attachments):
def create_message(self, contact, text, attachments, quick_replies=[]):
"""
Creates a new outgoing message
:param str contact: contact UUID
:param str text: message text
:param list[str] attachments: message attachments
:param list[dict] quick_replies: message quick replies
:return: the new message
"""
return Message.deserialize(
self._post("messages", None, self._build_params(contact=contact, text=text, attachments=attachments))
self._post(
"messages",
None,
self._build_params(contact=contact, text=text, attachments=attachments, quick_replies=quick_replies),
)
)

def create_resthook_subscriber(self, resthook, target_url):
Expand Down
69 changes: 65 additions & 4 deletions temba_client/v2/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ def test_get_broadcasts(self, mock_request):
mock_request,
"get",
"broadcasts",
params={"uuid": "c4f3b6e1-2d3a-4f4b-8f4e-1e2d3c4b5a6f", "after": "2014-12-12T22:34:36.978123Z", "before": "2014-12-12T22:56:58.917123Z"},
params={
"uuid": "c4f3b6e1-2d3a-4f4b-8f4e-1e2d3c4b5a6f",
"after": "2014-12-12T22:34:36.978123Z",
"before": "2014-12-12T22:56:58.917123Z",
},
)

def test_get_campaigns(self, mock_request):
Expand Down Expand Up @@ -498,11 +502,31 @@ def test_get_flows(self, mock_request):
["2bfbd76a-245a-473c-a296-28e4815f3a98", "d8b0ed18-a5c2-48be-98af-9b7f017fdc6c"],
)

# check with all params
# check with uuid param
self.client.get_flows(uuid="ffce0fbb-4fe1-4052-b26a-91beb2ebae9a").all()

self.assertRequest(mock_request, "get", "flows", params={"uuid": "ffce0fbb-4fe1-4052-b26a-91beb2ebae9a"})

# check with type param
self.client.get_flows(type="message").all()

self.assertRequest(mock_request, "get", "flows", params={"type": "message"})

# check with archived param
self.client.get_flows(archived=True).all()

self.assertRequest(mock_request, "get", "flows", params={"archived": True})

# check with all param
self.client.get_flows(uuid="ffce0fbb-4fe1-4052-b26a-91beb2ebae9a", type="message", archived=False).all()

self.assertRequest(
mock_request,
"get",
"flows",
params={"uuid": "ffce0fbb-4fe1-4052-b26a-91beb2ebae9a", "type": "message", "archived": False},
)

def test_get_flow_starts(self, mock_request):
# check no params
mock_request.return_value = MockResponse(200, self.read_json("flow_starts"))
Expand Down Expand Up @@ -548,6 +572,11 @@ def test_get_globals(self, mock_request):
self.assertEqual(results[0].value, "Acme Ltd")
self.assertEqual(results[0].modified_on, datetime(2015, 6, 8, 12, 18, 7, 671000, tzone.utc))

# check with all params
self.client.get_globals(key="org_name").all()

self.assertRequest(mock_request, "get", "globals", params={"key": "org_name"})

def test_get_groups(self, mock_request):
# check no params
mock_request.return_value = MockResponse(200, self.read_json("groups"))
Expand Down Expand Up @@ -626,6 +655,11 @@ def test_get_messages(self, mock_request):
self.assertEqual(results[1].attachments[0].content_type, "audio/wav")
self.assertEqual(results[1].attachments[0].url, "http://domain.com/recording.wav")

self.assertEqual(results[1].quick_replies[0].text, "Red")
self.assertEqual(results[1].quick_replies[1].text, "Green")
self.assertEqual(results[1].quick_replies[1].extra, "Like grass")
self.assertEqual(results[1].quick_replies[2].text, "Blue")

# check with all params
self.client.get_messages(
uuid="eb6aeae0-0433-45de-bbed-031039a1cfaa",
Expand Down Expand Up @@ -932,10 +966,34 @@ def test_create_message(self, mock_request):
mock_request,
"post",
"messages",
data={"contact": "5079cb96-a1d8-4f47-8c87-d8c7bb6ddab9", "text": "Hi there", "attachments": []},
data={
"contact": "5079cb96-a1d8-4f47-8c87-d8c7bb6ddab9",
"text": "Hi there",
"attachments": [],
"quick_replies": [],
},
)
self.assertEqual(message.uuid, "eb6aeae0-0433-45de-bbed-031039a1cfaa")

message = self.client.create_message(
contact="5079cb96-a1d8-4f47-8c87-d8c7bb6ddab9",
text="Hi there",
attachments=[],
quick_replies=[{"text": "Blue"}, {"text": "Green", "extra": "Like grass"}],
)

self.assertRequest(
mock_request,
"post",
"messages",
data={
"contact": "5079cb96-a1d8-4f47-8c87-d8c7bb6ddab9",
"text": "Hi there",
"attachments": [],
"quick_replies": [{"text": "Blue"}, {"text": "Green", "extra": "Like grass"}],
},
)

def test_create_resthook_subscriber(self, mock_request):
subscriber_json = self.read_json("resthook_subscribers", extract_result=0)

Expand Down Expand Up @@ -1241,7 +1299,10 @@ def test_contact_actions(self, mock_request):
def test_message_actions(self, mock_request):
mock_request.return_value = MockResponse(204, "")

messages = [Message.create(uuid="eb6aeae0-0433-45de-bbed-031039a1cfaa"), "2be38dc4-b3ae-4fdf-a3b6-7defb3c11c4c"]
messages = [
Message.create(uuid="eb6aeae0-0433-45de-bbed-031039a1cfaa"),
"2be38dc4-b3ae-4fdf-a3b6-7defb3c11c4c",
]
resolved_messages = ["eb6aeae0-0433-45de-bbed-031039a1cfaa", "2be38dc4-b3ae-4fdf-a3b6-7defb3c11c4c"]

self.client.bulk_label_messages(messages=messages, label="Testing", label_name="Spam")
Expand Down
10 changes: 8 additions & 2 deletions temba_client/v2/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Broadcast(TembaObject):
text = SimpleField()
created_on = DatetimeField()


class Campaign(TembaObject):
uuid = SimpleField()
name = SimpleField()
Expand Down Expand Up @@ -93,11 +94,10 @@ class Device(TembaObject):
power_source = SimpleField()
network_type = SimpleField()


uuid = SimpleField()
name = SimpleField()
address = SimpleField()
type=SimpleField()
type = SimpleField()
country = SimpleField()
device = ObjectField(item_class=Device)
last_seen = DatetimeField()
Expand Down Expand Up @@ -134,6 +134,7 @@ class Export(TembaObject):
fields = ListField()
groups = ListField()


class Field(TembaObject):
key = SimpleField()
name = SimpleField()
Expand Down Expand Up @@ -206,6 +207,10 @@ class AttachmentRef(TembaObject):
content_type = SimpleField()
url = SimpleField()

class QuickReply(TembaObject):
text = SimpleField()
extra = SimpleField()

uuid = SimpleField()
contact = ObjectField(item_class=ObjectRef)
urn = SimpleField()
Expand All @@ -217,6 +222,7 @@ class AttachmentRef(TembaObject):
text = SimpleField()
labels = ObjectListField(item_class=ObjectRef)
attachments = ObjectListField(item_class=AttachmentRef)
quick_replies = ObjectListField(item_class=QuickReply)
flow = ObjectField(item_class=ObjectRef)
created_on = DatetimeField()
sent_on = DatetimeField()
Expand Down
2 changes: 2 additions & 0 deletions test_files/v2/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"text": "How are you?",
"labels": [],
"attachments": [],
"quick_replies": [],
"flow": {
"uuid": "6ca413c7-bc3a-42cb-9744-6938f791bc36",
"name": "Registration"
Expand Down Expand Up @@ -53,6 +54,7 @@
"url": "http://domain.com/recording.wav"
}
],
"quick_replies": [{"text": "Red"}, {"text": "Green", "extra": "Like grass"}, {"text": "Blue"}],
"flow": null,
"created_on": "2015-11-10T12:59:10+00:00",
"sent_on": null,
Expand Down