Skip to content

Commit a83cca3

Browse files
feat(organization-domains): add organization domains (#166)
1 parent 581803c commit a83cca3

23 files changed

+582
-21
lines changed

src/directory_sync/types/directory.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use derive_more::{Deref, Display, From};
22
use serde::{Deserialize, Serialize};
33

44
use crate::directory_sync::DirectoryType;
5-
use crate::organizations::{OrganizationDomainId, OrganizationId};
5+
use crate::organization_domains::OrganizationDomainId;
6+
use crate::organizations::OrganizationId;
67
use crate::{KnownOrUnknown, Timestamps};
78

89
/// The ID of a [`Directory`].

src/events/types/events/organization_domain_created.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use serde::{Deserialize, Serialize};
22

3-
use crate::organizations::OrganizationDomain;
3+
use crate::organization_domains::OrganizationDomain;
44

55
/// [WorkOS Docs: `organization_domain.created` event](https://workos.com/docs/events/organization-domain).
66
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]

src/events/types/events/organization_domain_deleted.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use serde::{Deserialize, Serialize};
22

3-
use crate::organizations::OrganizationDomain;
3+
use crate::organization_domains::OrganizationDomain;
44

55
/// [WorkOS Docs: `organization_domain.deleted` event](https://workos.com/docs/events/organization-domain).
66
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]

src/events/types/events/organization_domain_updated.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use serde::{Deserialize, Serialize};
22

3-
use crate::organizations::OrganizationDomain;
3+
use crate::organization_domains::OrganizationDomain;
44

55
/// [WorkOS Docs: `organization_domain.updated` event](https://workos.com/docs/events/organization-domain).
66
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]

src/events/types/events/organization_domain_verification_failed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use serde::{Deserialize, Serialize};
22

3-
use crate::organizations::OrganizationDomain;
3+
use crate::organization_domains::OrganizationDomain;
44

55
/// [WorkOS Docs: `organization_domain.verification_failed` event](https://workos.com/docs/events/organization-domain).
66
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]

src/events/types/events/organization_domain_verified.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use serde::{Deserialize, Serialize};
22

3-
use crate::organizations::OrganizationDomain;
3+
use crate::organization_domains::OrganizationDomain;
44

55
/// [WorkOS Docs: `organization_domain.verified` event](https://workos.com/docs/events/organization-domain).
66
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod workos;
99
pub mod directory_sync;
1010
pub mod events;
1111
pub mod mfa;
12+
pub mod organization_domains;
1213
pub mod organizations;
1314
pub mod portal;
1415
pub mod roles;

