clerk_sdk_rust_community/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.dev
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13
14use crate::apis::ResponseContent;
15use super::{Error, configuration};
16
17
18/// struct for typed errors of method [`create_invitation`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum CreateInvitationError {
22    Status400(crate::models::ClerkErrors),
23    Status422(crate::models::ClerkErrors),
24    UnknownValue(serde_json::Value),
25}
26
27/// struct for typed errors of method [`list_invitations`]
28#[derive(Debug, Clone, Serialize, Deserialize)]
29#[serde(untagged)]
30pub enum ListInvitationsError {
31    UnknownValue(serde_json::Value),
32}
33
34/// struct for typed errors of method [`revoke_invitation`]
35#[derive(Debug, Clone, Serialize, Deserialize)]
36#[serde(untagged)]
37pub enum RevokeInvitationError {
38    Status400(crate::models::ClerkErrors),
39    Status404(crate::models::ClerkErrors),
40    UnknownValue(serde_json::Value),
41}
42
43
44/// 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.
45pub async fn create_invitation(configuration: &configuration::Configuration, create_invitation_request: Option<crate::models::CreateInvitationRequest>) -> Result<crate::models::Invitation, Error<CreateInvitationError>> {
46    let local_var_configuration = configuration;
47
48    let local_var_client = &local_var_configuration.client;
49
50    let local_var_uri_str = format!("{}/invitations", local_var_configuration.base_path);
51    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
52
53    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
54        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
55    }
56    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
57        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
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 { status: local_var_status, content: local_var_content, entity: local_var_entity };
72        Err(Error::ResponseError(local_var_error))
73    }
74}
75
76/// Returns all non-revoked invitations for your application, sorted by creation date
77pub async fn list_invitations(configuration: &configuration::Configuration, status: Option<&str>) -> Result<Vec<crate::models::Invitation>, Error<ListInvitationsError>> {
78    let local_var_configuration = configuration;
79
80    let local_var_client = &local_var_configuration.client;
81
82    let local_var_uri_str = format!("{}/invitations", local_var_configuration.base_path);
83    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
84
85    if let Some(ref local_var_str) = status {
86        local_var_req_builder = local_var_req_builder.query(&[("status", &local_var_str.to_string())]);
87    }
88    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
89        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
90    }
91    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
92        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
93    };
94
95    let local_var_req = local_var_req_builder.build()?;
96    let local_var_resp = local_var_client.execute(local_var_req).await?;
97
98    let local_var_status = local_var_resp.status();
99    let local_var_content = local_var_resp.text().await?;
100
101    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
102        serde_json::from_str(&local_var_content).map_err(Error::from)
103    } else {
104        let local_var_entity: Option<ListInvitationsError> = serde_json::from_str(&local_var_content).ok();
105        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
106        Err(Error::ResponseError(local_var_error))
107    }
108}
109
110/// 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.
111pub async fn revoke_invitation(configuration: &configuration::Configuration, invitation_id: &str) -> Result<crate::models::Invitation, Error<RevokeInvitationError>> {
112    let local_var_configuration = configuration;
113
114    let local_var_client = &local_var_configuration.client;
115
116    let local_var_uri_str = format!("{}/invitations/{invitation_id}/revoke", local_var_configuration.base_path, invitation_id=crate::apis::urlencode(invitation_id));
117    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
118
119    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
120        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
121    }
122    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
123        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
124    };
125
126    let local_var_req = local_var_req_builder.build()?;
127    let local_var_resp = local_var_client.execute(local_var_req).await?;
128
129    let local_var_status = local_var_resp.status();
130    let local_var_content = local_var_resp.text().await?;
131
132    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
133        serde_json::from_str(&local_var_content).map_err(Error::from)
134    } else {
135        let local_var_entity: Option<RevokeInvitationError> = serde_json::from_str(&local_var_content).ok();
136        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
137        Err(Error::ResponseError(local_var_error))
138    }
139}
140