Skip to content

Commit 5526068

Browse files
Merge pull request #72 from code0-tech/71-http-create-functions
Http Create Functions
2 parents 28402c1 + 9600c71 commit 5526068

File tree

6 files changed

+90
-21
lines changed

6 files changed

+90
-21
lines changed

Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ edition = "2024"
66
[dependencies]
77
code0-flow = { version = "0.0.14" }
88
tucana = { version = "0.0.33" }
9-
serde = "1.0.219"
10-
serde_json = "1.0.140"
119
tokio = { version = "1.44.1", features = ["rt-multi-thread"] }
1210
log = "0.4.27"
1311
futures-lite = "2.6.0"

src/implementation/http.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use tucana::shared::{Struct, Value};
2+
use tucana::shared::value::Kind;
3+
use crate::context::Context;
4+
use crate::error::RuntimeError;
5+
use crate::registry::HandlerFn;
6+
7+
pub fn collect_http_functions() -> Vec<(&'static str, HandlerFn)> {
8+
vec![
9+
("http::request::create", create_request),
10+
("http::response::create", create_response),
11+
]
12+
}
13+
14+
fn create_request(values: &[Value], _ctx: &mut Context) -> Result<Value, RuntimeError> {
15+
let [Value {
16+
kind: Some(Kind::StringValue(http_method)),
17+
},
18+
Value {
19+
kind: Some(Kind::StructValue(headers)),
20+
},
21+
Value {
22+
kind: Some(Kind::StringValue(http_url)),
23+
},
24+
payload
25+
] = values
26+
else {
27+
return Err(RuntimeError::simple(
28+
"InvalidArgumentRuntimeError",
29+
format!("Expected [method, headers, url, payload] but received {:?}", values),
30+
));
31+
};
32+
33+
let mut fields = std::collections::HashMap::new();
34+
35+
fields.insert("method".to_string(), Value {
36+
kind: Some(Kind::StringValue(http_method.clone())),
37+
});
38+
39+
fields.insert("url".to_string(), Value {
40+
kind: Some(Kind::StringValue(http_url.clone())),
41+
});
42+
43+
fields.insert("headers".to_string(),Value {
44+
kind: Some(Kind::StructValue(headers.clone())),
45+
});
46+
fields.insert("body".to_string(), payload.clone());
47+
48+
Ok(Value {
49+
kind: Some(Kind::StructValue(Struct { fields })),
50+
})
51+
}
52+
53+
fn create_response(values: &[Value], _ctx: &mut Context) -> Result<Value, RuntimeError> {
54+
let [Value {
55+
kind: Some(Kind::NumberValue(http_status_code)),
56+
},
57+
Value {
58+
kind: Some(Kind::StructValue(headers)),
59+
},
60+
payload
61+
] = values
62+
else {
63+
return Err(RuntimeError::simple(
64+
"InvalidArgumentRuntimeError",
65+
format!("Expected [http_status_code, headers, payload] but received {:?}", values),
66+
));
67+
};
68+
69+
let mut fields = std::collections::HashMap::new();
70+
71+
fields.insert("method".to_string(), Value {
72+
kind: Some(Kind::NumberValue(http_status_code.clone())),
73+
});
74+
75+
fields.insert("headers".to_string(),Value {
76+
kind: Some(Kind::StructValue(headers.clone())),
77+
});
78+
fields.insert("body".to_string(), payload.clone());
79+
80+
Ok(Value {
81+
kind: Some(Kind::StructValue(Struct { fields })),
82+
})
83+
}

src/implementation/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use crate::registry::HandlerFn;
22

3-
pub mod array;
4-
pub mod boolean;
3+
mod array;
4+
mod boolean;
55
mod control;
6-
pub mod number;
7-
pub mod object;
8-
pub mod text;
6+
mod number;
7+
mod object;
8+
mod text;
9+
mod http;
910

1011
pub fn collect() -> Vec<(&'static str, HandlerFn)> {
1112
let mut result = vec![];
@@ -16,6 +17,7 @@ pub fn collect() -> Vec<(&'static str, HandlerFn)> {
1617
result.extend(text::collect_text_functions());
1718
result.extend(object::collect_object_functions());
1819
result.extend(control::collect_control_functions());
20+
result.extend(http::collect_http_functions());
1921

2022
result
2123
}

translation/de-DE.toml

Lines changed: 0 additions & 6 deletions
This file was deleted.

translation/en-US.toml

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)