use super::refreshable::Client as RefreshableClient;
use crate::{
endpoints::{accounts, balance, feed_items, pots, transactions},
Result,
};
#[must_use]
pub struct Client {
http_client: reqwest::Client,
access_token: String,
}
impl Client {
pub fn new(access_token: impl Into<String>) -> Self {
let http_client = reqwest::Client::new();
Self::from_http_client(http_client, access_token)
}
pub fn from_http_client(http_client: reqwest::Client, access_token: impl Into<String>) -> Self {
let access_token = access_token.into();
Self {
http_client,
access_token,
}
}
pub fn with_refresh_tokens(
self,
client_id: impl Into<String>,
client_secret: impl Into<String>,
refresh_token: impl Into<String>,
) -> RefreshableClient {
RefreshableClient::from_quick_client(self, client_id, client_secret, refresh_token)
}
#[must_use]
pub fn access_token(&self) -> &String {
&self.access_token
}
pub async fn accounts(&self) -> Result<Vec<accounts::Account>> {
accounts::List::new(self.http_client(), self.access_token())
.send()
.await
}
pub async fn balance(&self, account_id: &str) -> Result<balance::Balance> {
balance::Get::new(self.http_client(), self.access_token(), account_id)
.send()
.await
}
pub async fn pots(&self) -> Result<Vec<pots::Pot>> {
pots::List::new(self.http_client(), self.access_token())
.send()
.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> {
feed_items::Basic::new(
self.http_client(),
self.access_token(),
account_id,
title,
image_url,
)
}
#[must_use]
pub fn deposit_into_pot(
&self,
pot_id: &str,
source_account_id: &str,
amount: i64,
) -> pots::Deposit {
pots::Deposit::new(
self.http_client(),
self.access_token(),
pot_id,
source_account_id,
amount,
)
}
#[must_use]
pub fn transactions<'a>(&self, account_id: &'a str) -> transactions::List<'a> {
transactions::List::new(self.http_client(), self.access_token(), 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.access_token = access_token.into();
}
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.http_client
}
pub fn set_http_client(&mut self, http_client: reqwest::Client) {
self.http_client = http_client;
}
}