feat: add site data api

dev
Jef Roosens 2023-05-11 14:36:07 +02:00
parent e8e73e9afe
commit 0aa021259a
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
6 changed files with 353 additions and 5 deletions

200
Cargo.lock generated
View File

@ -6,8 +6,19 @@ version = 3
name = "affluences-api" name = "affluences-api"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono",
"reqwest", "reqwest",
"serde", "serde",
"uuid",
]
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
] ]
[[package]] [[package]]
@ -33,6 +44,7 @@ name = "bot"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"affluences-api", "affluences-api",
"chrono",
"tokio", "tokio",
] ]
@ -60,6 +72,31 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e3c5919066adf22df73762e50cffcde3a758f2a848b113b586d1f86728b673b"
dependencies = [
"iana-time-zone",
"js-sys",
"num-integer",
"num-traits",
"time",
"wasm-bindgen",
"winapi",
]
[[package]]
name = "codespan-reporting"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e"
dependencies = [
"termcolor",
"unicode-width",
]
[[package]] [[package]]
name = "core-foundation" name = "core-foundation"
version = "0.9.3" version = "0.9.3"
@ -76,6 +113,50 @@ version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa"
[[package]]
name = "cxx"
version = "1.0.94"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f61f1b6389c3fe1c316bf8a4dccc90a38208354b330925bce1f74a6c4756eb93"
dependencies = [
"cc",
"cxxbridge-flags",
"cxxbridge-macro",
"link-cplusplus",
]
[[package]]
name = "cxx-build"
version = "1.0.94"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12cee708e8962df2aeb38f594aae5d827c022b6460ac71a7a3e2c3c2aae5a07b"
dependencies = [
"cc",
"codespan-reporting",
"once_cell",
"proc-macro2",
"quote",
"scratch",
"syn",
]
[[package]]
name = "cxxbridge-flags"
version = "1.0.94"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7944172ae7e4068c533afbb984114a56c46e9ccddda550499caa222902c7f7bb"
[[package]]
name = "cxxbridge-macro"
version = "1.0.94"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2345488264226bf682893e25de0769f3360aac9957980ec49361b083ddaa5bc5"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "encoding_rs" name = "encoding_rs"
version = "0.8.32" version = "0.8.32"
@ -295,6 +376,30 @@ dependencies = [
"tokio-native-tls", "tokio-native-tls",
] ]
[[package]]
name = "iana-time-zone"
version = "0.1.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0722cd7114b7de04316e7ea5456a0bbb20e4adb46fd27a3697adb812cff0f37c"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"windows",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca"
dependencies = [
"cxx",
"cxx-build",
]
[[package]] [[package]]
name = "idna" name = "idna"
version = "0.3.0" version = "0.3.0"
@ -368,6 +473,15 @@ version = "0.2.144"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1"
[[package]]
name = "link-cplusplus"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ecd207c9c713c34f95a097a5b029ac2ce6010530c7b49d7fea24d977dede04f5"
dependencies = [
"cc",
]
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.3.7" version = "0.3.7"
@ -407,7 +521,7 @@ checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9"
dependencies = [ dependencies = [
"libc", "libc",
"log", "log",
"wasi", "wasi 0.11.0+wasi-snapshot-preview1",
"windows-sys 0.45.0", "windows-sys 0.45.0",
] ]
@ -429,6 +543,25 @@ dependencies = [
"tempfile", "tempfile",
] ]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "num_cpus" name = "num_cpus"
version = "1.15.0" version = "1.15.0"
@ -644,6 +777,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "scratch"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1792db035ce95be60c3f8853017b3999209281c24e2ba5bc8e59bf97a0c590c1"
[[package]] [[package]]
name = "security-framework" name = "security-framework"
version = "2.8.2" version = "2.8.2"
@ -768,6 +907,26 @@ dependencies = [
"windows-sys 0.45.0", "windows-sys 0.45.0",
] ]
[[package]]
name = "termcolor"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6"
dependencies = [
"winapi-util",
]
[[package]]
name = "time"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a"
dependencies = [
"libc",
"wasi 0.10.0+wasi-snapshot-preview1",
"winapi",
]
[[package]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.6.0" version = "1.6.0"
@ -890,6 +1049,12 @@ dependencies = [
"tinyvec", "tinyvec",
] ]
[[package]]
name = "unicode-width"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
[[package]] [[package]]
name = "url" name = "url"
version = "2.3.1" version = "2.3.1"
@ -901,6 +1066,15 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "uuid"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4dad5567ad0cf5b760e5665964bec1b47dfd077ba8a2544b513f3556d3d239a2"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "vcpkg" name = "vcpkg"
version = "0.2.15" version = "0.2.15"
@ -917,6 +1091,12 @@ dependencies = [
"try-lock", "try-lock",
] ]
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"
@ -1015,12 +1195,30 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "winapi-x86_64-pc-windows-gnu" name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f"
dependencies = [
"windows-targets 0.48.0",
]
[[package]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.42.0" version = "0.42.0"

View File

@ -6,5 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
chrono = "0.4.24"
reqwest = { version = "0.11.17", features = ["json"] } reqwest = { version = "0.11.17", features = ["json"] }
serde = { version = "1.0.163", features = ["derive"] } serde = { version = "1.0.163", features = ["derive"] }
uuid = { version = "1.3.2", features = ["serde"] }

View File

@ -1,6 +1,7 @@
mod models; mod models;
use models::Resource; use chrono::NaiveDate;
use models::{Resource, Data, SiteData};
const USER_AGENT: &str = "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/112.0"; const USER_AGENT: &str = "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/112.0";
@ -16,7 +17,13 @@ impl AffluencesClient {
} }
} }
pub async fn available(&mut self) -> reqwest::Result<Vec<Resource>> { pub async fn available(&mut self, resource_uuid: &str, date: NaiveDate, resource_type: u32) -> reqwest::Result<Vec<Resource>> {
self.client.get("https://reservation.affluences.com/api/resources/026b8caa-d310-464f-b714-4c21a2cf98ea/available?date=2023-05-11&type=1").send().await?.json::<Vec<Resource>>().await let url = format!("https://reservation.affluences.com/api/resources/{}/available", resource_uuid);
self.client.get(url).query(&[("date", date.format("%Y-%m-%d").to_string()), ("type", resource_type.to_string())]).send().await?.json::<Vec<Resource>>().await
}
pub async fn site_data(&mut self, name: &str) -> reqwest::Result<SiteData> {
let url = format!("https://api.affluences.com/app/v3/sites/{}", name);
Ok(self.client.get(url).send().await?.json::<Data<SiteData>>().await?.data)
} }
} }

