api-client-macro

Crates.ioapi-client-macro
lib.rsapi-client-macro
version0.1.4
sourcesrc
created_at2023-05-15 15:15:07.14555
updated_at2024-11-05 03:53:27.014495
descriptionDeclaratively express a REST API client
homepagehttps://github.com/isaacadams/api-client-macro
repositoryhttps://github.com/isaacadams/api-client-macro
max_upload_size
id865124
size12,533
Isaac Adams (isaacadams)

documentation

README

Features

  • 🎉 supports both async and blocking reqwest clients

Usage

A reqwest powered REST API client can be generated using api_client_macro::generate! provided the following syntax.

api_client_macro::generate!(ApiClient, {
    user {
        #[get "user/{}"]
        get_by_id(id: &str),

        #[delete "user/{}"]
        delete_by_id(id: &str),

        #[post "user"]
        create(),

        #[get "users"]
        list()
    },
    contact {
        #[get "contact/{}"]
        get_by_id(id: &str),

        #[delete "contact/{}"]
        delete_by_id(id: &str),

        #[post "contact"]
        create(),

        #[get "contact"]
        list()
    }
});

After compilation, the following code is available.

async fn main_async() {
    let client = asynchronous::Builder::new("base_url", None);
    client.contact_create().body("<body>").send().await.unwrap();
    client.contact_get_by_id("<id>").send().await.unwrap();
    client.user_list().query(&[("email", "<email>")]).send().await.unwrap();
}

fn main_blocking() {
    let client = blocking::Builder::new("base_url", None);
    client.contact_create().body("<body>").send().unwrap();
    client.contact_get_by_id("<id>").send().unwrap();
    client.user_list().query(&[("email", "<email>")]).send().unwrap();
}
Commit count: 15

cargo fmt