use super::QuickClient;
use crate::{
endpoints::{accounts, auth, balance, feed_items, pots, transactions},
Result,
};
#[must_use]
pub struct Client {
quick_client: QuickClient,
client_id: String,
client_secret: String,
refresh_token: String,
}
impl Client {
pub fn quick(access_token: impl Into<String>) -> QuickClient {
QuickClient::new(access_token)
}
pub fn new(
access_token: impl Into<String>,
client_id: impl Into<String>,
client_secret: impl Into<String>,
refresh_token: impl Into<String>,
) -> Self {
QuickClient::new(access_token).with_refresh_tokens(client_id, client_secret, refresh_token)
}
pub fn from_http_client(
http_client: reqwest::Client,
access_token: impl Into<String>,
client_id: impl Into<String>,
client_secret: impl Into<String>,
refresh_token: impl Into<String>,
) -> Self {
QuickClient::from_http_client(http_client, access_token).with_refresh_tokens(
client_id,
client_secret,
refresh_token,
)
}
#[must_use]
pub fn client_id(&self) -> &String {
&self.client_id
}
#[must_use]
pub fn client_secret(&self) -> &String {
&self.client_secret
}
#[must_use]
pub fn refresh_token(&self) -> &String {
&self.refresh_token
}
pub(crate) fn from_quick_client(
quick_client: QuickClient,
client_id: impl Into<String>,
client_secret: impl Into<String>,
refresh_token: impl Into<String>,
) -> Self {
Self {
quick_client,
client_id: client_id.into(),
client_secret: client_secret.into(),
refresh_token: refresh_token.into(),
}
}
#[must_use]
fn get_refresh_tokens(&self) -> auth::Refresh {
auth::Refresh::new(
self.http_client(),
self.client_id(),
self.client_secret(),
self.refresh_token(),
)
}
pub async fn refresh_auth(&mut self) -> Result<()> {
let response = self.get_refresh_tokens().send().await?;
self.set_access_token(response.access_token);
self.refresh_token = response.refresh_token;
Ok(())
}
#[must_use]
pub fn access_token(&self) -> &String {
self.quick_client.access_token()
}
pub async fn accounts(&self) -> Result<Vec<accounts::Account>> {
self.quick_client.accounts().await
}
pub async fn balance(&self, account_id: &str) -> Result<balance::Balance> {
self.quick_client.balance(account_id).await
}
pub async fn pots(&self) -> Result<Vec<pots::Pot>> {
self.quick_client.pots().await
}
#[must_use]
pub fn basic_feed_item<'a>(
&self,
account_id: &'a str,
title: &'a str,
image_url: &'a str,
) -> feed_items::Basic<'a> {
self.quick_client
.basic_feed_item(account_id, title, image_url)
}
#[must_use]
pub fn deposit_into_pot(
&self,
pot_id: &str,
source_account_id: &str,
amount: i64,
) -> pots::Deposit {
self.quick_client
.deposit_into_pot(pot_id, source_account_id, amount)
}
#[must_use]
pub fn transactions<'a>(&self, account_id: &'a str) -> transactions::List<'a> {
self.quick_client.transactions(account_id)
}
#[must_use]
pub fn transaction(&self, transaction_id: &str) -> transactions::Get {
transactions::Get::new(self.http_client(), self.access_token(), transaction_id)
}
pub fn set_access_token(&mut self, access_token: impl Into<String>) {
self.quick_client.set_access_token(access_token);
}
pub fn with_access_token(mut self, access_token: impl Into<String>) -> Self {
self.set_access_token(access_token);
self
}
#[must_use]
pub fn http_client(&self) -> &reqwest::Client {
self.quick_client.http_client()
}
pub fn set_http_client(&mut self, http_client: reqwest::Client) {
self.quick_client.set_http_client(http_client);
}
}