Skip to main content

komga_sdk/apis/
api_keys_api.rs

1/*
2 * Komga API
3 *
4 * Komga REST API.  ## Reference  Check the API reference: - on the [Komga website](https://komga.org/docs/openapi/komga-api) - on any running Komga instance at `/swagger-ui.html` - on [GitHub](https://raw.githubusercontent.com/gotson/komga/refs/heads/master/komga/docs/openapi.json)  ## Authentication  Most endpoints require authentication. Authentication is done using either: - Basic Authentication - Passing an API Key in the `X-API-Key` header  ## Sessions  Upon successful authentication, a session is created, and can be reused.  - By default, a `KOMGA-SESSION` cookie is set via `Set-Cookie` response header. This works well for browsers and clients that can handle cookies. - If you specify a header `X-Auth-Token` during authentication, the session ID will be returned via this same header. You can then pass that header again for subsequent requests to reuse the session.  If you need to set the session cookie later on, you can call `/api/v1/login/set-cookie` with `X-Auth-Token`. The response will contain the `Set-Cookie` header.  ## Remember Me  During authentication, if a request parameter `remember-me` is passed and set to `true`, the server will also return a `komga-remember-me` cookie. This cookie will be used to login automatically even if the session has expired.  ## Logout  You can explicitly logout an existing session by calling `/api/logout`. This would return a `204`.  ## Deprecation  API endpoints marked as deprecated will be removed in the next major version.
5 *
6 * The version of the OpenAPI document: 1.23.4
7 * 
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`create_api_key_for_current_user`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum CreateApiKeyForCurrentUserError {
22    Status400(models::ValidationErrorResponse),
23    UnknownValue(serde_json::Value),
24}
25
26/// struct for typed errors of method [`delete_api_key_by_key_id`]
27#[derive(Debug, Clone, Serialize, Deserialize)]
28#[serde(untagged)]
29pub enum DeleteApiKeyByKeyIdError {
30    Status400(models::ValidationErrorResponse),
31    UnknownValue(serde_json::Value),
32}
33
34/// struct for typed errors of method [`get_api_keys_for_current_user`]
35#[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    // add a prefix to parameters to efficiently prevent name collisions
45    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    // add a prefix to parameters to efficiently prevent name collisions
93    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&lt;models::ApiKeyDto&gt;`"))),
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&lt;models::ApiKeyDto&gt;`")))),
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