Skip to content

Commit fb57008

Browse files
awolfdenAdam Wolfman
andauthored
Add support for idempotency key in create_organization method (#143)
* Add support for idempotency key in create_organization method * Refactor idempotency method and test * Update casing for idempotency-key header * Update headers declaration in organizations.py * Remove erroneous description of idempotency-key param Co-authored-by: Adam Wolfman <adamwolfman@Adams-MBP-3.domain>
1 parent 0b037ae commit fb57008

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

tests/test_organizations.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import json
2-
from requests import Response
3-
41
import pytest
5-
6-
import workos
72
from workos.organizations import Organizations
83

94

@@ -109,6 +104,19 @@ def test_create_organization(self, mock_organization, mock_request_method):
109104
assert organization["id"] == "org_01EHT88Z8J8795GZNQ4ZP1J81T"
110105
assert organization["name"] == "Foo Corporation"
111106

107+
def test_sends_idempotency_key(self, capture_and_mock_request):
108+
idempotency_key = "test_123456789"
109+
payload = {"domains": ["example.com"], "name": "Foo Corporation"}
110+
111+
_, request_kwargs = capture_and_mock_request("post", payload, 200)
112+
113+
response = self.organizations.create_organization(
114+
payload, idempotency_key=idempotency_key
115+
)
116+
117+
assert request_kwargs["headers"]["idempotency-key"] == idempotency_key
118+
assert response["name"] == "Foo Corporation"
119+
112120
def test_update_organization(self, mock_organization_updated, mock_request_method):
113121
mock_request_method("put", mock_organization_updated, 201)
114122

workos/organizations.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def get_organization(self, organization):
7878

7979
return WorkOSOrganization.construct_from_response(response).to_dict()
8080

81-
def create_organization(self, organization):
81+
def create_organization(self, organization, idempotency_key=None):
8282
"""Create an organization
8383
8484
Args:
@@ -88,14 +88,20 @@ def create_organization(self, organization):
8888
within the Organization allow profiles that are outside of the Organization's
8989
configured User Email Domains. (Optional)
9090
organization[domains] (list) - List of domains that belong to the organization
91+
idempotency_key (str) - Idempotency key for creating an organization. (Optional)
9192
9293
Returns:
9394
dict: Created Organization response from WorkOS.
9495
"""
96+
headers = {}
97+
if idempotency_key:
98+
headers["idempotency-key"] = idempotency_key
99+
95100
response = self.request_helper.request(
96101
ORGANIZATIONS_PATH,
97102
method=REQUEST_METHOD_POST,
98103
params=organization,
104+
headers=headers,
99105
token=workos.api_key,
100106
)
101107

0 commit comments

Comments
 (0)