|
| 1 | +use super::function_traits::BorrowFromStr; |
1 | 2 | use super::{ExecutionContext, RequestInfo}; |
2 | 3 | use crate::webserver::{ |
3 | 4 | database::{ |
@@ -26,8 +27,8 @@ super::function_definition_macro::sqlpage_functions! { |
26 | 27 | environment_variable(name: Cow<str>); |
27 | 28 | exec((&RequestInfo), program_name: Cow<str>, args: Vec<Cow<str>>); |
28 | 29 |
|
29 | | - fetch((&RequestInfo), http_request: SqlPageFunctionParam<super::http_fetch_request::HttpFetchRequest<'_>>); |
30 | | - fetch_with_meta((&RequestInfo), http_request: SqlPageFunctionParam<super::http_fetch_request::HttpFetchRequest<'_>>); |
| 30 | + fetch((&RequestInfo), http_request: Option<Cow<str>>); |
| 31 | + fetch_with_meta((&RequestInfo), http_request: Option<Cow<str>>); |
31 | 32 |
|
32 | 33 | hash_password(password: Option<String>); |
33 | 34 | header((&RequestInfo), name: Cow<str>); |
@@ -185,8 +186,14 @@ fn prepare_request_body( |
185 | 186 |
|
186 | 187 | async fn fetch( |
187 | 188 | request: &RequestInfo, |
188 | | - http_request: super::http_fetch_request::HttpFetchRequest<'_>, |
189 | | -) -> anyhow::Result<String> { |
| 189 | + http_request: Option<Cow<'_, str>>, |
| 190 | +) -> anyhow::Result<Option<String>> { |
| 191 | + let Some(http_request_str) = http_request else { |
| 192 | + return Ok(None); |
| 193 | + }; |
| 194 | + let http_request = |
| 195 | + super::http_fetch_request::HttpFetchRequest::borrow_from_str(http_request_str) |
| 196 | + .with_context(|| "Invalid http fetch request")?; |
190 | 197 | let client = make_http_client(&request.app_state.config) |
191 | 198 | .with_context(|| "Unable to create an HTTP client")?; |
192 | 199 | let req = build_request(&client, &http_request)?; |
@@ -219,7 +226,7 @@ async fn fetch( |
219 | 226 | .to_vec(); |
220 | 227 | let response_str = decode_response(body, http_request.response_encoding.as_deref())?; |
221 | 228 | log::debug!("Fetch response: {response_str}"); |
222 | | - Ok(response_str) |
| 229 | + Ok(Some(response_str)) |
223 | 230 | } |
224 | 231 |
|
225 | 232 | fn decode_response(response: Vec<u8>, encoding: Option<&str>) -> anyhow::Result<String> { |
@@ -259,9 +266,15 @@ fn decode_response(response: Vec<u8>, encoding: Option<&str>) -> anyhow::Result< |
259 | 266 |
|
260 | 267 | async fn fetch_with_meta( |
261 | 268 | request: &RequestInfo, |
262 | | - http_request: super::http_fetch_request::HttpFetchRequest<'_>, |
263 | | -) -> anyhow::Result<String> { |
| 269 | + http_request: Option<Cow<'_, str>>, |
| 270 | +) -> anyhow::Result<Option<String>> { |
264 | 271 | use serde::{ser::SerializeMap, Serializer}; |
| 272 | + let Some(http_request_str) = http_request else { |
| 273 | + return Ok(None); |
| 274 | + }; |
| 275 | + let http_request = |
| 276 | + super::http_fetch_request::HttpFetchRequest::borrow_from_str(http_request_str) |
| 277 | + .with_context(|| "Invalid http fetch request")?; |
265 | 278 |
|
266 | 279 | let client = make_http_client(&request.app_state.config) |
267 | 280 | .with_context(|| "Unable to create an HTTP client")?; |
@@ -337,7 +350,7 @@ async fn fetch_with_meta( |
337 | 350 |
|
338 | 351 | obj.end()?; |
339 | 352 | let return_value = String::from_utf8(resp_str)?; |
340 | | - Ok(return_value) |
| 353 | + Ok(Some(return_value)) |
341 | 354 | } |
342 | 355 |
|
343 | 356 | pub(crate) async fn hash_password(password: Option<String>) -> anyhow::Result<Option<String>> { |
|
0 commit comments