diff --git a/Cargo.lock b/Cargo.lock index bdc1700..96667d5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -423,7 +423,6 @@ version = "0.1.0" dependencies = [ "actix-web-utils", "dev-dtos", - "openssl", "reqwest", "serde", "tokio", @@ -528,6 +527,12 @@ version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e5aa3de05362c3fb88de6531e6296e85cde7739cccad4b9dfeeb7f6ebce56bf" +[[package]] +name = "futures-io" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbf4d2a7a308fd4578637c0b17c7e1c7ba127b8f6ba00b29f717e9655d85eb68" + [[package]] name = "futures-sink" version = "0.3.24" @@ -547,9 +552,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "44fb6cb1be61cc1d2e43b262516aafcf63b241cffdb1d3fa115f91d9c7b09c90" dependencies = [ "futures-core", + "futures-io", "futures-task", + "memchr", "pin-project-lite", "pin-utils", + "slab", ] [[package]] @@ -941,15 +949,6 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" -[[package]] -name = "openssl-src" -version = "111.22.0+1.1.1q" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f31f0d509d1c1ae9cada2f9539ff8f37933831fd5098879e482aa687d659853" -dependencies = [ - "cc", -] - [[package]] name = "openssl-sys" version = "0.9.75" @@ -959,7 +958,6 @@ dependencies = [ "autocfg", "cc", "libc", - "openssl-src", "pkg-config", "vcpkg", ] diff --git a/Cargo.toml b/Cargo.toml index ec2a508..2d9556b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" [dependencies] tokio = { version = "1.20.1", features = ["full"] } -reqwest = { version = "0.11.11", features = [ "json" ]} +reqwest = { version = "0.11.11", features = [ "json", "blocking" ]} serde = { version = "1.0", features = ["derive"] } actix-web-utils = "0.2.5" diff --git a/src/middleware/client.rs b/src/middleware/client.rs index ad37a69..730a30b 100644 --- a/src/middleware/client.rs +++ b/src/middleware/client.rs @@ -105,4 +105,56 @@ pub async fn perform_request_without_client( Err(Error::ClientError(MessageResource::new_from_err(e))) } } +} +/// Same as function above but blocking +pub async fn perform_request_without_client_sync( + base_url: String, + method: reqwest::Method, + path: String, + body: Option, + expected_status_code: u16, + headers: Vec<(String, String)>, +) -> Result { + let client = reqwest::blocking::Client::new(); + let mut req_incomplete = + client.request(method, format!("{url}{path}", url = base_url, path = path)); + + for header in headers { + req_incomplete = req_incomplete.header(&header.0, &header.1); + } + + let req_complete = match body { + Some(b) => req_incomplete.json(&b), + None => req_incomplete.header("content-length", 0), + }; + println!("{:?}", req_complete); + match req_complete.send() { + // Error handling here + Ok(res) => { + // Request sent correctly + match res.status().as_u16() == expected_status_code { + true => { + match res.json::() { + Ok(resp_dto) => Ok(resp_dto), // Return correctly deserialized obj + Err(err) => Err(Error::ClientError(MessageResource::new_from_err(err))), + } + } + false => { + //If status code is any other than expected + Err(Error::UnexpectedStatusCode( + expected_status_code, + res.status().as_u16(), + match res.json::>() { + Ok(messages) => messages, + Err(e) => vec![MessageResource::new_from_err(e.to_string())], + }, + )) + } + } + } + Err(e) => { + // Request couldn't be sent + Err(Error::ClientError(MessageResource::new_from_err(e))) + } + } } \ No newline at end of file