sendgrid_api/
api_keys.rs

1use crate::Client;
2use crate::ClientResult;
3
4pub struct ApiKeys {
5    pub client: Client,
6}
7
8impl ApiKeys {
9    #[doc(hidden)]
10    pub fn new(client: Client) -> Self {
11        ApiKeys { client }
12    }
13
14    /**
15     * Retrieve all API Keys belonging to the authenticated user.
16     *
17     * This function performs a `GET` to the `/api_keys` endpoint.
18     *
19     * **This endpoint allows you to retrieve all API Keys that belong to the authenticated user.**
20     *
21     * A successful response from this API will include all available API keys' names and IDs.
22     *
23     * For security reasons, there is not a way to retrieve the key itself after it's created. If you lose your API key, you must create a new one. Only the "Create API keys" endpoint will return a key to you and only at the time of creation.
24     *
25     * An `api_key_id` can be used to update or delete the key, as well as retrieve the key's details, such as its scopes.
26     *
27     * **Parameters:**
28     *
29     * * `limit: i64`
30     * * `on_behalf_of: &str` -- The license key provided with your New Relic account.
31     */
32    pub async fn get(
33        &self,
34        limit: i64,
35    ) -> ClientResult<crate::Response<crate::types::GetApiKeysResponse>> {
36        let mut query_args: Vec<(String, String)> = Default::default();
37        if limit > 0 {
38            query_args.push(("limit".to_string(), limit.to_string()));
39        }
40        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
41        let url = self.client.url(&format!("/api_keys?{}", query_), None);
42        self.client
43            .get(
44                &url,
45                crate::Message {
46                    body: None,
47                    content_type: None,
48                },
49            )
50            .await
51    }
52    /**
53     * Create API keys.
54     *
55     * This function performs a `POST` to the `/api_keys` endpoint.
56     *
57     * **This endpoint allows you to create a new API Key for the user.**
58     *
59     * To create your initial SendGrid API Key, you should [use the SendGrid App](https://app.sendgrid.com/settings/api_keys). Once you have created a first key with scopes to manage additional API keys, you can use this API for all other key management.
60     *
61     * > There is a limit of 100 API Keys on your account.
62     *
63     * A JSON request body containing a `name` property is required when making requests to this endpoint. If the number of maximum keys, 100, is reached, a `403` status will be returned.
64     *
65     * Though the `name` field is required, it does not need to be unique. A unique API key ID will be generated for each key you create and returned in the response body.
66     *
67     * It is not necessary to pass a `scopes` field to the API when creating a key, but you should be aware that omitting the `scopes` field from your request will create a key with "Full Access" permissions by default.
68     *
69     * See the [API Key Permissions List](https://sendgrid.api-docs.io/v3.0/how-to-use-the-sendgrid-v3-api/api-authorization) for all available scopes. An API key's scopes can be updated after creation using the "Update API keys" endpoint.
70     *
71     * **Parameters:**
72     *
73     * * `on_behalf_of: &str` -- The license key provided with your New Relic account.
74     */
75    pub async fn create(
76        &self,
77        body: &crate::types::CreateApiKeysRequest,
78    ) -> ClientResult<crate::Response<crate::types::CreateApiKeysResponse>> {
79        let url = self.client.url("/api_keys", None);
80        self.client
81            .post(
82                &url,
83                crate::Message {
84                    body: Some(reqwest::Body::from(serde_json::to_vec(body)?)),
85                    content_type: Some("application/json".to_string()),
86                },
87            )
88            .await
89    }
90    /**
91     * Retrieve an existing API Key.
92     *
93     * This function performs a `GET` to the `/api_keys/{api_key_id}` endpoint.
94     *
95     * **This endpoint allows you to retrieve a single API key using an `api_key_id`.**
96     *
97     * The endpoint will return a key's name, ID, and scopes. If the API Key ID does not, exist a `404` status will be returned.
98     *
99     * See the [API Key Permissions List](https://sendgrid.api-docs.io/v3.0/how-to-use-the-sendgrid-v3-api/api-authorization) for all available scopes. An API key's scopes can be updated after creation using the "Update API keys" endpoint.
100     *
101     * **Parameters:**
102     *
103     * * `on_behalf_of: &str` -- The license key provided with your New Relic account.
104     */
105    pub async fn get_key(
106        &self,
107        api_key_id: &str,
108    ) -> ClientResult<crate::Response<crate::types::GetApiKeysKeyResponse>> {
109        let url = self.client.url(
110            &format!(
111                "/api_keys/{}",
112                crate::progenitor_support::encode_path(api_key_id),
113            ),
114            None,
115        );
116        self.client
117            .get(
118                &url,
119                crate::Message {
120                    body: None,
121                    content_type: None,
122                },
123            )
124            .await
125    }
126    /**
127     * Update API key name and scopes.
128     *
129     * This function performs a `PUT` to the `/api_keys/{api_key_id}` endpoint.
130     *
131     * **This endpoint allows you to update the name and scopes of a given API key.**
132     *
133     * You must pass this endpoint a JSON request body with a `name` field and a `scopes` array containing at least one scope. The `name` and `scopes` fields will be used to update the key associated with the `api_key_id` in the request URL.
134     *
135     * If you need to update a key's scopes only, pass the `name` field with the key's existing name; the `name` will not be modified. If you need to update a key's name only, use the "Update API key name" endpoint.
136     *
137     * See the [API Key Permissions List](https://sendgrid.api-docs.io/v3.0/how-to-use-the-sendgrid-v3-api/api-authorization) for all available scopes.
138     *
139     * **Parameters:**
140     *
141     * * `on_behalf_of: &str` -- The license key provided with your New Relic account.
142     */
143    pub async fn put_key(
144        &self,
145        api_key_id: &str,
146        body: &crate::types::PutApiKeysKeyRequest,
147    ) -> ClientResult<crate::Response<crate::types::ApiKeyNameScopesAllOf>> {
148        let url = self.client.url(
149            &format!(
150                "/api_keys/{}",
151                crate::progenitor_support::encode_path(api_key_id),
152            ),
153            None,
154        );
155        self.client
156            .put(
157                &url,
158                crate::Message {
159                    body: Some(reqwest::Body::from(serde_json::to_vec(body)?)),
160                    content_type: Some("application/json".to_string()),
161                },
162            )
163            .await
164    }
165    /**
166     * Delete API keys.
167     *
168     * This function performs a `DELETE` to the `/api_keys/{api_key_id}` endpoint.
169     *
170     * **This endpoint allows you to revoke an existing API Key using an `api_key_id`**
171     *
172     * Authentications using a revoked API Key will fail after after some small propogation delay. If the API Key ID does not exist, a `404` status will be returned.
173     *
174     * **Parameters:**
175     *
176     * * `on_behalf_of: &str` -- The license key provided with your New Relic account.
177     */
178    pub async fn delete_key(&self, api_key_id: &str) -> ClientResult<crate::Response<()>> {
179        let url = self.client.url(
180            &format!(
181                "/api_keys/{}",
182                crate::progenitor_support::encode_path(api_key_id),
183            ),
184            None,
185        );
186        self.client
187            .delete(
188                &url,
189                crate::Message {
190                    body: None,
191                    content_type: None,
192                },
193            )
194            .await
195    }
196    /**
197     * Update API key name.
198     *
199     * This function performs a `PATCH` to the `/api_keys/{api_key_id}` endpoint.
200     *
201     * **This endpoint allows you to update the name of an existing API Key.**
202     *
203     * You must pass this endpoint a JSON request body with a `name` property, which will be used to rename the key associated with the `api_key_id` passed in the URL.
204     *
205     * **Parameters:**
206     *
207     * * `on_behalf_of: &str` -- The license key provided with your New Relic account.
208     */
209    pub async fn patch_key(
210        &self,
211        api_key_id: &str,
212        body: &crate::types::IpPool,
213    ) -> ClientResult<crate::Response<crate::types::ApiKeyNameId>> {
214        let url = self.client.url(
215            &format!(
216                "/api_keys/{}",
217                crate::progenitor_support::encode_path(api_key_id),
218            ),
219            None,
220        );
221        self.client
222            .patch(
223                &url,
224                crate::Message {
225                    body: Some(reqwest::Body::from(serde_json::to_vec(body)?)),
226                    content_type: Some("application/json".to_string()),
227                },
228            )
229            .await
230    }
231}