View File

@ -38,3 +38,134 @@ pub struct Resource {
pub slots_state: u32, pub slots_state: u32,
pub hours: Vec<Hour>, pub hours: Vec<Hour>,
} }
#[derive(Deserialize, Debug)]
pub struct Data<T> {
pub data: T
}
#[derive(Deserialize, Debug)]
pub struct SiteDataCategory {
pub id: u32,
pub name: String,
pub name_plural: String,
}
#[derive(Deserialize, Debug)]
pub struct SiteDataLocationCoordinates {
pub latitude: f64,
pub longitude: f64,
}
#[derive(Deserialize, Debug)]
pub struct SiteDataLocationAddress {
pub route: String,
pub city: String,
pub zip_code: String,
pub region: String,
pub country_code: String
}
#[derive(Deserialize, Debug)]
pub struct SiteDataLocation {
pub coordinates: SiteDataLocationCoordinates,
pub address: SiteDataLocationAddress
}
#[derive(Deserialize, Debug)]
pub struct SiteDataForecast {
pub opened: bool,
pub occupancy: u64,
// waiting_time
pub waiting_time_overflow: bool
}
#[derive(Deserialize, Debug)]
pub struct SiteDataNotice {
message: String,
url: Option<String>
}
#[derive(Deserialize, Debug)]
pub struct SiteDataChild {
pub id: uuid::Uuid,
pub slug: String,
pub parent: Option<String>,
pub primary_name: String,
pub secondary_name: String,
pub concat_name: String,
pub categories: Vec<SiteDataCategory>,
pub time_zone: String,
pub location: SiteDataLocation,
pub phone_number: String,
pub email: String,
pub url: String,
pub notices: Vec<SiteDataNotice>,
// messages
pub estimated_distance: f64,
pub current_forecast: SiteDataForecast,
pub today_forecasts: Vec<SiteDataForecast>,
// events
// children
// actions
// services
// infos
pub poster_image: String,
// images
}
#[derive(Deserialize, Debug)]
pub struct SiteDataService {
pub id: u64,
pub name: String,
}
#[derive(Deserialize, Debug)]
pub struct SiteDataInfo {
pub title: String,
pub description: String,
pub url: Option<String>,
}
#[derive(Deserialize, Debug)]
pub struct SiteDataStatus {
pub state: String,
pub text: String,
pub color: String,
}
#[derive(Deserialize, Debug)]
pub struct SiteData {
pub id: uuid::Uuid,
pub slug: String,
parent: Option<String>,
pub primary_name: String,
pub secondary_name: String,
pub concat_name: String,
pub categories: Vec<SiteDataCategory>,
pub time_zone: String,
pub location: SiteDataLocation,
pub phone_number: Option<String>,
pub email: Option<String>,
pub url: Option<String>,
notices: Vec<SiteDataNotice>,
// messages
pub estimated_distance: f64,
pub current_forecast: SiteDataForecast,
pub today_forecasts: Vec<SiteDataForecast>,
// events
pub children: Vec<SiteData>,
// actions
pub services: Vec<SiteDataService>,
pub infos: Vec<SiteDataInfo>,
pub poster_image: String,
pub image: Option<Vec<String>>,
// status
pub closed: bool,
pub booking_available: bool,
pub extended_forecasts: bool,
pub booking_url: Option<String>,
pub validated: bool,
pub validationStatus: String,
pub publicationStatus: String
}

View File

@ -8,3 +8,4 @@ edition = "2021"
[dependencies] [dependencies]
affluences-api = { path = "../affluences-api" } affluences-api = { path = "../affluences-api" }
tokio = { version = "1.28.1", features = ["full"] } tokio = { version = "1.28.1", features = ["full"] }
chrono = "*"

View File

@ -1,8 +1,17 @@
use affluences_api::AffluencesClient; use affluences_api::AffluencesClient;
use chrono::NaiveDate;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let site = "ghent-university";
// let uuid = "026b8caa-d310-464f-b714-4c21a2cf98ea";
let mut client = AffluencesClient::new(); let mut client = AffluencesClient::new();
let res = client.available().await.unwrap(); let res = client.site_data(site).await.unwrap();
// let res = client
// .available(uuid, NaiveDate::from_ymd_opt(2023, 5, 11).unwrap(), 1)
// .await
// .unwrap();
println!("{:?}", res); println!("{:?}", res);
} }