Skip to main content

rumtk_web/utils/
client.rs

1/*
2 *     rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 *     This toolkit aims to be reliable, simple, performant, and standards compliant.
4 *     Copyright (C) 2026  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
5 *     Copyright (C) 2026  MedicalMasses L.L.C. <contact@medicalmasses.com>
6 *
7 *     This program is free software: you can redistribute it and/or modify
8 *     it under the terms of the GNU General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 *
12 *     This program is distributed in the hope that it will be useful,
13 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *     GNU General Public License for more details.
16 *
17 *     You should have received a copy of the GNU General Public License
18 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 */
20pub use reqwest::{Client, Response, Request, Error as RequestError};
21use rumtk_core::base::RUMResult;
22use rumtk_core::buffers::RUMBuffer;
23use rumtk_core::rumtk_resolve_task;
24use rumtk_core::serde::RUMSerJson;
25use rumtk_core::strings::rumtk_format;
26
27pub type RUMWebClient = Client;
28pub type RUMWebClientResponse = (u16, RUMBuffer);
29
30#[inline]
31async fn consume_response(response: Response) -> RUMResult<RUMWebClientResponse> {
32    let response_code = response.status().as_u16();
33    let url = response.url().to_string();
34    let response = response.bytes().await;
35    match response {
36        Ok(text) => Ok((response_code, RUMBuffer::from(text.as_ref()))),
37        Err(e) => Err(rumtk_format!("No response from {} because {}", url, e)),
38    }
39}
40
41#[inline]
42pub async fn rumtk_web_get(url: String) -> RUMResult<RUMWebClientResponse> {
43    let client = Client::new();
44    let response = match client.get(&url).send().await {
45        Ok(r) => r,
46        Err(e) => return Err(rumtk_format!("Get request failed for {} because {}", &url, e)),
47    };
48    consume_response(response).await
49}
50
51#[inline]
52pub fn rumtk_web_sync_get(url: &str) -> RUMResult<RUMWebClientResponse> {
53    let url = url.to_string();
54    rumtk_resolve_task!(rumtk_web_get(url))
55}
56
57pub async fn rumtk_web_post<T: RUMSerJson + Sync + Send + 'static>(url: String, data: T) -> RUMResult<RUMWebClientResponse> {
58    let client = Client::new();
59    let response = match client.post(&url).json(&data).send().await {
60        Ok(r) => r,
61        Err(e) => return Err(rumtk_format!("Post request failed for {} because {}", &url, e)),
62    };
63    consume_response(response).await
64}
65
66pub fn rumtk_web_sync_post<T: RUMSerJson + Sync + Send + 'static>(url: &str, data: T) -> RUMResult<RUMWebClientResponse> {
67    let url = url.to_string();
68    rumtk_resolve_task!(rumtk_web_post(url, data))
69}