Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: header in http processor #1788

Merged
merged 10 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions fastn-core/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,14 @@ pub fn validation_error_to_user_err(
fastn_core::http::user_err(converted_error, fastn_core::http::StatusCode::OK)
}

pub(crate) fn get_header_key(header_key: &str) -> Option<&str> {
if let Some(remaining) = header_key.strip_prefix("$header-") {
return remaining.strip_suffix('$');
}

None
}

#[cfg(test)]
mod test {
use actix_web::body::MessageBody;
Expand Down
16 changes: 11 additions & 5 deletions fastn-core/src/library2022/processor/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub async fn process(
}
};

let (mut url, conf) =
let (mut url, mut conf) =
fastn_core::config::utils::get_clean_url(&req_config.config, url.as_str()).map_err(
|e| ftd::interpreter::Error::ParseError {
message: format!("invalid url: {:?}", e),
Expand All @@ -84,16 +84,22 @@ pub async fn process(
.get_value(header.line_number, value.as_str())?
.to_json_string(doc, true)?
{
if let Some(key) = fastn_core::http::get_header_key(header.key.as_str()) {
conf.insert(key.to_string(), value.trim_matches('"').to_string());
continue;
}
if method.as_str().eq("post") {
body.push(format!("\"{}\": {}", header.key, value));
continue;
}
url.query_pairs_mut().append_pair(
header.key.as_str(),
value.trim_start_matches('"').trim_end_matches('"'),
);
url.query_pairs_mut()
.append_pair(header.key.as_str(), value.trim_matches('"'));
}
} else {
if let Some(key) = fastn_core::http::get_header_key(header.key.as_str()) {
conf.insert(key.to_string(), value);
continue;
}
if method.as_str().eq("post") {
body.push(format!(
"\"{}\": \"{}\"",
Expand Down
4 changes: 3 additions & 1 deletion ftd/src/ast/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,9 @@ impl VariableValue {
})
.map(|header| {
let key = header.get_key();
let header_key = if ftd::ast::utils::is_variable_mutable(key.as_str()) {
let header_key = if ftd::ast::utils::is_variable_mutable(key.as_str())
&& !ftd::ast::utils::is_header_key(key.as_str())
{
key.trim_start_matches(ftd::ast::utils::REFERENCE)
} else {
key.as_str()
Expand Down
5 changes: 5 additions & 0 deletions ftd/src/ast/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ pub(crate) fn get_css_and_fields_from_headers(
Ok((css, fields))
}

pub(crate) fn is_header_key(key: &str) -> bool {
key.starts_with(HEADER_KEY_START) && key.ends_with('$')
}

pub const REFERENCE: &str = "$";
pub const CLONE: &str = "*$";
pub const LOOP: &str = "$loop$";
Expand All @@ -82,3 +86,4 @@ pub const IN: &str = " in ";
pub const IF: &str = "if";
pub const FOR: &str = "for";
pub const PROCESSOR: &str = "$processor$";
pub const HEADER_KEY_START: &str = "$header-";
7 changes: 7 additions & 0 deletions integration-tests/_tests/14-http-headers.test.ftd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- import: fastn

-- fastn.test: Passing request headers to http processor

-- fastn.get: Authenticating and Fetching User Details (using http processor)
url: /dummy-json-auth/
http-status: 200
74 changes: 74 additions & 0 deletions integration-tests/dummy-json-auth.ftd
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
-- import: fastn/processors as pr

-- record auth-response:
string token:

-- record user-response:
integer id:
string username:
string email:
string firstName:
string lastName:



-- auth-response auth-res:
$processor$: pr.http
method: post
url: https://dummyjson.com/auth/login
username: kminchelle
password: 0lelplR
$header-content-type$: application/json



-- string bearer-token: $join(a = Bearer, b = *$auth-res.token)



-- user-response user-res:
$processor$: pr.http
method: get
url: https://dummyjson.com/auth/me
$header-authorization$: $bearer-token



-- display-user: $user-res



-- string join(a, b):
string a:
string b:

a + " " + b



-- component display-user:
caption user-res user:

-- ftd.column:

-- ftd.row:
spacing.fixed.rem: 1

-- ftd.text: Username:

-- ftd.text: $user-res.username

-- end: ftd.row

-- ftd.row:
spacing.fixed.rem: 1

-- ftd.text: Email:

-- ftd.text: $user-res.email

-- end: ftd.row

-- end: ftd.column

-- end: display-user
Loading