Skip to content

Commit e05dd24

Browse files
Add messages failover (#320)
* add new response field * add failover parameter * formatting * add failover testing * change payload * fix tests * update messages package for release * prepare for new release --------- Co-authored-by: maxkahan <maxkahan94@gmail.com>
1 parent 82a02e4 commit e05dd24

File tree

10 files changed

+80
-8
lines changed

10 files changed

+80
-8
lines changed

messages/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 1.5.0
2+
- Add an optional "failover" property to `vonage_messages.Messages.send`
3+
14
# 1.4.0
25
- Make all models originally accessed by `vonage_messages.models.***` available at the top level of the package, i.e. `vonage_messages.***`
36

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.4.0'
1+
__version__ = '1.5.0'

messages/src/vonage_messages/messages.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,35 @@ def http_client(self) -> HttpClient:
3131
return self._http_client
3232

3333
@validate_call
34-
def send(self, message: BaseMessage) -> SendMessageResponse:
34+
def send(
35+
self, message: BaseMessage, failover: list[BaseMessage] = None
36+
) -> SendMessageResponse:
3537
"""Send a message using Vonage's Messages API.
3638
3739
Args:
3840
message (BaseMessage): The message to be sent as a Pydantic model.
3941
Use the provided models (in `vonage_messages.models`) to create messages and pass them in to this method.
42+
failover (list[BaseMessage]): A list of failover messages to be attempted if the primary message fails.
4043
4144
Returns:
4245
SendMessageResponse: Response model containing the unique identifier of the sent message.
4346
Access the identifier with the `message_uuid` attribute.
4447
"""
48+
body = message.model_dump(by_alias=True, exclude_none=True) or message
49+
50+
if failover is not None:
51+
failover_body = [
52+
m.model_dump(by_alias=True, exclude_none=True) or m for m in failover
53+
]
54+
body = {
55+
**body,
56+
'failover': failover_body,
57+
}
58+
4559
response = self._http_client.post(
4660
self._http_client.api_host,
4761
'/v1/messages',
48-
message.model_dump(by_alias=True, exclude_none=True) or message,
62+
body,
4963
self._auth_type,
5064
)
5165

messages/src/vonage_messages/models/rcs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class BaseRcs(BaseMessage):
3030
"""
3131

3232
to: PhoneNumber
33-
from_: str = Field(..., serialization_alias='from', pattern='^[a-zA-Z0-9]+$')
33+
from_: str = Field(..., serialization_alias='from', pattern='^[a-zA-Z0-9-_]+$')
3434
ttl: Optional[int] = Field(None, ge=300, le=259200)
3535
channel: ChannelType = ChannelType.RCS
3636

messages/src/vonage_messages/responses.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from pydantic import BaseModel
24

35

@@ -6,6 +8,8 @@ class SendMessageResponse(BaseModel):
68
79
Attributes:
810
message_uuid (str): The UUID of the sent message.
11+
workflow_id [str]: Workflow ID if the `failover` parameter was used in the request.
912
"""
1013

1114
message_uuid: str
15+
workflow_id: Optional[str] = None
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"message_uuid": "d8f86df1-dec6-442f-870a-2241be27d721",
3+
"workflow_id": "3TcNjguHxr2vcCZ9Ddsnq6tw8yQUpZ9rMHv9QXSxLan5ibMxqSzLdx9"
4+
}

messages/tests/test_messages.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from json import loads
12
from os.path import abspath
23

34
import responses
@@ -51,6 +52,49 @@ def test_send_message():
5152
assert messages._auth_type == 'jwt'
5253

5354

55+
@responses.activate
56+
def test_send_message_with_failover():
57+
build_response(
58+
path,
59+
'POST',
60+
'https://api.nexmo.com/v1/messages',
61+
'send_message_with_failover.json',
62+
202,
63+
)
64+
sms = Sms(
65+
from_='Vonage APIs',
66+
to='1234567890',
67+
text='Hello, World!',
68+
)
69+
failover = [
70+
Sms(from_='Vonage APIs', to='1987654321', text='Failover message'),
71+
]
72+
73+
response = messages.send(sms, failover=failover)
74+
print(messages._http_client.last_request.body)
75+
assert loads(messages._http_client.last_request.body) == {
76+
"to": "1234567890",
77+
"from": "Vonage APIs",
78+
"text": "Hello, World!",
79+
"channel": "sms",
80+
"message_type": "text",
81+
"failover": [
82+
{
83+
"to": "1987654321",
84+
"from": "Vonage APIs",
85+
"text": "Failover message",
86+
"channel": "sms",
87+
"message_type": "text",
88+
}
89+
],
90+
}
91+
assert response.message_uuid == 'd8f86df1-dec6-442f-870a-2241be27d721'
92+
assert (
93+
response.workflow_id == '3TcNjguHxr2vcCZ9Ddsnq6tw8yQUpZ9rMHv9QXSxLan5ibMxqSzLdx9'
94+
)
95+
assert messages._auth_type == 'jwt'
96+
97+
5498
@responses.activate
5599
def test_send_message_basic_auth():
56100
build_response(

vonage/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 4.5.0
2+
- vonage-messages: add an optional "failover" property to `vonage_messages.Messages.send`
3+
14
# 4.4.3
25
- vonage-number-insight: use basic header auth instead of request body auth
36

vonage/pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ description = "Python Server SDK for using Vonage APIs"
55
readme = "README.md"
66
requires-python = ">=3.9"
77
dependencies = [
8-
"vonage-utils>=1.1.4",
9-
"vonage-http-client>=1.5.1",
108
"vonage-account>=1.1.1",
119
"vonage-application>=2.0.1",
12-
"vonage-messages>=1.4.0",
10+
"vonage-http-client>=1.5.1",
11+
"vonage-messages>=1.5.0",
1312
"vonage-network-auth>=1.0.2",
1413
"vonage-network-sim-swap>=1.1.2",
1514
"vonage-network-number-verification>=1.0.2",
@@ -18,6 +17,7 @@ dependencies = [
1817
"vonage-sms>=1.1.6",
1918
"vonage-subaccounts>=1.0.4",
2019
"vonage-users>=1.2.1",
20+
"vonage-utils>=1.1.4",
2121
"vonage-verify>=2.1.0",
2222
"vonage-verify-legacy>=1.0.1",
2323
"vonage-video>=1.2.0",

vonage/src/vonage/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '4.4.3'
1+
__version__ = '4.5.0'

0 commit comments

Comments
 (0)