use reqwest::{Client as HttpClient, RequestBuilder};
pub mod accounts;
pub mod balance;
pub mod pots;
mod request;
use self::{
accounts::{Accounts, AccountsRequest},
balance::{Balance, BalanceRequest},
pots::{Pots, PotsRequest},
};
use crate::Result;
fn get_endpoint(endpoint: impl AsRef<str> + 'static) -> String {
format!("https://api.monzo.com/{}", endpoint.as_ref())
}
#[derive(Default)]
pub struct ClientBuilder {
access_token: Option<String>,
refresh_token: Option<String>,
}
impl ClientBuilder {
pub fn access_token(mut self, access_token: impl Into<String>) -> Self {
self.access_token = Some(access_token.into());
self
}
pub fn refresh_token(mut self, refresh_token: impl Into<String>) -> Self {
self.refresh_token = Some(refresh_token.into());
self
}
pub fn build(self) -> Client {
let access_token = self.access_token.unwrap();
let refresh_token = self.refresh_token;
let http_client = HttpClient::new();
Client {
http_client,
access_token,
refresh_token,
}
}
}
pub struct Client {
http_client: HttpClient,
access_token: String,
refresh_token: Option<String>,
}
impl Client {
pub fn new(access_token: impl Into<String>) -> Self {
Client::builder().access_token(access_token).build()
}
pub fn builder() -> ClientBuilder {
ClientBuilder::default()
}
fn get(&self, endpoint: impl AsRef<str> + 'static) -> RequestBuilder {
let endpoint = get_endpoint(endpoint);
self.http_client
.get(&endpoint)
.bearer_auth(&self.access_token)
}
pub async fn accounts(&self) -> Result<Accounts> {
AccountsRequest::from(self.get("accounts")).await
}
pub async fn balance(&self, account_id: impl AsRef<str>) -> Result<Balance> {
BalanceRequest::from(
self.get("balance")
.query(&[("account_id", account_id.as_ref())]),
)
.await
}
pub async fn pots(&self) -> Result<Pots> {
PotsRequest::from(self.get("pots")).await
}
}