Skip to content

Commit

Permalink
Correctly handle response codes
Browse files Browse the repository at this point in the history
  • Loading branch information
marv7000 committed Feb 26, 2024
1 parent 46354d8 commit c45bc8a
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/pathgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ fn gen_fn(name: &str, op_type: &str, op: &Operation) -> String {
result += ", Error>";

// Build the function body.
result += " {\n\tstate.client.";
result += " {\n\tlet r#response = state.client.";
result += op_type;
result += "(format!(\"{}";
result += name;
Expand All @@ -215,9 +215,31 @@ fn gen_fn(name: &str, op_type: &str, op: &Operation) -> String {
fn_header_params
.iter()
.for_each(|(name, _)| result += &format!("\n.header(\"{}\", header_{})", &name, &name));
result += "\t\t.send()?\n";
result += "\t\t.json()\n";
result += "}\n";
result += "\t\t.send()?;\n";
result += "\tmatch r#response.status().as_u16() {\n";

// Match response code.
for (status, response) in &op.responses.responses {
match response {
ReferenceOr::Item(x) => {
if let Some(y) = &x.content.get("application/json") {
result += &format!(
"\t\t{} => {{ Ok({}::Http{}(r#response.json::<{}>()?)) }},\n",
status,
&fn_response_name,
status,
&bindgen::type_to_string(&y.schema.as_ref().unwrap())
);
}
}
_ => (),
}
}

// Unknown response code.
result += "\t\t_ => { Ok(";
result += &fn_response_name;
result += "::None) }\n\t}\n}\n";

return result;
}
Expand Down

0 comments on commit c45bc8a

Please sign in to comment.