From 14574e3085a6f4d6103b2826a56ee3b7b09e740b Mon Sep 17 00:00:00 2001 From: Guus van Meerveld Date: Fri, 19 Apr 2024 17:33:37 +0200 Subject: [PATCH] lib: implemented reply_to_publication function --- Cargo.lock | 400 +-------------------------------------- sshn-lib/Cargo.toml | 1 - sshn-lib/queries.graphql | 17 ++ sshn-lib/src/client.rs | 51 ++++- sshn-lib/src/error.rs | 20 +- sshn-lib/src/lib.rs | 9 + sshn-lib/src/queries.rs | 10 +- sshn-lib/src/tokens.rs | 7 +- 8 files changed, 97 insertions(+), 418 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d2206c4..6332555 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,19 +17,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" -[[package]] -name = "ahash" -version = "0.8.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" -dependencies = [ - "cfg-if", - "getrandom", - "once_cell", - "version_check", - "zerocopy", -] - [[package]] name = "android-tzdata" version = "0.1.1" @@ -84,12 +71,6 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" -[[package]] -name = "bitflags" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" - [[package]] name = "bumpalo" version = "3.16.0" @@ -163,61 +144,6 @@ version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" -[[package]] -name = "cssparser" -version = "0.31.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b3df4f93e5fbbe73ec01ec8d3f68bba73107993a5b1e7519273c32db9b0d5be" -dependencies = [ - "cssparser-macros", - "dtoa-short", - "itoa", - "phf 0.11.2", - "smallvec", -] - -[[package]] -name = "cssparser-macros" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13b588ba4ac1a99f7f2964d24b3d896ddc6bf847ee3855dbd4366f058cfcd331" -dependencies = [ - "quote", - "syn 2.0.60", -] - -[[package]] -name = "derive_more" -version = "0.99.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - -[[package]] -name = "dtoa" -version = "1.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcbb2bf8e87535c23f7a8a321e364ce21462d0ff10cb6407820e8e96dfff6653" - -[[package]] -name = "dtoa-short" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbaceec3c6e4211c79e7b1800fb9680527106beb2f9c51904a3210c03a448c74" -dependencies = [ - "dtoa", -] - -[[package]] -name = "ego-tree" -version = "0.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a68a4904193147e0a8dec3314640e6db742afd5f6e634f428a6af230d9b3591" - [[package]] name = "either" version = "1.11.0" @@ -254,16 +180,6 @@ dependencies = [ "percent-encoding", ] -[[package]] -name = "futf" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df420e2e84819663797d1ec6544b13c5be84629e7bb00dc960d6917db2987843" -dependencies = [ - "mac", - "new_debug_unreachable", -] - [[package]] name = "futures-channel" version = "0.3.30" @@ -303,24 +219,6 @@ dependencies = [ "pin-utils", ] -[[package]] -name = "fxhash" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" -dependencies = [ - "byteorder", -] - -[[package]] -name = "getopts" -version = "0.2.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" -dependencies = [ - "unicode-width", -] - [[package]] name = "getrandom" version = "0.2.14" @@ -433,20 +331,6 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" -[[package]] -name = "html5ever" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bea68cab48b8459f17cf1c944c67ddc572d272d9f2b274140f223ecb1da4a3b7" -dependencies = [ - "log", - "mac", - "markup5ever", - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "http" version = "1.1.0" @@ -636,26 +520,6 @@ version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" -[[package]] -name = "mac" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4" - -[[package]] -name = "markup5ever" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a2629bb1404f3d34c2e921f21fd34ba00b206124c81f65c50b43b6aaefeb016" -dependencies = [ - "log", - "phf 0.10.1", - "phf_codegen", - "string_cache", - "string_cache_codegen", - "tendril", -] - [[package]] name = "memchr" version = "2.7.2" @@ -688,12 +552,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "new_debug_unreachable" -version = "1.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" - [[package]] name = "num-traits" version = "0.2.18" @@ -757,86 +615,6 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" -[[package]] -name = "phf" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fabbf1ead8a5bcbc20f5f8b939ee3f5b0f6f281b6ad3468b84656b658b455259" -dependencies = [ - "phf_shared 0.10.0", -] - -[[package]] -name = "phf" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" -dependencies = [ - "phf_macros", - "phf_shared 0.11.2", -] - -[[package]] -name = "phf_codegen" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb1c3a8bc4dd4e5cfce29b44ffc14bedd2ee294559a294e2a4d4c9e9a6a13cd" -dependencies = [ - "phf_generator 0.10.0", - "phf_shared 0.10.0", -] - -[[package]] -name = "phf_generator" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d5285893bb5eb82e6aaf5d59ee909a06a16737a8970984dd7746ba9283498d6" -dependencies = [ - "phf_shared 0.10.0", - "rand", -] - -[[package]] -name = "phf_generator" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0" -dependencies = [ - "phf_shared 0.11.2", - "rand", -] - -[[package]] -name = "phf_macros" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3444646e286606587e49f3bcf1679b8cef1dc2c5ecc29ddacaffc305180d464b" -dependencies = [ - "phf_generator 0.11.2", - "phf_shared 0.11.2", - "proc-macro2", - "quote", - "syn 2.0.60", -] - -[[package]] -name = "phf_shared" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6796ad771acdc0123d2a88dc428b5e38ef24456743ddb1744ed628f9815c096" -dependencies = [ - "siphasher", -] - -[[package]] -name = "phf_shared" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" -dependencies = [ - "siphasher", -] - [[package]] name = "pin-project" version = "1.1.5" @@ -869,18 +647,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" -[[package]] -name = "ppv-lite86" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" - -[[package]] -name = "precomputed-hash" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" - [[package]] name = "proc-macro2" version = "1.0.81" @@ -899,43 +665,13 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rand" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" -dependencies = [ - "libc", - "rand_chacha", - "rand_core", -] - -[[package]] -name = "rand_chacha" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" -dependencies = [ - "ppv-lite86", - "rand_core", -] - -[[package]] -name = "rand_core" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" -dependencies = [ - "getrandom", -] - [[package]] name = "redox_syscall" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" dependencies = [ - "bitflags 1.3.2", + "bitflags", ] [[package]] @@ -1056,41 +792,6 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" -[[package]] -name = "scraper" -version = "0.19.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b80b33679ff7a0ea53d37f3b39de77ea0c75b12c5805ac43ec0c33b3051af1b" -dependencies = [ - "ahash", - "cssparser", - "ego-tree", - "getopts", - "html5ever", - "once_cell", - "selectors", - "tendril", -] - -[[package]] -name = "selectors" -version = "0.25.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4eb30575f3638fc8f6815f448d50cb1a2e255b0897985c8c59f4d37b72a07b06" -dependencies = [ - "bitflags 2.5.0", - "cssparser", - "derive_more", - "fxhash", - "log", - "new_debug_unreachable", - "phf 0.10.1", - "phf_codegen", - "precomputed-hash", - "servo_arc", - "smallvec", -] - [[package]] name = "serde" version = "1.0.198" @@ -1134,15 +835,6 @@ dependencies = [ "serde", ] -[[package]] -name = "servo_arc" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d036d71a959e00c77a63538b90a6c2390969f9772b096ea837205c6bd0491a44" -dependencies = [ - "stable_deref_trait", -] - [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -1152,12 +844,6 @@ dependencies = [ "libc", ] -[[package]] -name = "siphasher" -version = "0.3.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" - [[package]] name = "slab" version = "0.4.9" @@ -1196,45 +882,12 @@ dependencies = [ "chrono", "graphql_client", "reqwest", - "scraper", "serde", "serde_urlencoded", "thiserror", "tokio", ] -[[package]] -name = "stable_deref_trait" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" - -[[package]] -name = "string_cache" -version = "0.8.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f91138e76242f575eb1d3b38b4f1362f10d3a43f47d182a5b359af488a02293b" -dependencies = [ - "new_debug_unreachable", - "once_cell", - "parking_lot", - "phf_shared 0.10.0", - "precomputed-hash", - "serde", -] - -[[package]] -name = "string_cache_codegen" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bb30289b722be4ff74a408c3cc27edeaad656e06cb1fe8fa9231fa59c728988" -dependencies = [ - "phf_generator 0.10.0", - "phf_shared 0.10.0", - "proc-macro2", - "quote", -] - [[package]] name = "subtle" version = "2.5.0" @@ -1275,7 +928,7 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7" dependencies = [ - "bitflags 1.3.2", + "bitflags", "core-foundation", "system-configuration-sys", ] @@ -1290,17 +943,6 @@ dependencies = [ "libc", ] -[[package]] -name = "tendril" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d24a120c5fc464a3458240ee02c299ebcb9d67b5249c8848b09d639dca8d7bb0" -dependencies = [ - "futf", - "mac", - "utf-8", -] - [[package]] name = "thiserror" version = "1.0.58" @@ -1466,12 +1108,6 @@ dependencies = [ "tinyvec", ] -[[package]] -name = "unicode-width" -version = "0.1.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" - [[package]] name = "unreachable" version = "1.0.0" @@ -1498,18 +1134,6 @@ dependencies = [ "percent-encoding", ] -[[package]] -name = "utf-8" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" - -[[package]] -name = "version_check" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" - [[package]] name = "void" version = "1.0.2" @@ -1774,26 +1398,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "zerocopy" -version = "0.7.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be" -dependencies = [ - "zerocopy-derive", -] - -[[package]] -name = "zerocopy-derive" -version = "0.7.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.60", -] - [[package]] name = "zeroize" version = "1.7.0" diff --git a/sshn-lib/Cargo.toml b/sshn-lib/Cargo.toml index 92f3f6f..d1bc1d1 100644 --- a/sshn-lib/Cargo.toml +++ b/sshn-lib/Cargo.toml @@ -15,7 +15,6 @@ reqwest = { version = "0.12.3", default-features = false, features = [ "http2", "macos-system-configuration", ] } -scraper = "0.19.0" serde = "1.0.198" serde_urlencoded = "0.7.1" thiserror = "1.0.58" diff --git a/sshn-lib/queries.graphql b/sshn-lib/queries.graphql index 58a11a9..bf76556 100644 --- a/sshn-lib/queries.graphql +++ b/sshn-lib/queries.graphql @@ -59,3 +59,20 @@ fragment PublicationListItem on HousingPublication { allocationChance } } + +mutation PostApplication($publicationId: ID!, $locale: String) { + housingApplyToUnit( + input: { publicationId: $publicationId } + locale: $locale + ) { + state + description + userErrors { + field + message { + locale + text + } + } + } +} diff --git a/sshn-lib/src/client.rs b/sshn-lib/src/client.rs index ba2afd5..a22f16f 100644 --- a/sshn-lib/src/client.rs +++ b/sshn-lib/src/client.rs @@ -5,8 +5,12 @@ use graphql_client::GraphQLQuery; use crate::{ constants::{AUTH_URL, GRAPHQL_URL, LOCALE}, - error::Result, - queries::{get_publications_list, GetPublicationsList, GraphqlResponse}, + error::{Error, Result}, + queries::{ + get_publications_list, + post_application::{self, HousingApplyState}, + GetPublicationsList, GraphqlResponse, PostApplication, + }, tokens::{RefreshTokenResponse, Token}, }; @@ -93,7 +97,46 @@ impl Client { Ok(body.data) } - pub async fn reply_to_publication>(&self, publication_id: I) -> Result<()> { - Ok(()) + /// Reply to a publication, given that publications id. + pub async fn reply_to_publication>(&self, publication_id: I) -> Result<()> { + let variables = post_application::Variables { + publication_id: publication_id.into(), + locale: Some(String::from(LOCALE)), + }; + + let request_body = PostApplication::build_query(variables); + + let response = self + .http_client + .post(&self.base_url) + .header( + reqwest::header::AUTHORIZATION, + format!("Bearer {}", self.access_token.as_ref()), + ) + .json(&request_body) + .send() + .await?; + + match response.error_for_status() { + Ok(response) => { + let body = response + .json::>() + .await?; + + if let Some(unit) = body.data.housing_apply_to_unit { + match unit.state { + HousingApplyState::OK => {} + _ => { + let error = Error::Api(unit.description.unwrap_or(String::new())); + + return Err(error); + } + }; + }; + + Ok(()) + } + Err(err) => Err(Error::HttpRequest(err)), + } } } diff --git a/sshn-lib/src/error.rs b/sshn-lib/src/error.rs index 2a6ea9a..f143de8 100644 --- a/sshn-lib/src/error.rs +++ b/sshn-lib/src/error.rs @@ -1,17 +1,13 @@ -use std::{fmt::Display, result}; +use std::result; -use thiserror::Error; - -#[derive(Error, Debug)] -pub enum ClientError { +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error("Error from SSHN API: {0}")] + Api(String), + #[error("Error encoding form data: {0}")] EncodeFormData(#[from] serde_urlencoded::ser::Error), + #[error("Error sending HTTP request: {0}")] HttpRequest(#[from] reqwest::Error), } -impl Display for ClientError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "") - } -} - -pub type Result = result::Result; +pub type Result = result::Result; diff --git a/sshn-lib/src/lib.rs b/sshn-lib/src/lib.rs index 276c6a8..ef120a1 100644 --- a/sshn-lib/src/lib.rs +++ b/sshn-lib/src/lib.rs @@ -8,6 +8,8 @@ pub use crate::client::Client; #[cfg(test)] mod tests { + use chrono::Utc; + use crate::{constants::GRAPHQL_URL, tokens::Token}; use super::*; @@ -29,4 +31,11 @@ mod tests { println!("{:?}", data); } + + #[tokio::test] + async fn test_post_application() { + let client = Client::new(GRAPHQL_URL, Token::new("", Utc::now())); + + client.reply_to_publication("").await.unwrap(); + } } diff --git a/sshn-lib/src/queries.rs b/sshn-lib/src/queries.rs index 9805526..f8c5399 100644 --- a/sshn-lib/src/queries.rs +++ b/sshn-lib/src/queries.rs @@ -13,7 +13,15 @@ type DateTimeOffset = String; )] pub struct GetPublicationsList; -#[derive(Deserialize)] +#[derive(GraphQLQuery)] +#[graphql( + schema_path = "schema.graphql", + query_path = "queries.graphql", + response_derives = "Debug" +)] +pub struct PostApplication; + +#[derive(Deserialize, Debug)] pub struct GraphqlResponse { pub data: T, } diff --git a/sshn-lib/src/tokens.rs b/sshn-lib/src/tokens.rs index 77d224a..52182c8 100644 --- a/sshn-lib/src/tokens.rs +++ b/sshn-lib/src/tokens.rs @@ -34,8 +34,11 @@ impl AsRef for Token { } impl Token { - pub fn new(content: String, expires: DateTime) -> Self { - Self { content, expires } + pub fn new>(content: C, expires: DateTime) -> Self { + Self { + content: content.into(), + expires, + } } pub fn content(&self) -> &str {