Skip to content

Commit 6d64c01

Browse files
authored
Add Portal module with list_organizations function (#37)
* Returns a list of the organizations accessible to the provided API key. Includes pagination functionality via the list_metadata property.
1 parent 4abe36b commit 6d64c01

File tree

6 files changed

+121
-1
lines changed

6 files changed

+121
-1
lines changed

tests/test_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class TestClient(object):
99
def setup(self):
1010
client._audit_trail = None
1111
client._directory_sync = None
12+
client._portal = None
1213
client._sso = None
1314

1415
def test_initialize_sso(self, set_api_key_and_project_id):
@@ -20,6 +21,9 @@ def test_initialize_audit_log(self, set_api_key_and_project_id):
2021
def test_initialize_directory_sync(self, set_api_key):
2122
assert bool(client.directory_sync)
2223

24+
def test_initialize_portal(self, set_api_key):
25+
assert bool(client.portal)
26+
2327
def test_initialize_sso_missing_api_key(self, set_project_id):
2428
with pytest.raises(ConfigurationException) as ex:
2529
client.sso
@@ -61,3 +65,11 @@ def test_initialize_directory_sync_missing_api_key(self):
6165
message = str(ex)
6266

6367
assert "api_key" in message
68+
69+
def test_initialize_portal_missing_api_key(self):
70+
with pytest.raises(ConfigurationException) as ex:
71+
client.portal
72+
73+
message = str(ex)
74+
75+
assert "api_key" in message

tests/test_directory_sync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from workos.utils.request import RESPONSE_TYPE_CODE
99

1010

11-
class TestSSO(object):
11+
class TestDirectorySync(object):
1212
@pytest.fixture(autouse=True)
1313
def setup(self, set_api_key):
1414
self.directory_sync = DirectorySync()

tests/test_portal.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import json
2+
from requests import Response
3+
4+
import pytest
5+
6+
import workos
7+
from workos.portal import Portal
8+
9+
10+
class TestPortal(object):
11+
@pytest.fixture(autouse=True)
12+
def setup(self, set_api_key):
13+
self.portal = Portal()
14+
15+
@pytest.fixture
16+
def mock_organizations(self):
17+
return {
18+
"object": "list",
19+
"data": [
20+
{
21+
"object": "organization",
22+
"id": "org_01EHQMYV6MBK39QC5PZXHY59C3",
23+
"name": "example.com",
24+
"domains": [
25+
{
26+
"object": "organization_domain",
27+
"id": "org_domain_01EHQMYV71XT8H31WE5HF8YK4A",
28+
"domain": "example.com",
29+
}
30+
],
31+
},
32+
{
33+
"object": "organization",
34+
"id": "org_01EHQMVDTC2GRAHFCCRNTSKH46",
35+
"name": "example2.com",
36+
"domains": [
37+
{
38+
"object": "organization_domain",
39+
"id": "org_domain_01EHQMVDTZVA27PK614ME4YK7V",
40+
"domain": "example2.com",
41+
}
42+
],
43+
},
44+
],
45+
"listMetadata": {"before": "before-id", "after": None},
46+
}
47+
48+
def test_list_organizations(self, mock_organizations, mock_request_method):
49+
mock_response = Response()
50+
mock_response.status_code = 200
51+
mock_response.response_dict = mock_organizations
52+
mock_request_method("get", mock_response, 200)
53+
response = self.portal.list_organizations()
54+
assert response.status_code == 200
55+
assert len(response.response_dict["data"]) == 2

workos/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from workos.audit_trail import AuditTrail
22
from workos.directory_sync import DirectorySync
3+
from workos.portal import Portal
34
from workos.sso import SSO
45

56

@@ -24,5 +25,11 @@ def directory_sync(self):
2425
self._directory_sync = DirectorySync()
2526
return self._directory_sync
2627

28+
@property
29+
def portal(self):
30+
if not getattr(self, "_portal", None):
31+
self._portal = Portal()
32+
return self._portal
33+
2734

2835
client = Client()

workos/portal.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import workos
2+
from workos.utils.request import RequestHelper, REQUEST_METHOD_GET
3+
from workos.utils.validation import PORTAL_MODULE, validate_settings
4+
5+
RESPONSE_LIMIT = 10
6+
7+
8+
class Portal(object):
9+
@validate_settings(PORTAL_MODULE)
10+
def __init__(self):
11+
pass
12+
13+
@property
14+
def request_helper(self):
15+
if not getattr(self, "_request_helper", None):
16+
self._request_helper = RequestHelper()
17+
return self._request_helper
18+
19+
def list_organizations(
20+
self, domains=None, limit=RESPONSE_LIMIT, before=None, after=None
21+
):
22+
"""Retrieve a list of organizations that have connections configured within your WorkOS dashboard.
23+
24+
Kwargs:
25+
domains (list): Filter organizations to only return those that are associated with the provided domains. (Optional)
26+
limit (int): Maximum number of records to return. (Optional)
27+
before (str): Pagination cursor to receive records before a provided Organization ID. (Optional)
28+
after (str): Pagination cursor to receive records after a provided Organization ID. (Optional)
29+
30+
Returns:
31+
dict: Organizations response from WorkOS.
32+
"""
33+
params = {
34+
"domains": domains,
35+
"limit": limit,
36+
"before": before,
37+
"after": after,
38+
}
39+
return self.request_helper.request(
40+
"organizations",
41+
method=REQUEST_METHOD_GET,
42+
params=params,
43+
token=workos.api_key,
44+
)

workos/utils/validation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
AUDIT_TRAIL_MODULE = "AuditTrail"
77
DIRECTORY_SYNC_MODULE = "DirectorySync"
8+
PORTAL_MODULE = "Portal"
89
SSO_MODULE = "SSO"
910

1011
REQUIRED_SETTINGS_FOR_MODULE = {
1112
AUDIT_TRAIL_MODULE: ["api_key",],
1213
DIRECTORY_SYNC_MODULE: ["api_key",],
14+
PORTAL_MODULE: ["api_key",],
1315
SSO_MODULE: ["api_key", "project_id",],
1416
}
1517

0 commit comments

Comments
 (0)