use artisan::plugins::{AddRadarPlugin, ParserPlugin, StartPlugin};
use artisan::{Artful, Plugin, Rocket, direction::DirectionKind, flow_ctrl::Next};
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;
struct MethodUrlPlugin {
method: reqwest::Method,
url: String,
}
#[async_trait]
impl Plugin for MethodUrlPlugin {
async fn assembly(&self, rocket: &mut Rocket, next: Next<'_>) -> artisan::Result<()> {
rocket.config.method = self.method.clone();
rocket.config.url = self.url.clone();
next.call(rocket).await
}
}
struct SetDirectionPlugin {
direction: DirectionKind,
}
#[async_trait]
impl Plugin for SetDirectionPlugin {
async fn assembly(&self, rocket: &mut Rocket, next: Next<'_>) -> artisan::Result<()> {
rocket.config.direction = self.direction.clone();
next.call(rocket).await
}
}
#[tokio::main]
async fn main() -> artisan::Result<()> {
let plugins: Vec<Arc<dyn Plugin>> = vec![
Arc::new(StartPlugin),
Arc::new(MethodUrlPlugin {
method: reqwest::Method::GET,
url: "https://httpbin.org/get".to_string(),
}),
Arc::new(AddRadarPlugin),
Arc::new(ParserPlugin),
];
let result = Artful::artful(HashMap::new(), plugins).await?;
if let artisan::Destination::Json(json) = result {
println!("JSON Response: {}", json);
}
let plugins: Vec<Arc<dyn Plugin>> = vec![
Arc::new(StartPlugin),
Arc::new(MethodUrlPlugin {
method: reqwest::Method::GET,
url: "https://httpbin.org/get".to_string(),
}),
Arc::new(SetDirectionPlugin {
direction: DirectionKind::Response,
}),
Arc::new(AddRadarPlugin),
Arc::new(ParserPlugin),
];
let result = Artful::artful(HashMap::new(), plugins).await?;
if let artisan::Destination::Response(response) = result {
println!("Response status: {}", response.status());
println!("Response headers: {:?}", response.headers());
}
Ok(())
}