Skip to content

Commit c2ceddc

Browse files
committed
feat: made header to list
1 parent 8109b51 commit c2ceddc

1 file changed

Lines changed: 52 additions & 11 deletions

File tree

adapter/rest/src/main.rs

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@ use base::{
44
traits::Server as ServerTrait,
55
};
66
use http_body_util::{BodyExt, Full};
7-
use hyper::{header::{HeaderName, HeaderValue}, server::conn::http1};
87
use hyper::{Request, Response};
98
use hyper::{
109
StatusCode,
1110
body::{Bytes, Incoming},
1211
};
12+
use hyper::{
13+
header::{HeaderName, HeaderValue},
14+
server::conn::http1,
15+
};
1316
use hyper_util::rt::TokioIo;
1417
use std::collections::HashMap;
1518
use std::convert::Infallible;
1619
use std::net::SocketAddr;
1720
use std::sync::Arc;
1821
use tokio::net::TcpListener;
1922
use tonic::async_trait;
20-
use tucana::shared::value::Kind;
2123
use tucana::shared::value::Kind::StructValue;
24+
use tucana::shared::{ListValue, value::Kind};
2225
use tucana::shared::{Struct, ValidationFlow, Value};
2326

2427
mod config;
@@ -51,8 +54,6 @@ fn json_error(status: StatusCode, msg: &str) -> Response<Full<Bytes>> {
5154
.unwrap()
5255
}
5356

54-
55-
5657
fn build_response(
5758
status: StatusCode,
5859
headers: HashMap<String, String>,
@@ -86,7 +87,6 @@ fn build_response(
8687
builder.body(Full::new(Bytes::from(body))).unwrap()
8788
}
8889

89-
9090
async fn execute_flow_to_hyper_response(
9191
flow: ValidationFlow,
9292
body: Vec<u8>,
@@ -99,6 +99,7 @@ async fn execute_flow_to_hyper_response(
9999

100100
match store.validate_and_execute_flow(flow, value).await {
101101
Some(result) => {
102+
log::debug!("Recieved Result: {:?}", result);
102103
let Value {
103104
kind: Some(StructValue(Struct { fields })),
104105
} = result
@@ -131,8 +132,8 @@ async fn execute_flow_to_hyper_response(
131132
// headers struct
132133
let Value {
133134
kind:
134-
Some(StructValue(Struct {
135-
fields: header_fields,
135+
Some(Kind::ListValue(ListValue {
136+
values: header_fields,
136137
})),
137138
} = headers_val
138139
else {
@@ -144,9 +145,46 @@ async fn execute_flow_to_hyper_response(
144145

145146
let http_headers: HashMap<String, String> = header_fields
146147
.iter()
147-
.filter_map(|(k, v)| match &v.kind {
148-
Some(Kind::StringValue(s)) if !s.is_empty() => Some((k.clone(), s.clone())),
149-
_ => None,
148+
.filter_map(|x| {
149+
if let Value {
150+
kind: Some(StructValue(Struct { fields: f })),
151+
} = x
152+
{
153+
return Some(f);
154+
} else {
155+
return None;
156+
}
157+
})
158+
.filter_map(|f| {
159+
let key = match f.get("key") {
160+
Some(value) => {
161+
if let Value {
162+
kind: Some(Kind::StringValue(x)),
163+
} = value
164+
{
165+
x
166+
} else {
167+
return None;
168+
}
169+
}
170+
None => return None,
171+
};
172+
let value = match f.get("value") {
173+
Some(value) => {
174+
if let Value {
175+
kind: Some(Kind::StringValue(x)),
176+
} = value
177+
{
178+
x
179+
} else {
180+
return None;
181+
}
182+
}
183+
None => return None,
184+
};
185+
186+
187+
return Some((key.clone(), value.clone()));
150188
})
151189
.collect();
152190

@@ -167,7 +205,10 @@ async fn execute_flow_to_hyper_response(
167205
StatusCode::from_u16(code as u16).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
168206
build_response(status, http_headers, json)
169207
}
170-
None => json_error(StatusCode::INTERNAL_SERVER_ERROR, "Flow execution failed"),
208+
None => {
209+
log::error!("flow execution failed");
210+
return json_error(StatusCode::INTERNAL_SERVER_ERROR, "Flow execution failed");
211+
}
171212
}
172213
}
173214

0 commit comments

Comments
 (0)