Initial commit
This commit is contained in:
commit
750fef66fd
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
1124
Cargo.lock
generated
Normal file
1124
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
15
Cargo.toml
Normal file
15
Cargo.toml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
[package]
|
||||||
|
name = "dev-communicators"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
[lib]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
tokio = { version = "1.20.1", features = ["full"] }
|
||||||
|
reqwest = { version = "0.11.11", features = [ "json" ]}
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
|
||||||
|
openssl = { version = "0.10", features = ["vendored"] }
|
||||||
|
dev-dtos = { git = "https://backend:Eo1n1TPsyWV7wwo9uFwgUJGKKheMM6paM2mDkVPA4zqkh5dt6Q6XPkbtojzYQudQsM84vSwKmhHHTPjyn535d6NLBmA3meeGj0Gb8if4sceAwvySdmzedg5mN2P5zzQt@gitea.blancoinfante.com/blancoinfante_backend/dev-dtos-rust.git" }
|
1
src/lib.rs
Normal file
1
src/lib.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod middleware;
|
51
src/middleware/client.rs
Normal file
51
src/middleware/client.rs
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
use dev_dtos::enums::error::Error;
|
||||||
|
use serde::{Serialize, de::DeserializeOwned};
|
||||||
|
|
||||||
|
pub async fn perform_request<B: Serialize, R: DeserializeOwned>(
|
||||||
|
base_url: String,
|
||||||
|
client: &reqwest::Client,
|
||||||
|
method: reqwest::Method,
|
||||||
|
path: String,
|
||||||
|
body: Option<B>,
|
||||||
|
expected_status_code: u16,
|
||||||
|
headers: Vec<(String, String)>,
|
||||||
|
) -> Result<R, Error> {
|
||||||
|
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
match req_complete.send().await {
|
||||||
|
// Error handling here
|
||||||
|
Ok(res) => {
|
||||||
|
// Request sent correctly
|
||||||
|
match res.status().as_u16() == expected_status_code {
|
||||||
|
true => {
|
||||||
|
match res.json::<R>().await {
|
||||||
|
Ok(resp_dto) => Ok(resp_dto), // Return correctly deserialized obj
|
||||||
|
Err(err) => Err(Error::CommunicatorError(err.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false => {
|
||||||
|
//If status code is any other than expected
|
||||||
|
Err(Error::UnexpectedStatusCode(
|
||||||
|
expected_status_code,
|
||||||
|
res.status().as_u16(),
|
||||||
|
res.text().await.unwrap(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
// Request couldn't be sent
|
||||||
|
Err(Error::NetworkError(e.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
2
src/middleware/mod.rs
Normal file
2
src/middleware/mod.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
pub mod client;
|
||||||
|
pub mod user_svc;
|
1
src/middleware/user_svc/mod.rs
Normal file
1
src/middleware/user_svc/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod user_service;
|
0
src/middleware/user_svc/user_service.rs
Normal file
0
src/middleware/user_svc/user_service.rs
Normal file
Loading…
Reference in New Issue
Block a user