Crates.io | restep |
lib.rs | restep |
version | 0.3.0 |
source | src |
created_at | 2022-08-15 03:40:49.052039 |
updated_at | 2022-08-16 07:42:14.55976 |
description | Simple and useful REST API endpoint generator |
homepage | |
repository | https://github.com/k-fujino-kohei/restep |
max_upload_size | |
id | 645672 |
size | 12,966 |
Restep can create highly readable APIClient.
Automatically generates the endpoint()
function that returns the specified endpoint.
use restep::endpoint;
#[endpoint("/customers")]
fn simple() -> String {
// You can use `fn endpoint() -> String` in this function.
endpoint()
}
assert_eq!(simple(), "/customers");
use restep::endpoint;
struct PathParameters {
customer_id: i32,
}
#[endpoint("/customers/{customer_id}", params = "PathParameters")]
fn dynamic_route() -> String {
let params = PathParameters { customer_id: 1 };
// You can use `fn endpoint(params: &PathParameters) -> String` in this function.
endpoint(¶ms)
}
assert_eq!(dynamic_route(), "/customers/1");
use restep::endpoint;
struct APIClient;
// Also You can change the function name.
#[endpoint("/customers", name = "__endpoint")]
impl APIClient {
pub fn path() -> String {
Self::__endpoint()
}
}
assert_eq!(APIClient::path(), "/customers");
use restep::endpoint;
#[derive(serde::Deserialize)]
struct Customer {
id: i32,
name: String,
}
struct APIClient {
client: reqwest::Client,
}
struct PathParameters {
customer_id: i32,
}
impl APIClient {
#[endpoint("/customer/{customer_id}", params = "PathParameters")]
async fn get_customer(&self, params: PathParameters) -> anyhow::Result<Customer> {
let url = format!("{}{}", std::env::var("BASE_URL").unwrap(), endpoint(¶ms));
let customer = self.client
.get(url)
.send()
.await?
.json()
.await?;
Ok(customer)
}
}