From 8909ac57a8c0c02ce0b6bfabf5f47cafc955d464 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 13 May 2023 12:29:35 +0200 Subject: [PATCH 1/3] docs: start of api reference file --- affluences-api/API.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/affluences-api/API.md b/affluences-api/API.md index fc0ecd0..708641e 100644 --- a/affluences-api/API.md +++ b/affluences-api/API.md @@ -1,3 +1,45 @@ +# Affluences API Reference + +## Terminology + +* `site` + * General name for a location/institution + * Defined by a UUID and a slug, e.g. `ghent-university` + * Can contain one more or more sites, e.g. + `ghent-university-library-book-tower` is a child of `ghent-university` + +## Notes + +The API checks for browser user agents, so your requests should include a valid +user agent of a modern browser. + +## API Routes + +### Search for sites + +`GET https://api.affluences.com/app/v3/sites` + +**Body format** + +```json +{ + "selected_categories": [ + 1 + ], + "page": 0, + "search_query": "university of ghent" +} +``` + +**Response format** + +`Data>` + +### Retrieve time table for a given site + +`GET https://reservation.affluences.com/api/sites/4737e57a-ee05-4f7b-901a-7bb541eeb297` + + curl -L 'https://api.affluences.com/app/v3/sites/ghent-university' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/112.0' returnt lijst van alle bibliotheken op ugent, inclusief de uuids die dan nodig zijn om de specifieke requests te sturen naar die bib zijn stuff From 29f13d49b94b81e55f8e95496b31587145e452a6 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 13 May 2023 13:08:03 +0200 Subject: [PATCH 2/3] feat: start affluences cli for testing --- Cargo.lock | 191 ++++++++++++++++++++++++- Cargo.toml | 1 + affluences-api/src/lib.rs | 9 ++ affluences-api/src/models/site_data.rs | 38 +++-- affluences-cli/Cargo.toml | 12 ++ affluences-cli/src/main.rs | 30 ++++ bot/src/commands.rs | 2 + 7 files changed, 269 insertions(+), 14 deletions(-) create mode 100644 affluences-cli/Cargo.toml create mode 100644 affluences-cli/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index afa86eb..5d0a985 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,6 +18,16 @@ dependencies = [ "uuid", ] +[[package]] +name = "affluences-cli" +version = "0.1.0" +dependencies = [ + "affluences-api", + "clap", + "serde_json", + "tokio", +] + [[package]] name = "affy" version = "0.1.0" @@ -38,6 +48,55 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is-terminal", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41ed9a86bf92ae6580e0a31281f65a1b1d867c0cc68d5346e2ae128dddfa6a7d" + +[[package]] +name = "anstyle-parse" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e765fd216e48e067936442276d1d57399e37bce53c264d6fefbe298080cb57ee" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys 0.48.0", +] + +[[package]] +name = "anstyle-wincon" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180abfa45703aebe0093f79badacc01b8fd4ea2e35118747e5811127f926e188" +dependencies = [ + "anstyle", + "windows-sys 0.48.0", +] + [[package]] name = "async-trait" version = "0.1.68" @@ -144,6 +203,48 @@ dependencies = [ "winapi", ] +[[package]] +name = "clap" +version = "4.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34d21f9bf1b425d2968943631ec91202fe5e837264063503708b83013f8fc938" +dependencies = [ + "clap_builder", + "clap_derive", + "once_cell", +] + +[[package]] +name = "clap_builder" +version = "4.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "914c8c79fb560f238ef6429439a30023c862f7a28e688c58f7203f12b29970bd" +dependencies = [ + "anstream", + "anstyle", + "bitflags", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9644cd56d6b87dbe899ef8b053e331c0637664e9e21a33dfcdc36093f5c5c4" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.15", +] + +[[package]] +name = "clap_lex" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1" + [[package]] name = "codespan-reporting" version = "0.11.1" @@ -154,6 +255,12 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "core-foundation-sys" version = "0.8.4" @@ -311,6 +418,27 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "errno" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" +dependencies = [ + "errno-dragonfly", + "libc", + "windows-sys 0.48.0", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "flate2" version = "1.0.26" @@ -459,6 +587,12 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.2.6" @@ -468,6 +602,12 @@ dependencies = [ "libc", ] +[[package]] +name = "hermit-abi" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" + [[package]] name = "http" version = "0.2.9" @@ -589,12 +729,35 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "io-lifetimes" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c66c74d2ae7e79a5a8f7ac924adbe38ee42a859c6539ad869eb51f0b52dc220" +dependencies = [ + "hermit-abi 0.3.1", + "libc", + "windows-sys 0.48.0", +] + [[package]] name = "ipnet" version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "12b6ee2129af8d4fb011108c73d99a1b83a85977f23b82460c0ae2e25bb4b57f" +[[package]] +name = "is-terminal" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f" +dependencies = [ + "hermit-abi 0.3.1", + "io-lifetimes", + "rustix", + "windows-sys 0.48.0", +] + [[package]] name = "itoa" version = "1.0.6" @@ -625,6 +788,12 @@ dependencies = [ "cc", ] +[[package]] +name = "linux-raw-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ece97ea872ece730aed82664c424eb4c8291e1ff2480247ccf7409044bc6479f" + [[package]] name = "lock_api" version = "0.4.9" @@ -712,7 +881,7 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b" dependencies = [ - "hermit-abi", + "hermit-abi 0.2.6", "libc", ] @@ -938,6 +1107,20 @@ dependencies = [ "winapi", ] +[[package]] +name = "rustix" +version = "0.37.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acf8729d8542766f1b2cf77eb034d52f40d375bb8b615d0b147089946e16613d" +dependencies = [ + "bitflags", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys", + "windows-sys 0.48.0", +] + [[package]] name = "rustls" version = "0.20.8" @@ -1432,6 +1615,12 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "uuid" version = "1.3.2" diff --git a/Cargo.toml b/Cargo.toml index e07b19a..eea9b1e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,5 +2,6 @@ members = [ "affluences-api", + "affluences-cli", "bot", ] diff --git a/affluences-api/src/lib.rs b/affluences-api/src/lib.rs index 17c7ace..3e11326 100644 --- a/affluences-api/src/lib.rs +++ b/affluences-api/src/lib.rs @@ -20,6 +20,15 @@ impl AffluencesClient { } } + pub async fn search(&self, query: String) -> reqwest::Result { + 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::>().await?.data) + } + pub async fn available( &self, site_id: uuid::Uuid, diff --git a/affluences-api/src/models/site_data.rs b/affluences-api/src/models/site_data.rs index 1d13a22..c2a7d98 100644 --- a/affluences-api/src/models/site_data.rs +++ b/affluences-api/src/models/site_data.rs @@ -1,24 +1,24 @@ -use serde::Deserialize; +use serde::{Serialize, Deserialize}; -#[derive(Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct Data { pub data: T, } -#[derive(Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct SiteDataCategory { pub id: u32, pub name: String, pub name_plural: String, } -#[derive(Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct SiteDataLocationCoordinates { pub latitude: f64, pub longitude: f64, } -#[derive(Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct SiteDataLocationAddress { pub route: String, pub city: String, @@ -27,47 +27,47 @@ pub struct SiteDataLocationAddress { pub country_code: String, } -#[derive(Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct SiteDataLocation { pub coordinates: SiteDataLocationCoordinates, pub address: SiteDataLocationAddress, } -#[derive(Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct SiteDataForecast { pub opened: bool, - pub occupancy: u32, + pub occupancy: Option, // waiting_time pub waiting_time_overflow: bool, } -#[derive(Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct SiteDataNotice { pub message: String, pub url: Option, } -#[derive(Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct SiteDataService { pub id: u32, pub name: String, } -#[derive(Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct SiteDataInfo { pub title: String, pub description: String, pub url: Option, } -#[derive(Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct SiteDataStatus { pub state: String, pub text: String, pub color: String, } -#[derive(Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct SiteData { pub id: uuid::Uuid, pub slug: String, @@ -104,3 +104,15 @@ pub struct SiteData { #[serde(rename = "publicationStatus")] pub publication_status: String, } + +#[derive(Serialize, Debug)] +pub struct SiteSearch { + pub search_query: String +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct SiteSearchResponse { + pub page: u32, + pub max_size: u32, + pub results: Vec +} diff --git a/affluences-cli/Cargo.toml b/affluences-cli/Cargo.toml new file mode 100644 index 0000000..f46a340 --- /dev/null +++ b/affluences-cli/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "affluences-cli" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +affluences-api = { path = "../affluences-api" } +clap = { version = "4.2.7", features = ["derive"] } +serde_json = "1.0.96" +tokio = { version = "1.28.1", features = ["full"] } diff --git a/affluences-cli/src/main.rs b/affluences-cli/src/main.rs new file mode 100644 index 0000000..3b31cb5 --- /dev/null +++ b/affluences-cli/src/main.rs @@ -0,0 +1,30 @@ +use affluences_api::AffluencesClient; +use clap::{Parser, Subcommand}; + +#[derive(Parser)] +#[command(author, version, about, long_about = None)] +struct Cli { + #[command(subcommand)] + command: Option, +} + +#[derive(Subcommand)] +enum Commands { + /// does testing things + SearchSite { query: String }, +} + +#[tokio::main] +async fn main() { + let cli = Cli::parse(); + let client = AffluencesClient::new(); + + match &cli.command { + Some(Commands::SearchSite { query }) => { + let res = client.search(query.to_string()).await.unwrap(); + let s = serde_json::to_string_pretty(&res).unwrap(); + println!("{}", s); + } + None => {} + } +} diff --git a/bot/src/commands.rs b/bot/src/commands.rs index 60d82d5..6a2c548 100644 --- a/bot/src/commands.rs +++ b/bot/src/commands.rs @@ -158,3 +158,5 @@ pub async fn available(ctx: Context<'_>, date: NaiveDate) -> Result<(), Error> { // ctx: Context<'_>, // date: NaiveDate, // ) -> Result<(), Error> { + +// } From fd81b6426266319248ee5f01164296947f1247f1 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 13 May 2023 13:10:24 +0200 Subject: [PATCH 3/3] fix(ci): add clippy and rustfmt --- .woodpecker/clippy.yml | 1 + .woodpecker/lint.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/.woodpecker/clippy.yml b/.woodpecker/clippy.yml index c100904..92851e5 100644 --- a/.woodpecker/clippy.yml +++ b/.woodpecker/clippy.yml @@ -7,4 +7,5 @@ pipeline: clippy: image: 'rust:1.69' commands: + - rustup component add clippy - cargo clippy -- --no-deps -Dwarnings diff --git a/.woodpecker/lint.yml b/.woodpecker/lint.yml index 01065f8..ad2b612 100644 --- a/.woodpecker/lint.yml +++ b/.woodpecker/lint.yml @@ -7,4 +7,5 @@ pipeline: lint: image: 'rust:1.69' commands: + - rustup component add rustfmt - cargo fmt -- --check