mod charge;
mod district;
pub mod error;
pub mod export;
mod notice;
pub mod request;
pub mod util;
use std::{
path::{Path, PathBuf},
str::FromStr,
};
use anyhow::anyhow;
use charge::{get_charge_from_wegli_api, get_charges_from_wegli_api};
use district::{get_district_from_wegli_api, get_districts_from_wegli_api};
use error::ApiError;
use export::{download_latest_export_from_wegli, get_exports_from_wegli_api};
use notice::{get_notice_from_wegli_api, get_notices_from_wegli_api};
use url::Url;
use crate::types::{
charge::Charge, district::District, export::Export, notice::Notice, request::RetrySettings,
};
pub struct WegLiApiClient {
api_url: Url,
api_token: String,
pub retry_settings: Option<RetrySettings>,
}
impl WegLiApiClient {
pub fn new(
api_url: &String,
api_token: &String,
retry_settings: Option<RetrySettings>,
) -> Result<Self, anyhow::Error> {
Ok(WegLiApiClient {
api_url: match Url::from_str(&api_url) {
Err(error) => return Err(anyhow!(error)),
Ok(url) => url,
},
api_token: api_token.to_string(),
retry_settings,
})
}
pub async fn get_notice(&self, notice_token: &String) -> Result<Notice, ApiError> {
return get_notice_from_wegli_api(
&self.api_url,
&self.api_token,
notice_token,
&self.retry_settings,
)
.await;
}
pub async fn get_notices(&self) -> Result<Vec<Notice>, ApiError> {
return get_notices_from_wegli_api(&self.api_url, &self.api_token, &self.retry_settings)
.await;
}
pub async fn get_charge(&self, tbnr: &String) -> Result<Charge, ApiError> {
return get_charge_from_wegli_api(
&self.api_url,
&self.api_token,
tbnr,
&self.retry_settings,
)
.await;
}
pub async fn get_charges(&self) -> Result<Vec<Charge>, ApiError> {
return get_charges_from_wegli_api(&self.api_url, &self.api_token, &self.retry_settings)
.await;
}
pub async fn get_district(&self, zip: &String) -> Result<District, ApiError> {
return get_district_from_wegli_api(
&self.api_url,
&self.api_token,
zip,
&self.retry_settings,
)
.await;
}
pub async fn get_districts(&self) -> Result<Vec<District>, ApiError> {
return get_districts_from_wegli_api(&self.api_url, &self.api_token, &self.retry_settings)
.await;
}
pub async fn get_user_exports(&self) -> Result<Vec<Export>, ApiError> {
return get_exports_from_wegli_api(
&self.api_url,
&self.api_token,
false,
&self.retry_settings,
)
.await;
}
pub async fn get_public_exports(&self) -> Result<Vec<Export>, ApiError> {
return get_exports_from_wegli_api(
&self.api_url,
&self.api_token,
true,
&self.retry_settings,
)
.await;
}
pub async fn download_latest_export(
&self,
path: &Path,
public: bool,
unzip: bool,
) -> Result<PathBuf, anyhow::Error> {
return download_latest_export_from_wegli(
&self.api_url,
&self.api_token,
path,
public,
unzip,
&self.retry_settings,
)
.await;
}
}