clerk_rs/apis/
invitations_api.rs

1/*
2 * Clerk Backend API
3 *
4 * The Clerk REST Backend API, meant to be accessed by backend servers. Please see https://clerk.com/docs for more information.
5 *
6 * The version of the OpenAPI document: v1
7 * Contact: support@clerk.com
8 * Generated by: https://openapi-generator.tech
9 */
10use reqwest;
11
12use super::Error;
13use crate::{apis::ResponseContent, clerk::Clerk};
14
15/// struct for typed errors of method [`create_invitation`]
16#[derive(Debug, Clone, Serialize, Deserialize)]
17#[serde(untagged)]
18pub enum CreateInvitationError {
19	Status400(crate::models::ClerkErrors),
20	Status422(crate::models::ClerkErrors),
21	UnknownValue(serde_json::Value),
22}
23
24/// struct for typed errors of method [`list_invitations`]
25#[derive(Debug, Clone, Serialize, Deserialize)]
26#[serde(untagged)]
27pub enum ListInvitationsError {
28	UnknownValue(serde_json::Value),
29}
30
31/// struct for typed errors of method [`revoke_invitation`]
32#[derive(Debug, Clone, Serialize, Deserialize)]
33#[serde(untagged)]
34pub enum RevokeInvitationError {
35	Status400(crate::models::ClerkErrors),
36	Status404(crate::models::ClerkErrors),
37	UnknownValue(serde_json::Value),
38}
39
40pub struct Invitation;
41
42impl Invitation {
43	/// Creates a new invitation for the given email address and sends the invitation email. Keep in mind that you cannot create an invitation if there is already one for the given email address. Also, trying to create an invitation for an email address that already exists in your application will result to an error.
44	pub async fn create_invitation(
45		clerk_client: &Clerk,
46		create_invitation_request: Option<crate::models::CreateInvitationRequest>,
47	) -> Result<crate::models::Invitation, Error<CreateInvitationError>> {
48		let local_var_configuration = &clerk_client.config;
49
50		let local_var_client = &local_var_configuration.client;
51
52		let local_var_uri_str = format!("{}/invitations", local_var_configuration.base_path);
53		let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
54
55		if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
56			local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
57		}
58
59		local_var_req_builder = local_var_req_builder.json(&create_invitation_request);
60
61		let local_var_req = local_var_req_builder.build()?;
62		let local_var_resp = local_var_client.execute(local_var_req).await?;
63
64		let local_var_status = local_var_resp.status();
65		let local_var_content = local_var_resp.text().await?;
66
67		if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
68			serde_json::from_str(&local_var_content).map_err(Error::from)
69		} else {
70			let local_var_entity: Option<CreateInvitationError> = serde_json::from_str(&local_var_content).ok();
71			let local_var_error = ResponseContent {
72				status: local_var_status,
73				content: local_var_content,
74				entity: local_var_entity,
75			};
76			Err(Error::ResponseError(local_var_error))
77		}
78	}
79
80	/// Returns all non-revoked invitations for your application, sorted by creation date
81	pub async fn list_invitations(clerk_client: &Clerk, status: Option<&str>) -> Result<Vec<crate::models::Invitation>, Error<ListInvitationsError>> {
82		let local_var_configuration = &clerk_client.config;
83
84		let local_var_client = &local_var_configuration.client;
85
86		let local_var_uri_str = format!("{}/invitations", local_var_configuration.base_path);
87		let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
88
89		if let Some(ref local_var_str) = status {
90			local_var_req_builder = local_var_req_builder.query(&[("status", &local_var_str.to_string())]);
91		}
92		if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
93			local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
94		}
95
96		let local_var_req = local_var_req_builder.build()?;
97		let local_var_resp = local_var_client.execute(local_var_req).await?;
98
99		let local_var_status = local_var_resp.status();
100		let local_var_content = local_var_resp.text().await?;
101
102		if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
103			serde_json::from_str(&local_var_content).map_err(Error::from)
104		} else {
105			let local_var_entity: Option<ListInvitationsError> = serde_json::from_str(&local_var_content).ok();
106			let local_var_error = ResponseContent {
107				status: local_var_status,
108				content: local_var_content,
109				entity: local_var_entity,
110			};
111			Err(Error::ResponseError(local_var_error))
112		}
113	}
114
115	/// Revokes the given invitation. Revoking an invitation will prevent the user from using the invitation link that was sent to them. However, it doesn't prevent the user from signing up if they follow the sign up flow. Only active (i.e. non-revoked) invitations can be revoked.
116	pub async fn revoke_invitation(clerk_client: &Clerk, invitation_id: &str) -> Result<crate::models::Invitation, Error<RevokeInvitationError>> {
117		let local_var_configuration = &clerk_client.config;
118
119		let local_var_client = &local_var_configuration.client;
120
121		let local_var_uri_str = format!(
122			"{}/invitations/{invitation_id}/revoke",
123			local_var_configuration.base_path,
124			invitation_id = crate::apis::urlencode(invitation_id)
125		);
126		let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
127
128		if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
129			local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
130		}
131
132		let local_var_req = local_var_req_builder.build()?;
133		let local_var_resp = local_var_client.execute(local_var_req).await?;
134
135		let local_var_status = local_var_resp.status();
136		let local_var_content = local_var_resp.text().await?;
137
138		if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
139			serde_json::from_str(&local_var_content).map_err(Error::from)
140		} else {
141			let local_var_entity: Option<RevokeInvitationError> = serde_json::from_str(&local_var_content).ok();
142			let local_var_error = ResponseContent {
143				status: local_var_status,
144				content: local_var_content,
145				entity: local_var_entity,
146			};
147			Err(Error::ResponseError(local_var_error))
148		}
149	}
150}