#api-client #rest #macro #express #declaratively #user #str

api-client-macro

Declaratively express a REST API client

4 releases

0.1.4 Nov 5, 2024
0.1.2 May 16, 2023
0.1.1 May 15, 2023
0.1.0 May 15, 2023

#4 in #express

Download history 1/week @ 2024-09-22 140/week @ 2024-11-03 8/week @ 2024-11-10 8/week @ 2024-11-17

156 downloads per month

MIT/Apache

13KB
174 lines

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();
}

Dependencies

~4–15MB
~191K SLoC