use std::fmt;
use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::error::Result;
use crate::http::HttpClient;
#[derive(Clone, Deserialize)]
#[non_exhaustive]
pub struct ApiKeyResponse {
pub api_key: Option<String>,
}
impl fmt::Debug for ApiKeyResponse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ApiKeyResponse")
.field("api_key", &self.api_key.as_ref().map(|_| "**redacted**"))
.finish()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateApiKeyBody {
pub password: String,
}
impl CreateApiKeyBody {
pub fn new<S: Into<String>>(password: S) -> Self {
Self {
password: password.into(),
}
}
}
#[derive(Debug)]
pub struct ApiKeysApi<'a> {
http: &'a HttpClient,
}
impl<'a> ApiKeysApi<'a> {
pub(crate) fn new(http: &'a HttpClient) -> Self {
Self { http }
}
pub async fn create(&self, body: &CreateApiKeyBody) -> Result<ApiKeyResponse> {
let req = self
.http
.request(Method::POST, "users/api-keys")?
.json(body);
self.http.send_envelope(req).await
}
pub async fn get(&self) -> Result<ApiKeyResponse> {
let req = self.http.request(Method::GET, "users/api-keys")?;
self.http.send_envelope(req).await
}
pub async fn delete(&self) -> Result<()> {
let req = self.http.request(Method::DELETE, "users/api-keys")?;
self.http.send_no_content(req).await
}
}