Feign-RS (Rest client of Rust)

How to use

Demo server

A server has any restful interface (like this : finduserbyid, newuser)

```shell curl 127.1:3000/user/findbyid/1

-> {"id":1,"name":"hello"}

curl -X POST 127.1:3000/user/new_user \ -H 'Content-Type: application/json' \ -d '{"id":1,"name":"Link"}'

-> "Link" ➜ ~

```

Dependencies

```toml [dependencies] feign = "1" reqwest = { version = "0.11", features = ["json"] } serde = "1.0" serde_derive = "1.0"

runtime

tokio = { version = "1.15", features = ["macros", "rt-multi-thread"] } ```

Entites

Add a user entity add derives serdederive::Deserialize and serdederive::Serialize

```rust use serdederive::Deserialize; use serdederive::Serialize;

[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]

pub struct User { pub id: i64, pub name: String, } ```

Feign client

```rust

use feign::{client, ClientResult};

[client(host = "http://127.0.0.1:3000", path = "/user")]

pub trait UserClient { #[get(path = "/findbyid/")] async fn findbyid(&self, #[path] id: i64) -> ClientResult>; #[post(path = "/newuser")] async fn newuser(&self, #[json] user: &User) -> ClientResult>; } ```

Demo

Use

```rust

[tokio::main]

async fn main() { let user_client: UserClient = UserClient::new();

match user_client.find_by_id(12).await {
    Ok(option) => match option {
        Some(user) => println!("user : {}", user.name),
        None => println!("none"),
    },
    Err(err) => panic!("{}", err),
};

match user_client
    .new_user(&User {
        id: 123,
        name: "name".to_owned(),
    })
    .await
{
    Ok(option) => match option {
        Some(result) => println!("result : {}", result),
        None => println!("none"),
    },
    Err(err) => panic!("{}", err),
};

} text user : hello result : name ```

Dynamic modify host with configure_host

```rust

[client(path = "/user")]

pub trait UserClient { }

[tokio::main]

async fn main() { let userclient: UserClient = UserClient::new(); userclient .configure_host(String::from("http://127.0.0.1:3000")) .await; } ```