1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//! API key management.
use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::{client::Sendry, error::Error, DeleteResponse, Page};
/// API keys resource handle.
#[derive(Debug, Clone)]
pub struct ApiKeys {
client: Sendry,
}
impl ApiKeys {
pub(crate) fn new(client: Sendry) -> Self {
Self { client }
}
/// Create a new API key. The plaintext `key` is only returned once.
pub async fn create(&self, params: CreateApiKey) -> Result<ApiKeyCreated, Error> {
self.client
.request(
self.client
.build(Method::POST, "/v1/api-keys", &[], Some(¶ms)),
)
.await
}
/// List API keys (values are masked, only prefix is shown).
pub async fn list(&self) -> Result<Page<ApiKey>, Error> {
self.client
.request(
self.client
.build::<()>(Method::GET, "/v1/api-keys", &[], None),
)
.await
}
/// Revoke (delete) an API key.
pub async fn remove(&self, id: &str) -> Result<DeleteResponse, Error> {
self.client
.request(self.client.build::<()>(
Method::DELETE,
&format!("/v1/api-keys/{id}"),
&[],
None,
))
.await
}
}
/// Parameters for creating an API key.
#[derive(Debug, Clone, Serialize)]
pub struct CreateApiKey {
/// Display name.
pub name: String,
/// Scope: `full_access`, `sending_access`, or `read_only`.
#[serde(skip_serializing_if = "Option::is_none")]
pub scope: Option<String>,
}
/// API key record (key value is masked).
#[derive(Debug, Clone, Deserialize)]
pub struct ApiKey {
/// API key id.
pub id: String,
/// Display name.
pub name: String,
/// Scope name.
pub scope: String,
/// Visible prefix (first few characters).
pub key_prefix: String,
/// Timestamp of last use, if any.
pub last_used_at: Option<String>,
/// Creation timestamp.
pub created_at: String,
}
/// Response from [`ApiKeys::create`]. Includes the full plaintext key — store it now.
#[derive(Debug, Clone, Deserialize)]
pub struct ApiKeyCreated {
/// API key id.
pub id: String,
/// Display name.
pub name: String,
/// Scope name.
pub scope: String,
/// The full plaintext API key. Only returned once.
pub key: String,
/// Creation timestamp.
pub created_at: String,
}