Skip to main content

request

Attribute Macro request 

Source
#[request]
Expand description

Concise HTTP client request for toolkit-zero socket-client routes.

Replaces a decorated fn item with a let binding statement that performs the HTTP request inline. The function name becomes the binding name; the return type annotation is used as the response type R in .send::<R>(). The function body is discarded entirely.

§Syntax

#[request(client, METHOD, "/path", async|sync)]
#[request(client, METHOD, "/path", json(<body_expr>), async|sync)]
#[request(client, METHOD, "/path", query(<params_expr>), async|sync)]
#[request(client, METHOD, "/path", encrypted(<body_expr>, <key_expr>), async|sync)]
#[request(client, METHOD, "/path", encrypted_query(<params_expr>, <key_expr>), async|sync)]

§Parameters

ArgumentMeaning
clientThe Client variable in the enclosing scope
METHODHTTP method: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS
"/path"Endpoint path string literal
json(expr)Serialise expr as a JSON body (Content-Type: application/json)
query(expr)Serialise expr as URL query parameters
encrypted(body, key)VEIL-seal body with a SerializationKey
encrypted_query(params, key)VEIL-seal params, send as ?data=<base64url>
asyncFinalise with .send::<R>().await?
syncFinalise with .send_sync::<R>()?

The function must carry an explicit return type — it becomes R in the turbofish. The enclosing function must return a Result<_, E> where E implements the relevant From for ? to propagate: reqwest::Error for plain/json/query, or ClientError for encrypted variants.

§Example

use toolkit_zero::socket::client::{Client, Target, request};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Clone)] struct Item   { id: u32, name: String }
#[derive(Serialize)]                     struct NewItem { name: String }
#[derive(Serialize)]                     struct Filter  { page: u32 }

async fn example() -> Result<(), reqwest::Error> {
    let client = Client::new_async(Target::Localhost(8080));

    // Plain async GET → let items: Vec<Item> = client.get("/items").send::<Vec<Item>>().await?
    #[request(client, GET, "/items", async)]
    async fn items() -> Vec<Item> {}

    // POST with JSON body
    #[request(client, POST, "/items", json(NewItem { name: "widget".into() }), async)]
    async fn created() -> Item {}

    // GET with query params
    #[request(client, GET, "/items", query(Filter { page: 2 }), async)]
    async fn page() -> Vec<Item> {}

    // Sync DELETE
    #[request(client, DELETE, "/items/1", sync)]
    fn deleted() -> Item {}

    Ok(())
}