affluence/affluences-api/src/lib.rs

84 lines
2.1 KiB
Rust
Raw Normal View History

2023-05-11 09:30:25 +02:00
mod models;
2023-05-11 16:55:01 +02:00
use chrono::NaiveDate;
2023-05-13 10:04:36 +02:00
pub use models::*;
2023-05-11 09:30:25 +02:00
2023-05-13 10:04:36 +02:00
const USER_AGENT: &str =
"User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/112.0";
2023-05-11 09:30:25 +02:00
pub struct AffluencesClient {
client: reqwest::Client,
2023-05-11 08:56:26 +02:00
}
2023-05-11 09:30:25 +02:00
impl AffluencesClient {
pub fn new() -> Self {
Self {
client: reqwest::Client::builder()
2023-05-13 10:04:36 +02:00
.user_agent(USER_AGENT)
.build()
.unwrap(),
2023-05-11 09:30:25 +02:00
}
}
2023-05-11 08:56:26 +02:00
2023-05-13 13:08:03 +02:00
pub async fn search(&self, query: String) -> reqwest::Result<SiteSearchResponse> {
let url = "https://api.affluences.com/app/v3/sites";
let body = SiteSearch{
search_query: query
};
Ok(self.client.post(url).json(&body).send().await?.json::<Data<SiteSearchResponse>>().await?.data)
}
2023-05-13 10:04:36 +02:00
pub async fn available(
&self,
site_id: uuid::Uuid,
date: NaiveDate,
resource_type: u32,
) -> reqwest::Result<Vec<Resource>> {
let url = format!(
"https://reservation.affluences.com/api/resources/{}/available",
site_id
);
self.client
.get(url)
.query(&[
("date", date.format("%Y-%m-%d").to_string()),
("type", resource_type.to_string()),
])
.send()
.await?
.json::<Vec<Resource>>()
.await
2023-05-11 14:36:07 +02:00
}
2023-05-12 15:51:46 +02:00
pub async fn site_data(&self, slug: &str) -> reqwest::Result<SiteData> {
2023-05-11 15:49:05 +02:00
let url = format!("https://api.affluences.com/app/v3/sites/{}", slug);
2023-05-13 10:04:36 +02:00
Ok(self
.client
.get(url)
.send()
.await?
.json::<Data<SiteData>>()
.await?
.data)
2023-05-11 08:56:26 +02:00
}
2023-05-11 15:49:05 +02:00
2023-05-13 10:04:36 +02:00
pub async fn make_reservation(
&self,
resource_id: u32,
reservation: &Reservation,
) -> reqwest::Result<ReservationResponse> {
let url = format!(
"https://reservation.affluences.com/api/reserve/{}",
resource_id
);
self.client
.post(url)
.json(reservation)
.send()
.await?
.json::<ReservationResponse>()
.await
2023-05-11 15:49:05 +02:00
}
2023-05-11 08:56:26 +02:00
}