Skip to content

Commit 9d7eba2

Browse files
authored
Add JSON header and use simple strings for enumerations (#3)
1 parent 980f47d commit 9d7eba2

File tree

5 files changed

+172
-449
lines changed

5 files changed

+172
-449
lines changed

openai_client/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ categories = ["api-bindings", "web-programming::http-client"]
1111
description = "A native Rust client for the complete OpenAI REST API."
1212

1313
[dependencies]
14-
reqwest = { version = "0.12.22", features = ["multipart"] }
14+
reqwest = { version = "0.12.22", features = ["multipart", "json"] }
1515
serde = { version = "1.0.219", features = ["derive"] }
1616
serde_json = "1.0.140"
1717
tokio = { version = "1.46.0", features = ["rt", "macros"] }

openai_client/build.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,9 @@ fn parse_object_type(name: &str, schema: &Yaml, output_file: &mut File) {
426426
if let Some(type_label) = schema_map.get(&Yaml::String("x-oaiTypeLabel".to_string())) {
427427
let type_label_str = type_label.as_str().unwrap();
428428
match type_label_str {
429-
"map" => writeln!(output_file, "pub struct {}(pub serde_json::Value);\n", name).unwrap(),
429+
"map" => {
430+
writeln!(output_file, "pub struct {}(pub serde_json::Value);\n", name).unwrap()
431+
}
430432
_ => unimplemented!("{} with type label {:?}", name, type_label),
431433
}
432434
} else {
@@ -520,6 +522,7 @@ fn parse_oneof_type(name: &str, schema: &Yaml, output_file: &mut File) {
520522
writeln!(output_file, "#[serde(untagged)]").unwrap();
521523
writeln!(output_file, "pub enum {} {{", name).unwrap();
522524

525+
let mut string_enum_already_processed = false;
523526
for (index, one_of_variant) in one_of_list.iter().enumerate() {
524527
let one_of_variant_hash = one_of_variant.as_hash().unwrap();
525528
if let Some(doc) = one_of_variant_hash
@@ -544,28 +547,9 @@ fn parse_oneof_type(name: &str, schema: &Yaml, output_file: &mut File) {
544547
{
545548
match variant_type.as_str() {
546549
"string" => {
547-
// Some variants have two String types to account for enumerations but for
548-
// our type this is not necessary because all String representations are
549-
// equal
550-
if let Some(Yaml::Array(enum_list)) =
551-
one_of_variant_hash.get(&Yaml::String("enum".to_string()))
552-
{
553-
for string_variant in enum_list {
554-
writeln!(
555-
output_file,
556-
"\t#[serde(rename=\"{}\")]",
557-
string_variant.as_str().unwrap()
558-
)
559-
.unwrap();
560-
writeln!(
561-
output_file,
562-
"\t{},",
563-
str_to_camel_case(string_variant.as_str().unwrap())
564-
)
565-
.unwrap();
566-
}
567-
} else {
550+
if !string_enum_already_processed {
568551
writeln!(output_file, "\tString(String),").unwrap();
552+
string_enum_already_processed = true;
569553
}
570554
}
571555
"integer" => {
@@ -1299,7 +1283,7 @@ fn parse_endpoint_path(path_schema: &Yaml, client_output_file: &mut File) {
12991283
if request_body_is_required {
13001284
writeln!(
13011285
client_output_file,
1302-
"\t\trequest = request.body(serde_json::to_string(&request_body)?);",
1286+
"\t\trequest = request.json(&request_body);",
13031287
)
13041288
.unwrap();
13051289
} else {

0 commit comments

Comments
 (0)