clerk_sdk_rust_community/apis/
organizations_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_organization`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum CreateOrganizationError {
22    Status400(crate::models::ClerkErrors),
23    Status403(crate::models::ClerkErrors),
24    Status422(crate::models::ClerkErrors),
25    UnknownValue(serde_json::Value),
26}
27
28/// struct for typed errors of method [`delete_organization`]
29#[derive(Debug, Clone, Serialize, Deserialize)]
30#[serde(untagged)]
31pub enum DeleteOrganizationError {
32    Status404(crate::models::ClerkErrors),
33    UnknownValue(serde_json::Value),
34}
35
36/// struct for typed errors of method [`get_organization`]
37#[derive(Debug, Clone, Serialize, Deserialize)]
38#[serde(untagged)]
39pub enum GetOrganizationError {
40    Status403(crate::models::ClerkErrors),
41    Status404(crate::models::ClerkErrors),
42    UnknownValue(serde_json::Value),
43}
44
45/// struct for typed errors of method [`list_organizations`]
46#[derive(Debug, Clone, Serialize, Deserialize)]
47#[serde(untagged)]
48pub enum ListOrganizationsError {
49    Status400(crate::models::ClerkErrors),
50    Status403(crate::models::ClerkErrors),
51    Status422(crate::models::ClerkErrors),
52    UnknownValue(serde_json::Value),
53}
54
55/// struct for typed errors of method [`merge_organization_metadata`]
56#[derive(Debug, Clone, Serialize, Deserialize)]
57#[serde(untagged)]
58pub enum MergeOrganizationMetadataError {
59    Status400(crate::models::ClerkErrors),
60    Status401(crate::models::ClerkErrors),
61    Status404(crate::models::ClerkErrors),
62    Status422(crate::models::ClerkErrors),
63    UnknownValue(serde_json::Value),
64}
65
66/// struct for typed errors of method [`update_organization`]
67#[derive(Debug, Clone, Serialize, Deserialize)]
68#[serde(untagged)]
69pub enum UpdateOrganizationError {
70    Status404(crate::models::ClerkErrors),
71    Status422(crate::models::ClerkErrors),
72    UnknownValue(serde_json::Value),
73}
74
75/// struct for typed errors of method [`upload_organization_logo`]
76#[derive(Debug, Clone, Serialize, Deserialize)]
77#[serde(untagged)]
78pub enum UploadOrganizationLogoError {
79    Status400(crate::models::ClerkErrors),
80    Status403(crate::models::ClerkErrors),
81    Status404(crate::models::ClerkErrors),
82    Status413(crate::models::ClerkErrors),
83    UnknownValue(serde_json::Value),
84}
85
86
87/// Creates a new organization with the given name for an instance. In order to successfully create an organization you need to provide the ID of the User who will become the organization administrator. You can specify an optional slug for the new organization. If provided, the organization slug can contain only lowercase alphanumeric characters (letters and digits) and the dash \"-\". Organization slugs must be unique for the instance. You can provide additional metadata for the organization and set any custom attribute you want. Organizations support private and public metadata. Private metadata can only be accessed from the Backend API. Public metadata can be accessed from the Backend API, and are read-only from the Frontend API.
88pub async fn create_organization(configuration: &configuration::Configuration, create_organization_request: Option<crate::models::CreateOrganizationRequest>) -> Result<crate::models::Organization, Error<CreateOrganizationError>> {
89    let local_var_configuration = configuration;
90
91    let local_var_client = &local_var_configuration.client;
92
93    let local_var_uri_str = format!("{}/organizations", local_var_configuration.base_path);
94    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
95
96    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
97        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
98    }
99    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
100        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
101    };
102    local_var_req_builder = local_var_req_builder.json(&create_organization_request);
103
104    let local_var_req = local_var_req_builder.build()?;
105    let local_var_resp = local_var_client.execute(local_var_req).await?;
106
107    let local_var_status = local_var_resp.status();
108    let local_var_content = local_var_resp.text().await?;
109
110    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
111        serde_json::from_str(&local_var_content).map_err(Error::from)
112    } else {
113        let local_var_entity: Option<CreateOrganizationError> = serde_json::from_str(&local_var_content).ok();
114        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
115        Err(Error::ResponseError(local_var_error))
116    }
117}
118
119/// Deletes the given organization. Please note that deleting an organization will also delete all memberships and invitations. This is not reversible.
120pub async fn delete_organization(configuration: &configuration::Configuration, organization_id: &str) -> Result<crate::models::DeletedObject, Error<DeleteOrganizationError>> {
121    let local_var_configuration = configuration;
122
123    let local_var_client = &local_var_configuration.client;
124
125    let local_var_uri_str = format!("{}/organizations/{organization_id}", local_var_configuration.base_path, organization_id=crate::apis::urlencode(organization_id));
126    let mut local_var_req_builder = local_var_client.request(reqwest::Method::DELETE, 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    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
132        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
133    };
134
135    let local_var_req = local_var_req_builder.build()?;
136    let local_var_resp = local_var_client.execute(local_var_req).await?;
137
138    let local_var_status = local_var_resp.status();
139    let local_var_content = local_var_resp.text().await?;
140
141    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
142        serde_json::from_str(&local_var_content).map_err(Error::from)
143    } else {
144        let local_var_entity: Option<DeleteOrganizationError> = serde_json::from_str(&local_var_content).ok();
145        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
146        Err(Error::ResponseError(local_var_error))
147    }
148}
149
150/// Fetches the organization whose ID or slug matches the provided `id_or_slug` URL query parameter.
151pub async fn get_organization(configuration: &configuration::Configuration, organization_id: &str) -> Result<crate::models::Organization, Error<GetOrganizationError>> {
152    let local_var_configuration = configuration;
153
154    let local_var_client = &local_var_configuration.client;
155
156    let local_var_uri_str = format!("{}/organizations/{organization_id}", local_var_configuration.base_path, organization_id=crate::apis::urlencode(organization_id));
157    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
158
159    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
160        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
161    }
162    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
163        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
164    };
165
166    let local_var_req = local_var_req_builder.build()?;
167    let local_var_resp = local_var_client.execute(local_var_req).await?;
168
169    let local_var_status = local_var_resp.status();
170    let local_var_content = local_var_resp.text().await?;
171
172    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
173        serde_json::from_str(&local_var_content).map_err(Error::from)
174    } else {
175        let local_var_entity: Option<GetOrganizationError> = serde_json::from_str(&local_var_content).ok();
176        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
177        Err(Error::ResponseError(local_var_error))
178    }
179}
180
181/// This request returns the list of organizations for an instance. Results can be paginated using the optional `limit` and `offset` query parameters. The organizations are ordered by descending creation date. Most recent organizations will be returned first.
182pub async fn list_organizations(configuration: &configuration::Configuration, limit: Option<f32>, offset: Option<f32>, include_members_count: Option<bool>, query: Option<&str>) -> Result<crate::models::Organizations, Error<ListOrganizationsError>> {
183    let local_var_configuration = configuration;
184
185    let local_var_client = &local_var_configuration.client;
186
187    let local_var_uri_str = format!("{}/organizations", local_var_configuration.base_path);
188    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
189
190    if let Some(ref local_var_str) = limit {
191        local_var_req_builder = local_var_req_builder.query(&[("limit", &local_var_str.to_string())]);
192    }
193    if let Some(ref local_var_str) = offset {
194        local_var_req_builder = local_var_req_builder.query(&[("offset", &local_var_str.to_string())]);
195    }
196    if let Some(ref local_var_str) = include_members_count {
197        local_var_req_builder = local_var_req_builder.query(&[("include_members_count", &local_var_str.to_string())]);
198    }
199    if let Some(ref local_var_str) = query {
200        local_var_req_builder = local_var_req_builder.query(&[("query", &local_var_str.to_string())]);
201    }
202    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
203        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
204    }
205    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
206        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
207    };
208
209    let local_var_req = local_var_req_builder.build()?;
210    let local_var_resp = local_var_client.execute(local_var_req).await?;
211
212    let local_var_status = local_var_resp.status();
213    let local_var_content = local_var_resp.text().await?;
214
215    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
216        serde_json::from_str(&local_var_content).map_err(Error::from)
217    } else {
218        let local_var_entity: Option<ListOrganizationsError> = serde_json::from_str(&local_var_content).ok();
219        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
220        Err(Error::ResponseError(local_var_error))
221    }
222}
223
224/// Update organization metadata attributes by merging existing values with the provided parameters. Metadata values will be updated via a deep merge. Deep meaning that any nested JSON objects will be merged as well. You can remove metadata keys at any level by setting their value to `null`.
225pub async fn merge_organization_metadata(configuration: &configuration::Configuration, organization_id: &str, merge_organization_metadata_request: crate::models::MergeOrganizationMetadataRequest) -> Result<crate::models::Organization, Error<MergeOrganizationMetadataError>> {
226    let local_var_configuration = configuration;
227
228    let local_var_client = &local_var_configuration.client;
229
230    let local_var_uri_str = format!("{}/organizations/{organization_id}/metadata", local_var_configuration.base_path, organization_id=crate::apis::urlencode(organization_id));
231    let mut local_var_req_builder = local_var_client.request(reqwest::Method::PATCH, local_var_uri_str.as_str());
232
233    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
234        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
235    }
236    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
237        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
238    };
239    local_var_req_builder = local_var_req_builder.json(&merge_organization_metadata_request);
240
241    let local_var_req = local_var_req_builder.build()?;
242    let local_var_resp = local_var_client.execute(local_var_req).await?;
243
244    let local_var_status = local_var_resp.status();
245    let local_var_content = local_var_resp.text().await?;
246
247    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
248        serde_json::from_str(&local_var_content).map_err(Error::from)
249    } else {
250        let local_var_entity: Option<MergeOrganizationMetadataError> = serde_json::from_str(&local_var_content).ok();
251        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
252        Err(Error::ResponseError(local_var_error))
253    }
254}
255
256/// Updates an existing organization
257pub async fn update_organization(configuration: &configuration::Configuration, organization_id: &str, update_organization_request: crate::models::UpdateOrganizationRequest) -> Result<crate::models::Organization, Error<UpdateOrganizationError>> {
258    let local_var_configuration = configuration;
259
260    let local_var_client = &local_var_configuration.client;
261
262    let local_var_uri_str = format!("{}/organizations/{organization_id}", local_var_configuration.base_path, organization_id=crate::apis::urlencode(organization_id));
263    let mut local_var_req_builder = local_var_client.request(reqwest::Method::PATCH, local_var_uri_str.as_str());
264
265    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
266        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
267    }
268    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
269        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
270    };
271    local_var_req_builder = local_var_req_builder.json(&update_organization_request);
272
273    let local_var_req = local_var_req_builder.build()?;
274    let local_var_resp = local_var_client.execute(local_var_req).await?;
275
276    let local_var_status = local_var_resp.status();
277    let local_var_content = local_var_resp.text().await?;
278
279    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
280        serde_json::from_str(&local_var_content).map_err(Error::from)
281    } else {
282        let local_var_entity: Option<UpdateOrganizationError> = serde_json::from_str(&local_var_content).ok();
283        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
284        Err(Error::ResponseError(local_var_error))
285    }
286}
287
288/// Set or replace an organization's logo, by uploading an image file. This endpoint uses the `multipart/form-data` request content type and accepts a file of image type. The file size cannot exceed 10MB. Only the following file content types are supported: `image/jpeg`, `image/png`, `image/gif`, `image/webp`, `image/x-icon`, `image/vnd.microsoft.icon`.
289pub async fn upload_organization_logo(configuration: &configuration::Configuration, organization_id: &str, uploader_user_id: Option<&str>, file: Option<std::path::PathBuf>) -> Result<crate::models::OrganizationWithLogo, Error<UploadOrganizationLogoError>> {
290    let local_var_configuration = configuration;
291
292    let local_var_client = &local_var_configuration.client;
293
294    let local_var_uri_str = format!("{}/organizations/{organization_id}/logo", local_var_configuration.base_path, organization_id=crate::apis::urlencode(organization_id));
295    let mut local_var_req_builder = local_var_client.request(reqwest::Method::PUT, local_var_uri_str.as_str());
296
297    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
298        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
299    }
300    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
301        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
302    };
303    let mut local_var_form = reqwest::multipart::Form::new();
304    if let Some(local_var_param_value) = uploader_user_id {
305        local_var_form = local_var_form.text("uploader_user_id", local_var_param_value.to_string());
306    }
307    // TODO: support file upload for 'file' parameter
308    local_var_req_builder = local_var_req_builder.multipart(local_var_form);
309
310    let local_var_req = local_var_req_builder.build()?;
311    let local_var_resp = local_var_client.execute(local_var_req).await?;
312
313    let local_var_status = local_var_resp.status();
314    let local_var_content = local_var_resp.text().await?;
315
316    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
317        serde_json::from_str(&local_var_content).map_err(Error::from)
318    } else {
319        let local_var_entity: Option<UploadOrganizationLogoError> = serde_json::from_str(&local_var_content).ok();
320        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
321        Err(Error::ResponseError(local_var_error))
322    }
323}
324