use conreg_client::FeignError;
use conreg_feign_macro::{feign_client, get, post};
use reqwest::multipart::Form;
use rocket::http::hyper::body::Bytes;
use std::path::Path;
#[feign_client(service_id = "httpbin", url = "https://httpbin.org")]
trait ExampleClient {
#[get("/ip")]
async fn ip(&self) -> Result<String, FeignError>;
#[get(path = "/json")]
async fn json(&self) -> Result<serde_json::Value, FeignError>;
#[post(path = "/post", body = "{data}")]
async fn body(&self, data: Vec<u8>) -> Result<String, FeignError>;
#[get(path = "/image", headers("Accept=image/png"))]
async fn image(&self) -> Result<Bytes, FeignError>;
#[post(path = "/post", form = "{form}")]
async fn form(&self, form: Form) -> Result<String, FeignError>;
#[get(path = "/headers", headers("My-Header={my_header}"))]
async fn headers(&self, my_header: &str) -> Result<String, FeignError>;
}
#[tokio::main]
async fn main() {
let client = ExampleClientImpl::default();
let response = client.ip().await.unwrap();
println!("ip -> {:?}", response);
let response = client.json().await.unwrap();
println!("json -> {:#?}", response);
let response = client.image().await.unwrap();
let path = Path::new("image.png");
std::fs::write(&path, response).unwrap();
println!("image saved -> {:?}", path.canonicalize().unwrap());
let form = Form::new().text("custname", "Hello, this is form form!");
let response = client.form(form).await.unwrap();
println!("form -> {:?}", response);
let response = client.headers("Hello, this is headers!").await.unwrap();
println!("headers -> {:?}", response);
}