use std::time::Duration;
use crate::error::VynFiError;
use crate::types::*;
pub struct Client {
inner: crate::Client,
rt: tokio::runtime::Runtime,
}
impl Client {
pub fn builder(api_key: impl Into<String>) -> ClientBuilder {
ClientBuilder {
api_key: api_key.into(),
base_url: None,
timeout: None,
max_retries: None,
}
}
pub fn jobs(&self) -> Jobs<'_> {
Jobs { client: self }
}
pub fn catalog(&self) -> Catalog<'_> {
Catalog { client: self }
}
pub fn usage(&self) -> Usage<'_> {
Usage { client: self }
}
pub fn api_keys(&self) -> ApiKeys<'_> {
ApiKeys { client: self }
}
pub fn credits(&self) -> Credits<'_> {
Credits { client: self }
}
fn block_on<F: std::future::Future>(&self, f: F) -> F::Output {
self.rt.block_on(f)
}
}
pub struct ClientBuilder {
api_key: String,
base_url: Option<String>,
timeout: Option<Duration>,
max_retries: Option<u32>,
}
impl ClientBuilder {
pub fn base_url(mut self, url: impl Into<String>) -> Self {
self.base_url = Some(url.into());
self
}
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}
pub fn max_retries(mut self, retries: u32) -> Self {
self.max_retries = Some(retries);
self
}
pub fn build(self) -> Result<Client, VynFiError> {
let mut builder = crate::Client::builder(&self.api_key);
if let Some(url) = self.base_url {
builder = builder.base_url(url);
}
if let Some(t) = self.timeout {
builder = builder.timeout(t);
}
if let Some(r) = self.max_retries {
builder = builder.max_retries(r);
}
let inner = builder.build()?;
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.map_err(|e| VynFiError::Config(e.to_string()))?;
Ok(Client { inner, rt })
}
}
pub struct Jobs<'a> {
client: &'a Client,
}
impl Jobs<'_> {
pub fn generate(&self, req: &GenerateRequest) -> Result<SubmitJobResponse, VynFiError> {
self.client.block_on(self.client.inner.jobs().generate(req))
}
pub fn generate_quick(&self, req: &GenerateRequest) -> Result<Job, VynFiError> {
self.client
.block_on(self.client.inner.jobs().generate_quick(req))
}
pub fn list(&self, params: &crate::ListJobsParams) -> Result<JobList, VynFiError> {
self.client.block_on(self.client.inner.jobs().list(params))
}
pub fn get(&self, job_id: &str) -> Result<Job, VynFiError> {
self.client.block_on(self.client.inner.jobs().get(job_id))
}
pub fn download(&self, job_id: &str) -> Result<DownloadResponse, VynFiError> {
self.client
.block_on(self.client.inner.jobs().download(job_id))
}
}
pub struct Catalog<'a> {
client: &'a Client,
}
impl Catalog<'_> {
pub fn list_sectors(&self) -> Result<Vec<SectorSummary>, VynFiError> {
self.client
.block_on(self.client.inner.catalog().list_sectors())
}
pub fn get_sector(&self, slug: &str) -> Result<Sector, VynFiError> {
self.client
.block_on(self.client.inner.catalog().get_sector(slug))
}
}
pub struct Usage<'a> {
client: &'a Client,
}
impl Usage<'_> {
pub fn summary(&self) -> Result<UsageSummary, VynFiError> {
self.client.block_on(self.client.inner.usage().summary())
}
pub fn daily(&self, days: Option<u32>) -> Result<DailyUsageResponse, VynFiError> {
self.client.block_on(self.client.inner.usage().daily(days))
}
}
pub struct ApiKeys<'a> {
client: &'a Client,
}
impl ApiKeys<'_> {
pub fn create(&self, req: &CreateApiKeyRequest) -> Result<ApiKeyCreated, VynFiError> {
self.client
.block_on(self.client.inner.api_keys().create(req))
}
pub fn list(&self) -> Result<Vec<ApiKey>, VynFiError> {
self.client.block_on(self.client.inner.api_keys().list())
}
pub fn get(&self, key_id: &str) -> Result<ApiKey, VynFiError> {
self.client
.block_on(self.client.inner.api_keys().get(key_id))
}
pub fn update(&self, key_id: &str, req: &UpdateApiKeyRequest) -> Result<ApiKey, VynFiError> {
self.client
.block_on(self.client.inner.api_keys().update(key_id, req))
}
pub fn revoke(&self, key_id: &str) -> Result<(), VynFiError> {
self.client
.block_on(self.client.inner.api_keys().revoke(key_id))
}
}
pub struct Credits<'a> {
client: &'a Client,
}
impl Credits<'_> {
pub fn purchase(
&self,
req: &PurchaseCreditsRequest,
) -> Result<PurchaseCreditsResponse, VynFiError> {
self.client
.block_on(self.client.inner.credits().purchase(req))
}
pub fn balance(&self) -> Result<CreditBalance, VynFiError> {
self.client.block_on(self.client.inner.credits().balance())
}
pub fn history(&self) -> Result<CreditHistory, VynFiError> {
self.client.block_on(self.client.inner.credits().history())
}
}