komga_sdk/apis/
api_keys_api.rs1use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum CreateApiKeyForCurrentUserError {
22 Status400(models::ValidationErrorResponse),
23 UnknownValue(serde_json::Value),
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
28#[serde(untagged)]
29pub enum DeleteApiKeyByKeyIdError {
30 Status400(models::ValidationErrorResponse),
31 UnknownValue(serde_json::Value),
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize)]
36#[serde(untagged)]
37pub enum GetApiKeysForCurrentUserError {
38 Status400(models::ValidationErrorResponse),
39 UnknownValue(serde_json::Value),
40}
41
42
43pub async fn create_api_key_for_current_user(configuration: &configuration::Configuration, api_key_request_dto: models::ApiKeyRequestDto) -> Result<models::ApiKeyDto, Error<CreateApiKeyForCurrentUserError>> {
44 let p_body_api_key_request_dto = api_key_request_dto;
46
47 let uri_str = format!("{}/api/v2/users/me/api-keys", configuration.base_path);
48 let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
49
50 if let Some(ref user_agent) = configuration.user_agent {
51 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
52 }
53 if let Some(ref apikey) = configuration.api_key {
54 let key = apikey.key.clone();
55 let value = match apikey.prefix {
56 Some(ref prefix) => format!("{} {}", prefix, key),
57 None => key,
58 };
59 req_builder = req_builder.header("X-API-Key", value);
60 };
61 if let Some(ref auth_conf) = configuration.basic_auth {
62 req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
63 };
64 req_builder = req_builder.json(&p_body_api_key_request_dto);
65
66 let req = req_builder.build()?;
67 let resp = configuration.client.execute(req).await?;
68
69 let status = resp.status();
70 let content_type = resp
71 .headers()
72 .get("content-type")
73 .and_then(|v| v.to_str().ok())
74 .unwrap_or("application/octet-stream");
75 let content_type = super::ContentType::from(content_type);
76
77 if !status.is_client_error() && !status.is_server_error() {
78 let content = resp.text().await?;
79 match content_type {
80 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
81 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ApiKeyDto`"))),
82 ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ApiKeyDto`")))),
83 }
84 } else {
85 let content = resp.text().await?;
86 let entity: Option<CreateApiKeyForCurrentUserError> = serde_json::from_str(&content).ok();
87 Err(Error::ResponseError(ResponseContent { status, content, entity }))
88 }
89}
90
91pub async fn delete_api_key_by_key_id(configuration: &configuration::Configuration, key_id: &str) -> Result<(), Error<DeleteApiKeyByKeyIdError>> {
92 let p_path_key_id = key_id;
94
95 let uri_str = format!("{}/api/v2/users/me/api-keys/{keyId}", configuration.base_path, keyId=crate::apis::urlencode(p_path_key_id));
96 let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);
97
98 if let Some(ref user_agent) = configuration.user_agent {
99 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
100 }
101 if let Some(ref apikey) = configuration.api_key {
102 let key = apikey.key.clone();
103 let value = match apikey.prefix {
104 Some(ref prefix) => format!("{} {}", prefix, key),
105 None => key,
106 };
107 req_builder = req_builder.header("X-API-Key", value);
108 };
109 if let Some(ref auth_conf) = configuration.basic_auth {
110 req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
111 };
112
113 let req = req_builder.build()?;
114 let resp = configuration.client.execute(req).await?;
115
116 let status = resp.status();
117
118 if !status.is_client_error() && !status.is_server_error() {
119 Ok(())
120 } else {
121 let content = resp.text().await?;
122 let entity: Option<DeleteApiKeyByKeyIdError> = serde_json::from_str(&content).ok();
123 Err(Error::ResponseError(ResponseContent { status, content, entity }))
124 }
125}
126
127pub async fn get_api_keys_for_current_user(configuration: &configuration::Configuration, ) -> Result<Vec<models::ApiKeyDto>, Error<GetApiKeysForCurrentUserError>> {
128
129 let uri_str = format!("{}/api/v2/users/me/api-keys", configuration.base_path);
130 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
131
132 if let Some(ref user_agent) = configuration.user_agent {
133 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
134 }
135 if let Some(ref apikey) = configuration.api_key {
136 let key = apikey.key.clone();
137 let value = match apikey.prefix {
138 Some(ref prefix) => format!("{} {}", prefix, key),
139 None => key,
140 };
141 req_builder = req_builder.header("X-API-Key", value);
142 };
143 if let Some(ref auth_conf) = configuration.basic_auth {
144 req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
145 };
146
147 let req = req_builder.build()?;
148 let resp = configuration.client.execute(req).await?;
149
150 let status = resp.status();
151 let content_type = resp
152 .headers()
153 .get("content-type")
154 .and_then(|v| v.to_str().ok())
155 .unwrap_or("application/octet-stream");
156 let content_type = super::ContentType::from(content_type);
157
158 if !status.is_client_error() && !status.is_server_error() {
159 let content = resp.text().await?;
160 match content_type {
161 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
162 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec<models::ApiKeyDto>`"))),
163 ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `Vec<models::ApiKeyDto>`")))),
164 }
165 } else {
166 let content = resp.text().await?;
167 let entity: Option<GetApiKeysForCurrentUserError> = serde_json::from_str(&content).ok();
168 Err(Error::ResponseError(ResponseContent { status, content, entity }))
169 }
170}
171