#http #客户端 #interfacer

interfacer-http-hyper

为interfacer-http提供的基于hyper的HTTP服务

1个不稳定版本

0.1.0 2019年9月5日

#430#http-client


用于 zju-jw-scraper

MIT 协议

41KB
928

一个神奇的HTTP客户端,类似于Java中的retrofit

// define interface

#![feature(custom_attribute, async_await, param_attrs)]
#![allow(unused_attributes)]

use interfacer_http::derive::{FromContent, ToContent};
use interfacer_http::{content_types, http::header::COOKIE, http_interface, Response, Result};
use serde_derive::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, FromContent, ToContent, Debug)]
struct User {
    name: String,
    age: i32,
}

#[http_interface]
trait UserInterface: Clone {
    #[put("/api/user/{id}?age={age}")]
    #[expect(200, content_types::APPLICATION_JSON)]
    async fn put_user(
        &self,
        id: u64,
        age: i32,
        #[body] user: &User,
        #[header(COOKIE)] cookie: &str
    ) -> Result<Response<User>>;
}

// use it
use interfacer_http_hyper::AsyncService;

依赖项

~15MB
~281K SLoC