cdumay_rest_client

Crates.iocdumay_rest_client
lib.rscdumay_rest_client
version0.3.0
sourcesrc
created_at2024-07-13 11:32:52.502782
updated_at2024-07-13 11:32:52.502782
descriptionA library to call remote REST API application
homepagehttps://github.com/cdumay/rust-cdumay_rest_client
repositoryhttps://github.com/cdumay/rust-cdumay_rest_client
max_upload_size
id1302288
size18,319
Cédric Dumay (cdumay)

documentation

https://docs.rs/cdumay_rest_client

README

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"
}

Project Links

Commit count: 0

cargo fmt