src/organization_domains.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//! A module for interacting with the WorkOS Organization Domains API.
2+
//!
3+
//! [WorkOS Docs: Domain Verification Guide](https://workos.com/docs/domain-verification/guide)
4+
5+
mod operations;
6+
mod types;
7+
8+
pub use operations::*;
9+
pub use types::*;
10+
11+
use crate::WorkOs;
12+
13+
/// Organization Domains.
14+
///
15+
/// [WorkOS Docs: Domain Verification Guide](https://workos.com/docs/domain-verification/guide)
16+
pub struct OrganizationDomains<'a> {
17+
workos: &'a WorkOs,
18+
}
19+
20+
impl<'a> OrganizationDomains<'a> {
21+
/// Returns a new [`OrganizationDomains`] instance for the provided WorkOS client.
22+
pub fn new(workos: &'a WorkOs) -> Self {
23+
Self { workos }
24+
}
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
mod create_organization_domain;
2+
mod delete_organization_domain;
3+
mod get_organization_domain;
4+
mod verify_organization_domain;
5+
6+
pub use create_organization_domain::*;
7+
pub use delete_organization_domain::*;
8+
pub use get_organization_domain::*;
9+
pub use verify_organization_domain::*;
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
use async_trait::async_trait;
2+
use serde::Serialize;
3+
use thiserror::Error;
4+
5+
use crate::organization_domains::{OrganizationDomain, OrganizationDomains};
6+
use crate::organizations::OrganizationId;
7+
use crate::{ResponseExt, WorkOsError, WorkOsResult};
8+
9+
/// The parameters for [`CreateOrganizationDomain`].
10+
#[derive(Debug, Serialize)]
11+
pub struct CreateOrganizationDomainParams<'a> {
12+
/// ID of the parent Organization.
13+
pub organization_id: &'a OrganizationId,
14+
15+
/// Domain for the organization domain.
16+
pub domain: &'a str,
17+
}
18+
19+
/// An error returned from [`CreateOrganizationDomain`].
20+
#[derive(Debug, Error)]
21+
pub enum CreateOrganizationDomainError {}
22+
23+
impl From<CreateOrganizationDomainError> for WorkOsError<CreateOrganizationDomainError> {
24+
fn from(err: CreateOrganizationDomainError) -> Self {
25+
Self::Operation(err)
26+
}
27+
}
28+
29+
/// [WorkOS Docs: Create an Organization Domain](https://workos.com/docs/reference/domain-verification/create)
30+
#[async_trait]
31+
pub trait CreateOrganizationDomain {
32+
/// Creates a new Organization Domain.
33+
///
34+
/// [WorkOS Docs: Create an Organization Domain](https://workos.com/docs/reference/domain-verification/create)
35+
///
36+
/// # Examples
37+
///
38+
/// ```
39+
/// # use workos::WorkOsResult;
40+
/// # use workos::organization_domains::*;
41+
/// use workos::organizations::OrganizationId;
42+
/// use workos::{ApiKey, Metadata, WorkOs};
43+
///
44+
/// # async fn run() -> WorkOsResult<(), CreateOrganizationDomainError> {
45+
/// let workos = WorkOs::new(&ApiKey::from("sk_example_123456789"));
46+
///
47+
/// let organization_domain = workos
48+
/// .organization_domains()
49+
/// .create_organization_domain(&CreateOrganizationDomainParams {
50+
/// organization_id: &OrganizationId::from("org_01EHT88Z8J8795GZNQ4ZP1J81T"),
51+
/// domain: "foo-corp.com",
52+
/// })
53+
/// .await?;
54+
/// # Ok(())
55+
/// # }
56+
/// ```
57+
async fn create_organization_domain(
58+
&self,
59+
params: &CreateOrganizationDomainParams<'_>,
60+
) -> WorkOsResult<OrganizationDomain, CreateOrganizationDomainError>;
61+
}
62+
63+
#[async_trait]
64+
impl CreateOrganizationDomain for OrganizationDomains<'_> {
65+
async fn create_organization_domain(
66+
&self,
67+
params: &CreateOrganizationDomainParams<'_>,
68+
) -> WorkOsResult<OrganizationDomain, CreateOrganizationDomainError> {
69+
let url = self.workos.base_url().join("/organization_domains")?;
70+
71+
let organization = self
72+
.workos
73+
.client()
74+
.post(url)
75+
.bearer_auth(self.workos.key())
76+
.json(&params)
77+
.send()
78+
.await?
79+
.handle_unauthorized_or_generic_error()
80+
.await?
81+
.json::<OrganizationDomain>()
82+
.await?;
83+
84+
Ok(organization)
85+
}
86+
}
87+
88+
#[cfg(test)]
89+
mod test {
90+
use serde_json::json;
91+
use tokio;
92+
93+
use crate::organization_domains::OrganizationDomainId;
94+
use crate::organizations::OrganizationId;
95+
use crate::{ApiKey, WorkOs};
96+
97+
use super::*;
98+
99+
#[tokio::test]
100+
async fn it_calls_the_create_organization_domain_endpoint() {
101+
let mut server = mockito::Server::new_async().await;
102+
103+
let workos = WorkOs::builder(&ApiKey::from("sk_example_123456789"))
104+
.base_url(&server.url())
105+
.unwrap()
106+
.build();
107+
108+
server
109+
.mock("POST", "/organization_domains")
110+
.match_header("Authorization", "Bearer sk_example_123456789")
111+
.with_status(201)
112+
.with_body(
113+
json!({
114+
"object": "organization_domain",
115+
"id": "org_domain_01HEJXJSTVEDT7T58BM70FMFET",
116+
"organization_id": "org_01EHT88Z8J8795GZNQ4ZP1J81T",
117+
"domain": "foo-corp.com",
118+
"state": "pending",
119+
"verification_strategy": "dns",
120+
"verification_token": "aW5HQ8Sgps1y3LQyrShsFRo3F",
121+
"created_at": "2021-06-25T19:07:33.155Z",
122+
"updated_at": "2021-06-25T19:07:33.155Z"
123+
})
124+
.to_string(),
125+
)
126+
.create_async()
127+
.await;
128+
129+
let organization_domain = workos
130+
.organization_domains()
131+
.create_organization_domain(&CreateOrganizationDomainParams {
132+
organization_id: &OrganizationId::from("org_01EHT88Z8J8795GZNQ4ZP1J81T"),
133+
domain: "foo-corp.com",
134+
})
135+
.await
136+
.unwrap();
137+
138+
assert_eq!(
139+
organization_domain.id,
140+
OrganizationDomainId::from("org_domain_01HEJXJSTVEDT7T58BM70FMFET")
141+
)
142+
}
143+
}

0 commit comments

Comments
 (0)