-
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.
- Loading branch information
Showing
5 changed files
with
70 additions
and
10 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 # 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,30 @@ | ||
import httpx | ||
from django.conf import settings | ||
from httpx import BasicAuth, Response | ||
|
||
from commcare_connect.connect_id_client.models import ConnectIdUser | ||
|
||
GET = "GET" | ||
POST = "POST" | ||
|
||
|
||
def fetch_users(phone_number_list) -> 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 _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,11 @@ | ||
import dataclasses | ||
|
||
|
||
@dataclasses.dataclass | ||
class ConnectIdUser: | ||
name: str | ||
username: str | ||
phone_number: str | ||
|
||
def __str__(self) -> str: | ||
return f"{self.name} ({self.username})" |
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,25 @@ | ||
from .main import fetch_users | ||
|
||
|
||
def test_fetch_users(httpx_mock): | ||
httpx_mock.add_response( | ||
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" |
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