Skip to content

Commit b8ee3af

Browse files
authored
Cleaner and saner http request parsing. (#93)
1 parent 107123a commit b8ee3af

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/http.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use std::io::Write;
99

1010
#[derive(Deserialize)]
1111
struct RequestOptions {
12+
#[serde(default)]
1213
output_filename: Option<String>,
14+
#[serde(default)]
1315
body_filename: Option<String>,
1416
}
1517

@@ -22,8 +24,8 @@ struct Response<'a> {
2224

2325
// If the response can be deserialized -> success.
2426
// If the response can't be deserialized -> failure.
25-
byond_fn!(fn http_request_blocking(method, url, body, headers, ...rest) {
26-
let req = match construct_request(method, url, body, headers, rest.first().map(|x| &**x)) {
27+
byond_fn!(fn http_request_blocking(method, url, body, headers, options) {
28+
let req = match construct_request(method, url, body, headers, options) {
2729
Ok(r) => r,
2830
Err(e) => return Some(e.to_string())
2931
};
@@ -35,8 +37,8 @@ byond_fn!(fn http_request_blocking(method, url, body, headers, ...rest) {
3537
});
3638

3739
// Returns new job-id.
38-
byond_fn!(fn http_request_async(method, url, body, headers, ...rest) {
39-
let req = match construct_request(method, url, body, headers, rest.first().map(|x| &**x)) {
40+
byond_fn!(fn http_request_async(method, url, body, headers, options) {
41+
let req = match construct_request(method, url, body, headers, options) {
4042
Ok(r) => r,
4143
Err(e) => return Some(e.to_string())
4244
};
@@ -91,7 +93,7 @@ fn construct_request(
9193
url: &str,
9294
body: &str,
9395
headers: &str,
94-
options: Option<&str>,
96+
options: &str,
9597
) -> Result<RequestPrep> {
9698
let mut req = match method {
9799
"post" => HTTP_CLIENT.post(url),
@@ -114,7 +116,7 @@ fn construct_request(
114116
}
115117

116118
let mut output_filename = None;
117-
if let Some(options) = options {
119+
if !options.is_empty() {
118120
let options: RequestOptions = serde_json::from_str(options)?;
119121
output_filename = options.output_filename;
120122
if let Some(fname) = options.body_filename {

0 commit comments

Comments
 (0)