use crate::error::OctopustError;
use crate::models::*;
use crate::api;
use reqwest::{Client as HttpClient, header};
use std::sync::Arc;
use base64::engine::general_purpose;
use base64::Engine as _;
#[derive(Clone)]
pub struct Client {
#[allow(dead_code)]
api_key: String,
http: Arc<HttpClient>,
base_url: String,
}
impl Client {
pub fn new<S: Into<String>>(api_key: S) -> Self {
let api_key = api_key.into();
let mut headers = header::HeaderMap::new();
let auth = general_purpose::STANDARD.encode(format!("{}:", api_key));
headers.insert(
header::AUTHORIZATION,
header::HeaderValue::from_str(&format!("Basic {}", auth)).unwrap(),
);
headers.insert(
header::AUTHORIZATION,
header::HeaderValue::from_str(&format!("Basic {}", auth)).unwrap(),
);
let http = HttpClient::builder()
.default_headers(headers)
.build()
.unwrap();
Client {
api_key: "".into(), http: Arc::new(http),
base_url: "https://api.octopus.energy/v1/".to_string(),
}
}
pub async fn list_products(&self) -> Result<Vec<Product>, OctopustError> {
api::products::list_products(&self.http, &self.base_url).await
}
pub async fn retrieve_product(&self, product_code: &str) -> Result<ProductDetail, OctopustError> {
api::products::retrieve_product(&self.http, &self.base_url, product_code).await
}
pub async fn list_electricity_tariff_day_unit_rates(&self, product_code: &str, tariff_code: &str) -> Result<TariffChargesResponse, OctopustError> {
api::tariffs::list_electricity_tariff_day_unit_rates(&self.http, &self.base_url, product_code, tariff_code).await
}
pub async fn list_electricity_tariff_night_unit_rates(&self, product_code: &str, tariff_code: &str) -> Result<TariffChargesResponse, OctopustError> {
api::tariffs::list_electricity_tariff_night_unit_rates(&self.http, &self.base_url, product_code, tariff_code).await
}
pub async fn list_electricity_tariff_standard_unit_rates(&self, product_code: &str, tariff_code: &str) -> Result<TariffChargesResponse, OctopustError> {
api::tariffs::list_electricity_tariff_standard_unit_rates(&self.http, &self.base_url, product_code, tariff_code).await
}
pub async fn list_electricity_tariff_standing_charges(&self, product_code: &str, tariff_code: &str) -> Result<TariffChargesResponse, OctopustError> {
api::tariffs::list_electricity_tariff_standing_charges(&self.http, &self.base_url, product_code, tariff_code).await
}
pub async fn list_gas_tariff_standard_unit_rates(&self, product_code: &str, tariff_code: &str) -> Result<TariffChargesResponse, OctopustError> {
api::tariffs::list_gas_tariff_standard_unit_rates(&self.http, &self.base_url, product_code, tariff_code).await
}
pub async fn list_gas_tariff_standing_charges(&self, product_code: &str, tariff_code: &str) -> Result<TariffChargesResponse, OctopustError> {
api::tariffs::list_gas_tariff_standing_charges(&self.http, &self.base_url, product_code, tariff_code).await
}
pub async fn list_electricity_consumption(&self, mpan: &str, serial_number: &str) -> Result<ConsumptionResponse, OctopustError> {
api::consumption::list_electricity_consumption(&self.http, &self.base_url,mpan, serial_number).await
}
pub async fn list_gas_consumption(&self, mprn: &str, serial_number: &str) -> Result<ConsumptionResponse, OctopustError> {
api::consumption::list_gas_consumption(&self.http, &self.base_url,mprn, serial_number).await
}
pub async fn list_industry_grid_supply_points(&self) -> Result<GridSupplyPointsResponse, OctopustError> {
api::industry::list_industry_grid_supply_points(&self.http, &self.base_url).await
}
}