-
Hi! I do not understand how to use the Webhook macro. In the macro documentation we have the following example: use poem_openapi::{Object, Webhook, payload::Json, OpenApiService};
#[derive(Object)]
struct Pet {
id: i64,
name: String,
}
#[Webhook]
trait MyWebhooks: Sync {
#[oai(method = "post")]
async fn new_pet(&self, pet: Json<Pet>);
}
let api = OpenApiService::new((), "Demo", "1.0.0")
.webhooks::<dyn MyWebhooks>(); In the main openapi documentation we have that example for how to use the swagger ui: use poem::{listener::TcpListener, Route, Server};
use poem_openapi::{payload::PlainText, OpenApi, OpenApiService};
struct Api;
#[OpenApi]
impl Api {
/// Hello world
#[oai(path = "/", method = "get")]
async fn index(&self) -> PlainText<&'static str> {
PlainText("Hello World")
}
}
let api_service =
OpenApiService::new(Api, "Hello World", "1.0").server("http://localhost:3000");
let ui = api_service.swagger_ui();
let app = Route::new().nest("/", api_service).nest("/docs", ui);
Server::new(TcpListener::bind("127.0.0.1:3000"))
.run(app)
.await; If I merge the examples I get: use poem::{listener::TcpListener, Route, Server};
use poem_openapi::{payload::Json, Object, Webhook};
use poem_openapi::{payload::PlainText, OpenApi, OpenApiService};
struct Api;
#[OpenApi]
impl Api {
/// Hello world
#[oai(path = "/", method = "get")]
async fn index(&self) -> PlainText<&'static str> {
PlainText("Hello World")
}
}
#[derive(Object)]
struct Pet {
id: i64,
name: String,
}
#[Webhook]
trait MyWebhooks: Sync {
#[oai(method = "post")]
async fn new_pet(&self, pet: Json<Pet>);
}
#[tokio::main]
async fn main() {
let api_service = OpenApiService::new(Api, "Hello World", "1.0")
.webhooks::<dyn MyWebhooks>()
.server("http://localhost:3000");
let ui = api_service.swagger_ui();
let app = Route::new().nest("/", api_service).nest("/docs", ui);
Server::new(TcpListener::bind("127.0.0.1:3000"))
.run(app)
.await
.unwrap();
} If I try to compile that example I get the error that some trait bound is not satisfied:
In the openapi-example folder I found no matching example for webhooks. Used `Cargo.toml`[package]
name = "webkooook"
version = "0.1.0"
edition = "2021"
[dependencies]
poem = "1.3.47"
poem-openapi = { version = "2.0.18", features = ["swagger-ui"] }
tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] } |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
sorry, docs is incorrect. 😂 let api_service = OpenApiService::new(Api, "Hello World", "1.0")
.webhooks::<&dyn MyWebhooks>() <<<< <<
.server("http://localhost:3000"); |
Beta Was this translation helpful? Give feedback.
-
油条哥,我想请教您一下这个webhook主要作用是什么?这个好像和以前我在其它语言或者用于上下文环境传参或延时触发new_pet不太一样。 use poem::{listener::TcpListener, Route, Server};
use poem_openapi::{payload::Json, Object, Webhook};
use poem_openapi::{payload::PlainText, OpenApi, OpenApiService};
struct Api;
#[OpenApi]
impl Api {
/// Hello world
#[oai(path = "/", method = "get")]
async fn index(&self) -> PlainText<&'static str> {
PlainText("Hello World")
}
}
#[derive(Object)]
struct Pet {
id: i64,
name: String,
}
#[Webhook]
trait MyWebhooks: Sync {
#[oai(method = "post")]
async fn new_pet(&self, pet: Json<Pet>);
}
#[tokio::main]
async fn main() {
let api_service = OpenApiService::new(Api, "Hello World", "1.0")
.webhooks::<&dyn MyWebhooks>()
.server("http://localhost:3000");
let ui = api_service.swagger_ui();
let app = Route::new().nest("/", api_service).nest("/docs", ui);
Server::new(TcpListener::bind("127.0.0.1:3000"))
.run(app)
.await
.unwrap();
} |
Beta Was this translation helpful? Give feedback.
sorry, docs is incorrect. 😂