-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from dimagi/sk/connect-id-client
Python client for 'connect-id' API
- Loading branch information
Showing
9 changed files
with
245 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .main import fetch_users, send_message, send_message_bulk # noqa: F401 |
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,45 @@ | ||
import httpx | ||
from django.conf import settings | ||
from httpx import BasicAuth, Response | ||
|
||
from commcare_connect.connect_id_client.models import ConnectIdUser, Message, MessagingBulkResponse, MessagingResponse | ||
|
||
GET = "GET" | ||
POST = "POST" | ||
|
||
|
||
def fetch_users(phone_number_list: list[str]) -> list[ConnectIdUser]: | ||
response = _make_request(GET, "/users/fetch_users", params={"phone_numbers": phone_number_list}) | ||
data = response.json() | ||
return [ConnectIdUser(**user_dict) for user_dict in data["found_users"]] | ||
|
||
|
||
def send_message(message: Message): | ||
"""Send a push notification to a user.""" | ||
response = _make_request(POST, "/messaging/send/", json=message.asdict()) | ||
data = response.json() | ||
return MessagingResponse.build(**data) | ||
|
||
|
||
def send_message_bulk(messages: list[Message]) -> MessagingBulkResponse: | ||
"""Send a push notification to multiple users.""" | ||
json = {"messages": [message.asdict() for message in messages]} | ||
response = _make_request(POST, "/messaging/send_bulk/", json=json) | ||
data = response.json() | ||
return MessagingBulkResponse.build(**data) | ||
|
||
|
||
def _make_request(method, path, params=None, json=None) -> Response: | ||
if json and not method == "POST": | ||
raise ValueError("json can only be used with POST requests") | ||
|
||
auth = BasicAuth(settings.CONNECTID_CLIENT_ID, settings.CONNECTID_CLIENT_SECRET) | ||
response = httpx.request( | ||
method, | ||
f"{settings.CONNECTID_URL}{path}", | ||
params=params, | ||
json=json, | ||
auth=auth, | ||
) | ||
response.raise_for_status() | ||
return response |
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,73 @@ | ||
import dataclasses | ||
from enum import StrEnum | ||
|
||
|
||
@dataclasses.dataclass | ||
class ConnectIdUser: | ||
name: str | ||
username: str | ||
phone_number: str | ||
|
||
def __str__(self) -> str: | ||
return f"{self.name} ({self.username})" | ||
|
||
|
||
class MessageStatus(StrEnum): | ||
success = "success" | ||
error = "error" | ||
deactivated = "deactivated" | ||
|
||
|
||
@dataclasses.dataclass | ||
class UserMessageStatus: | ||
username: str | ||
status: MessageStatus | ||
|
||
@classmethod | ||
def build(cls, username: str, status: str): | ||
return cls(username, MessageStatus(status)) | ||
|
||
|
||
@dataclasses.dataclass | ||
class MessagingResponse: | ||
all_success: bool | ||
responses: list[UserMessageStatus] | ||
|
||
@classmethod | ||
def build(cls, all_success: bool, responses: list[dict]): | ||
return cls(all_success, [UserMessageStatus.build(**response) for response in responses]) | ||
|
||
def get_failures(self) -> list[UserMessageStatus]: | ||
return [response for response in self.responses if response.status != MessageStatus.success] | ||
|
||
|
||
@dataclasses.dataclass | ||
class MessagingBulkResponse: | ||
all_success: bool | ||
messages: list[MessagingResponse] | ||
|
||
@classmethod | ||
def build(cls, all_success: bool, messages: list[dict]): | ||
return cls(all_success, [MessagingResponse.build(**message) for message in messages]) | ||
|
||
def get_failures(self) -> list[list[UserMessageStatus]]: | ||
"""Return a list of lists of UserMessageStatus objects, where each | ||
inner list is the list of failures for a single message. | ||
Usage: | ||
for message, failures in zip(messages, response.get_failures()): | ||
if failures: | ||
# do something with the failures | ||
""" | ||
return [message.get_failures() for message in self.messages] | ||
|
||
|
||
@dataclasses.dataclass | ||
class Message: | ||
usernames: list[str] | ||
title: str = None | ||
body: str = None | ||
data: dict = None | ||
|
||
def asdict(self): | ||
return {k: v for k, v in dataclasses.asdict(self).items() if v is not None} |
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,109 @@ | ||
from httpx import URL | ||
|
||
from .main import fetch_users, send_message, send_message_bulk | ||
from .models import Message, MessageStatus | ||
|
||
|
||
def test_fetch_users(httpx_mock): | ||
httpx_mock.add_response( | ||
method="GET", | ||
json={ | ||
"found_users": [ | ||
{ | ||
"username": "user_name1", | ||
"name": "name1", | ||
"phone_number": "phone_number1", | ||
}, | ||
{ | ||
"name": "name2", | ||
"username": "user_name2", | ||
"phone_number": "phone_number2", | ||
}, | ||
] | ||
}, | ||
) | ||
|
||
users = fetch_users(["phone_number1", "phone_number2"]) | ||
assert len(users) == 2 | ||
assert users[0].name == "name1" | ||
assert users[1].name == "name2" | ||
|
||
request = httpx_mock.get_request() | ||
assert URL(request.url).params.get_list("phone_numbers") == ["phone_number1", "phone_number2"] | ||
|
||
|
||
def test_send_message(httpx_mock): | ||
httpx_mock.add_response( | ||
method="POST", | ||
match_json={"usernames": ["user_name1"], "body": "test message"}, | ||
json={"all_success": True, "responses": [{"username": "user_name1", "status": "success"}]}, | ||
) | ||
print(Message(usernames=["user_name1"], body="test message").asdict()) | ||
result = send_message(Message(usernames=["user_name1"], body="test message")) | ||
assert result.all_success is True | ||
assert len(result.responses) == 1 | ||
assert result.responses[0].username == "user_name1" | ||
assert result.responses[0].status == MessageStatus.success | ||
|
||
|
||
def test_send_message_bulk(httpx_mock): | ||
httpx_mock.add_response( | ||
match_json={ | ||
"messages": [ | ||
{ | ||
"usernames": ["user_name1", "user_name2"], | ||
"body": "test message1", | ||
}, | ||
{ | ||
"usernames": ["user_name3", "user_name4"], | ||
"body": "test message2", | ||
}, | ||
] | ||
}, | ||
json={ | ||
"all_success": False, | ||
"messages": [ | ||
{ | ||
"all_success": True, | ||
"responses": [ | ||
{"status": "success", "username": "user_name1"}, | ||
{"status": "success", "username": "user_name2"}, | ||
], | ||
}, | ||
{ | ||
"all_success": False, | ||
"responses": [ | ||
{"status": "error", "username": "user_name3"}, | ||
{"status": "deactivated", "username": "user_name4"}, | ||
], | ||
}, | ||
], | ||
}, | ||
) | ||
|
||
result = send_message_bulk( | ||
[ | ||
Message(usernames=["user_name1", "user_name2"], body="test message1"), | ||
Message(usernames=["user_name3", "user_name4"], body="test message2"), | ||
] | ||
) | ||
assert result.all_success is False | ||
assert len(result.messages) == 2 | ||
|
||
assert result.messages[0].all_success is True | ||
assert [(resp.username, resp.status) for resp in result.messages[0].responses] == [ | ||
("user_name1", MessageStatus.success), | ||
("user_name2", MessageStatus.success), | ||
] | ||
assert result.messages[0].responses[0].username == "user_name1" | ||
assert result.messages[0].responses[0].status == MessageStatus.success | ||
assert result.messages[0].responses[1].username == "user_name2" | ||
assert result.messages[0].responses[1].status == MessageStatus.success | ||
|
||
assert result.messages[1].all_success is False | ||
assert [(resp.username, resp.status) for resp in result.messages[1].responses] == [ | ||
("user_name3", MessageStatus.error), | ||
("user_name4", MessageStatus.deactivated), | ||
] | ||
|
||
assert result.get_failures() == [[], list(result.messages[1].responses)] |
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
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