Skip to content

Commit 1b35c85

Browse files
committed
Allow configuring timeout setting on sqlpage.fetch
fixes #386
1 parent cfc0ce3 commit 1b35c85

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- **Empty Uploaded files**: when a form contains an optional file upload field, and the user does not upload a file, the field used to still be accessible to SQLPage file-related functions such as `sqlpage.uploaded_file_path` and `sqlpage.uploaded_file_mime_type`. This is now fixed, and these functions will return `NULL` when the user does not upload a file. `sqlpage.persist_uploaded_file` will not create an empty file in the target directory when the user does not upload a file, instead it will do nothing and return `NULL`.
1414
- In the [map](https://sql.ophir.dev/documentation.sql?component=map#component) component, when top-level latitude and longitude properties are omitted, the map will now center on its markers. This makes it easier to create zoomed maps with a single marker.
1515
- In the [button](https://sql.ophir.dev/documentation.sql?component=button#component) component, add a `download` property to make the button download a file when clicked, a `target` property to open the link in a new tab, and a `rel` property to prevent search engines from following the link.
16+
- New `timeout` option in the [sqlpage.fetch](https://sql.ophir.dev/functions.sql?function=fetch#function) function to set a timeout for the request. This is useful when working with slow or unreliable APIs, large payloads, or when you want to avoid waiting too long for a response.
1617

1718
## 0.22.0 (2024-05-29)
1819
- **Important Security Fix:** The behavior of `SET $x` has been modified to match `SELECT $x`.

examples/official-site/sqlpage/migrations/40_fetch.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ select
5656
''json'' as language,
5757
$api_results as contents;
5858
```
59+
60+
# JSON parameter format
61+
62+
The fetch function accepts either a URL string, or a JSON object with the following parameters:
63+
- `method`: The HTTP method to use. Defaults to `GET`.
64+
- `url`: The URL to fetch.
65+
- `headers`: A JSON object with the headers to send.
66+
- `body`: The body of the request. If it is a JSON object, it will be sent as JSON. If it is a string, it will be sent as is.
67+
- `timeout_ms`: The maximum time to wait for the request, in milliseconds. Defaults to 5000.
68+
5969
'
6070
);
6171
INSERT INTO sqlpage_function_parameters (

src/webserver/database/sqlpage_functions/functions.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ async fn fetch(
132132
Method::GET
133133
};
134134
let mut req = client.request(method, http_request.url.as_ref());
135+
if let Some(timeout) = http_request.timeout_ms {
136+
req = req.timeout(core::time::Duration::from_millis(timeout));
137+
}
135138
for (k, v) in http_request.headers {
136139
req = req.insert_header((k.as_ref(), v.as_ref()));
137140
}

src/webserver/database/sqlpage_functions/http_fetch_request.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub(super) struct HttpFetchRequest<'b> {
99
pub url: Cow<'b, str>,
1010
#[serde(borrow)]
1111
pub method: Option<Cow<'b, str>>,
12+
pub timeout_ms: Option<u64>,
1213
#[serde(borrow, deserialize_with = "deserialize_map_to_vec_pairs")]
1314
pub headers: HeaderVec<'b>,
1415
#[serde(borrow)]
@@ -50,6 +51,7 @@ impl<'a> BorrowFromStr<'a> for HttpFetchRequest<'a> {
5051
method: None,
5152
headers: Vec::new(),
5253
body: None,
54+
timeout_ms: None,
5355
}
5456
} else {
5557
match s {
@@ -72,6 +74,7 @@ impl<'a> HttpFetchRequest<'a> {
7274
.map(|(k, v)| (Cow::Owned(k.into_owned()), Cow::Owned(v.into_owned())))
7375
.collect(),
7476
body: self.body.map(Cow::into_owned).map(Cow::Owned),
77+
timeout_ms: self.timeout_ms,
7578
}
7679
}
7780
}

0 commit comments

Comments
 (0)