1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use crate::Context;
use core::fmt::Display;
use minicbor::Encode;
use ockam_core::api::{assert_request_match, RequestBuilder};
use ockam_core::compat::vec::Vec;
use ockam_core::{Result, Route};

/// Encode request header and body (if any), send the package to the server and returns its response.
pub async fn request<T, R>(
    ctx: &mut Context,
    label: &str,
    schema: impl Into<Option<&str>>,
    route: R,
    req: RequestBuilder<'_, T>,
) -> Result<Vec<u8>>
where
    T: Encode<()>,
    R: Into<Route> + Display,
{
    let mut buf = Vec::new();
    req.encode(&mut buf)?;
    assert_request_match(schema, &buf);
    trace! {
        target:  "ockam_api",
        id     = %req.header().id(),
        method = ?req.header().method(),
        path   = %req.header().path(),
        body   = %req.header().has_body(),
        "-> {label}"
    };
    let vec: Vec<u8> = ctx.send_and_receive(route, buf).await?;
    Ok(vec)
}