use crate::api::{
new_items_all_with_args, new_items_with_args, ping_with_args, read_items_with_args,
NewInputItemsResponse, PingResponse, ReadOptions,
};
use crate::errors::Result;
use crate::models::{FeedItem, InputItem};
use crate::{api_token, env_or_default_url};
pub fn new_async_client() -> Result<AsyncYupdatesClient> {
let base_url = env_or_default_url()?;
let http_client = reqwest::Client::new();
let token = api_token()?;
Ok(AsyncYupdatesClient {
base_url,
http_client,
token,
})
}
pub fn new_async_client_with_http_client(
http_client: reqwest::Client,
) -> Result<AsyncYupdatesClient> {
let base_url = env_or_default_url()?;
let token = api_token()?;
Ok(AsyncYupdatesClient {
base_url,
http_client,
token,
})
}
pub struct AsyncYupdatesClient {
pub base_url: String,
pub http_client: reqwest::Client,
pub token: String,
}
impl AsyncYupdatesClient {
pub async fn new_items(&self, items: &[InputItem]) -> Result<NewInputItemsResponse> {
new_items_with_args(items, &self.http_client, &self.base_url, &self.token).await
}
pub async fn new_items_all(&self, items: &[InputItem], sleep_ms: u64) -> Result<String> {
new_items_all_with_args(
items,
sleep_ms,
&self.http_client,
&self.base_url,
&self.token,
)
.await
}
pub async fn ping(&self) -> Result<PingResponse> {
ping_with_args(&self.http_client, &self.base_url, &self.token).await
}
pub async fn ping_bool(&self) -> bool {
self.ping().await.is_ok()
}
pub async fn read_items<S>(&self, feed_id: S) -> Result<Vec<FeedItem>>
where
S: AsRef<str>,
{
read_items_with_args(
feed_id.as_ref(),
None,
&self.http_client,
&self.base_url,
&self.token,
)
.await
}
pub async fn read_items_with_options<S>(
&self,
feed_id: S,
options: &ReadOptions,
) -> Result<Vec<FeedItem>>
where
S: AsRef<str>,
{
read_items_with_args(
feed_id.as_ref(),
Some(options),
&self.http_client,
&self.base_url,
&self.token,
)
.await
}
}
pub mod sync {
use crate::api::{NewInputItemsResponse, PingResponse, ReadOptions, YupdatesV0};
use crate::clients::{new_async_client, AsyncYupdatesClient};
use crate::errors::{Error, Result};
use crate::models::{FeedItem, InputItem};
use crate::Kind;
use tokio::runtime::Runtime;
pub struct SyncYupdatesClient {
pub client: AsyncYupdatesClient,
pub rt: Runtime,
}
pub fn new_sync_client() -> Result<SyncYupdatesClient> {
let rt = match Runtime::new() {
Ok(rt) => rt,
Err(e) => {
return Err(Error {
kind: Kind::Config(format!("Could not create Tokio runtime: {}", e)),
})
}
};
Ok(SyncYupdatesClient {
client: new_async_client()?,
rt,
})
}
impl YupdatesV0 for SyncYupdatesClient {
fn new_items(&self, items: &[InputItem]) -> Result<NewInputItemsResponse> {
self.rt.block_on(self.client.new_items(items))
}
fn new_items_all(&self, items: &[InputItem], sleep_ms: u64) -> Result<String> {
self.rt.block_on(self.client.new_items_all(items, sleep_ms))
}
fn ping(&self) -> Result<PingResponse> {
self.rt.block_on(self.client.ping())
}
fn ping_bool(&self) -> bool {
self.rt.block_on(self.client.ping_bool())
}
fn read_items<S>(&self, feed_id: S) -> Result<Vec<FeedItem>>
where
S: AsRef<str>,
{
self.rt.block_on(self.client.read_items(feed_id))
}
fn read_items_with_options<S>(
&self,
feed_id: S,
options: &ReadOptions,
) -> Result<Vec<FeedItem>>
where
S: AsRef<str>,
{
self.rt
.block_on(self.client.read_items_with_options(feed_id, options))
}
}
}