influxdb2/api/
authorizations.rs

1//! Authorizations (tokens) API.
2
3use reqwest::Method;
4use serde::{Deserialize, Serialize};
5use snafu::ResultExt;
6
7use crate::models::authorization::{Authorization, Authorizations, Status};
8use crate::models::permission::Permission;
9use crate::{Client, Http, RequestError, ReqwestProcessing, Serializing};
10
11impl Client {
12    /// List all authorization matching specified parameters
13    pub async fn list_authorizations(
14        &self,
15        request: ListAuthorizationsRequest,
16    ) -> Result<Authorizations, RequestError> {
17        let url = self.url("/api/v2/authorizations");
18
19        let response = self
20            .request(Method::GET, &url)
21            .query(&request)
22            .send()
23            .await
24            .context(ReqwestProcessing)?;
25
26        if !response.status().is_success() {
27            let status = response.status();
28            let text = response.text().await.context(ReqwestProcessing)?;
29            let res = Http { status, text }.fail();
30            return res;
31        }
32
33        response.json().await.context(ReqwestProcessing)
34    }
35
36    /// Create a new authorization in the organization.
37    pub async fn create_authorization(
38        &self,
39        request: CreateAuthorizationRequest,
40    ) -> Result<Authorization, RequestError> {
41        let create_bucket_url = self.url("/api/v2/authorizations");
42
43        let response = self
44            .request(Method::POST, &create_bucket_url)
45            .body(serde_json::to_string(&request).context(Serializing)?)
46            .send()
47            .await
48            .context(ReqwestProcessing)?;
49
50        if !response.status().is_success() {
51            let status = response.status();
52            let text = response.text().await.context(ReqwestProcessing)?;
53            let res = Http { status, text }.fail();
54            return res;
55        }
56
57        response
58            .json::<Authorization>()
59            .await
60            .context(ReqwestProcessing)
61    }
62}
63
64/// Request for listing authorizations.
65#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
66pub struct ListAuthorizationsRequest {
67    /// Only returns authorizations that belong to the specified organization.
68    #[serde(skip_serializing_if = "Option::is_none")]
69    pub org: Option<String>,
70    /// Only returns authorizations that belong to the specified organization ID.
71    #[serde(rename = "orgID")]
72    #[serde(skip_serializing_if = "Option::is_none")]
73    pub org_id: Option<String>,
74    /// Only returns the authorization that match the token value.
75    #[serde(skip_serializing_if = "Option::is_none")]
76    pub token: Option<String>,
77    /// Only returns the authorization scoped to the specified user.
78    #[serde(skip_serializing_if = "Option::is_none")]
79    pub user: Option<String>,
80    /// Only returns the authorization scoped to the specified user ID.
81    #[serde(rename = "userID")]
82    #[serde(skip_serializing_if = "Option::is_none")]
83    pub user_id: Option<String>,
84}
85
86/// Request for creating an authorization.
87#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
88#[serde(rename_all = "camelCase")]
89pub struct CreateAuthorizationRequest {
90    /// A description of the token.
91    #[serde(skip_serializing_if = "Option::is_none")]
92    pub description: Option<String>,
93    /// An organization ID. Specifies the organization that owns the authorization.
94    #[serde(rename = "orgID")]
95    pub org_id: String,
96    /// A list of permissions for an authorization (at least 1 required).
97    pub permissions: Vec<Permission>,
98    /// Status of the token after creation.
99    #[serde(skip_serializing_if = "Option::is_none")]
100    pub status: Option<Status>,
101    /// A user ID. Specifies the user that the authorization is scoped to.
102    #[serde(rename = "userID")]
103    #[serde(skip_serializing_if = "Option::is_none")]
104    pub user_id: Option<String>,
105}