#rest

cdumay_rest_client

A library to call remote REST API application

1 unstable release

0.3.0 Jul 13, 2024

#389 in HTTP client

BSD-3-Clause

17KB
233 lines

cdumay_rest_client License: BSD-3-Clause cdumay_rest_client on crates.io cdumay_rest_client on docs.rs Source Code Repository

cdumay_rest_client is a basic REST library used to standardize result and serialize them using serde.

Quickstart

Cargo.toml:

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
cdumay_error = "0.3"
cdumay_http_client = "0.3"
cdumay_rest_client = "0.3"

main.rs:

extern crate cdumay_error;
extern crate cdumay_http_client;
extern crate cdumay_rest_client;
extern crate serde_json;

use cdumay_error::JsonError;
use cdumay_http_client::{ClientBuilder, HttpClient};
use cdumay_http_client::authentication::NoAuth;
use cdumay_rest_client::RestClient;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Clone, Debug)]
struct Todo {
    id: usize,
    task: String,
}

fn main() {
    let cli = RestClient::new("http://127.0.0.1:5000").unwrap();
    let result = cli.get::<Todo>("/todos/1".into(), None, None, None, None);

    match result {
        Ok(todo) => println!("{}", serde_json::to_string_pretty(&todo).unwrap()),
        Err(err) => println!("{}", serde_json::to_string_pretty(&JsonError::from(err)).unwrap()),
    }
}

Output:

{
  "id": 1,
  "task": "Build an API"
}

Errors

Errors can be displayed using cdumay_error:

{
  "code": 404,
  "extra": {
    "message": "Todo 7000 doesn't exist. You have requested this URI [/todos/7000] but did you mean /todos/<int:id> ?"
  },
  "message": "Not Found",
  "msgid": "Err-18430"
}

Dependencies

~5–16MB
~219K SLoC