Skip to content

Commit

Permalink
Rest get body (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierHecart authored Oct 5, 2023
1 parent 6a485c6 commit 1f935b1
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions plugins/zenoh-plugin-rest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ fn with_extended_string<R, F: FnMut(&mut String) -> R>(
result
}

async fn query(req: Request<(Arc<Session>, String)>) -> tide::Result<Response> {
async fn query(mut req: Request<(Arc<Session>, String)>) -> tide::Result<Response> {
log::trace!("Incoming GET request: {:?}", req);

let first_accept = match req.header("accept") {
Expand Down Expand Up @@ -374,6 +374,7 @@ async fn query(req: Request<(Arc<Session>, String)>) -> tide::Result<Response> {
},
))
} else {
let body = req.body_bytes().await.unwrap_or_default();
let url = req.url();
let key_expr = match path_to_key_expr(url.path(), &req.state().1) {
Ok(ke) => ke,
Expand All @@ -397,14 +398,15 @@ async fn query(req: Request<(Arc<Session>, String)>) -> tide::Result<Response> {
QueryConsolidation::from(zenoh::query::ConsolidationMode::Latest)
};
let raw = selector.decode().any(|(k, _)| k.as_ref() == RAW_KEY);
match req
.state()
.0
.get(&selector)
.consolidation(consolidation)
.res()
.await
{
let mut query = req.state().0.get(&selector).consolidation(consolidation);
if !body.is_empty() {
let encoding: Encoding = req
.content_type()
.map(|m| m.to_string().into())
.unwrap_or_default();
query = query.with_value(Value::from(body).encoding(encoding));
}
match query.res().await {
Ok(receiver) => {
if raw {
Ok(to_raw_response(receiver).await)
Expand Down Expand Up @@ -439,7 +441,7 @@ async fn write(mut req: Request<(Arc<Session>, String)>) -> tide::Result<Respons
};
let encoding: Encoding = req
.content_type()
.map(|m| m.essence().to_owned().into())
.map(|m| m.to_string().into())
.unwrap_or_default();

// @TODO: Define the right congestion control value
Expand Down Expand Up @@ -481,16 +483,26 @@ pub async fn run(runtime: Runtime, conf: Config) -> ZResult<()> {
app.with(
tide::security::CorsMiddleware::new()
.allow_methods(
"GET, PUT, PATCH, DELETE"
"GET, POST, PUT, PATCH, DELETE"
.parse::<http_types::headers::HeaderValue>()
.unwrap(),
)
.allow_origin(tide::security::Origin::from("*"))
.allow_credentials(false),
);

app.at("/").get(query).put(write).patch(write).delete(write);
app.at("*").get(query).put(write).patch(write).delete(write);
app.at("/")
.get(query)
.post(query)
.put(write)
.patch(write)
.delete(write);
app.at("*")
.get(query)
.post(query)
.put(write)
.patch(write)
.delete(write);

if let Err(e) = app.listen(conf.http_port).await {
log::error!("Unable to start http server for REST: {:?}", e);
Expand Down

0 comments on commit 1f935b1

Please sign in to comment.