use std::sync::Arc;
use serde::Serialize;
use crate::error::Result;
use crate::http::HttpClient;
use crate::resources::{enc, to_value};
use crate::response::{Page, Response};
use crate::transport::Method;
use crate::types::params::ListParams;
pub struct ApiKeys {
http: Arc<HttpClient>,
}
impl ApiKeys {
pub(crate) fn new(http: Arc<HttpClient>) -> Self {
Self { http }
}
pub async fn list(&self, params: ListParams) -> Result<Page> {
self.http
.list(
"/api-keys",
vec![
("limit", params.limit.map(|n| n.to_string())),
("after", params.after),
],
)
.await
}
pub async fn list_all(&self, params: ListParams) -> Result<Vec<Response>> {
self.http
.list_all(
"/api-keys",
vec![("limit", params.limit.map(|n| n.to_string()))],
)
.await
}
pub async fn create(&self, body: impl Serialize) -> Result<Response> {
self.http
.request_object(
Method::Post,
"/api-keys",
Some(to_value(body)?),
false,
None,
)
.await
}
pub async fn get(&self, id: &str) -> Result<Response> {
self.http
.request_object(
Method::Get,
&format!("/api-keys/{}", enc(id)),
None,
false,
None,
)
.await
}
pub async fn update(&self, id: &str, body: impl Serialize) -> Result<Response> {
self.http
.request_object(
Method::Patch,
&format!("/api-keys/{}", enc(id)),
Some(to_value(body)?),
false,
None,
)
.await
}
pub async fn delete(&self, id: &str) -> Result<()> {
self.http
.request_empty(Method::Delete, &format!("/api-keys/{}", enc(id)))
.await
}
}