1 个不稳定版本
0.3.0 | 2024年7月13日 |
---|
#282 in HTTP客户端
112 每月下载量
17KB
233 代码行
cdumay_rest_client
cdumay_rest_client 是一个基本的REST库,用于使用 serde 标准化结果并序列化它们。
快速入门
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()),
}
}
输出:
{
"id": 1,
"task": "Build an API"
}
错误
错误可以使用 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"
}
项目链接
依赖项
~5–16MB
~229K SLoC