basic client object, request methods

This commit is contained in:
Joshua M. Clulow 2021-07-01 08:41:45 +00:00
parent 25ed132e2e
commit 9c90253237
1 changed files with 51 additions and 7 deletions

View File

@ -8,6 +8,8 @@ use std::fs::{File, OpenOptions};
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
mod template;
fn save<P>(p: P, data: &str) -> Result<()> fn save<P>(p: P, data: &str) -> Result<()>
where where
P: AsRef<Path>, P: AsRef<Path>,
@ -755,11 +757,13 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result<String> {
/* /*
* Deal with any dependencies we require to produce this client. * Deal with any dependencies we require to produce this client.
*/ */
a("");
if ts.import_chrono { if ts.import_chrono {
a("");
a("use chrono::prelude::*;"); a("use chrono::prelude::*;");
a("");
} }
a("use serde::{Serialize, Deserialize};");
a("use anyhow::Result;"); /* XXX */
a("");
/* /*
* Declare named types we know about: * Declare named types we know about:
@ -767,6 +771,7 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result<String> {
for te in ts.id_to_entry.values() { for te in ts.id_to_entry.values() {
match &te.details { match &te.details {
TypeDetails::Object(omap) => { TypeDetails::Object(omap) => {
a("#[derive(Serialize, Deserialize, Debug)]");
a(&format!("pub struct {} {{", te.name.as_deref().unwrap())); a(&format!("pub struct {} {{", te.name.as_deref().unwrap()));
for (name, tid) in omap.iter() { for (name, tid) in omap.iter() {
a(&format!(" pub {}: {},", name, ts.render_type(tid)?)); a(&format!(" pub {}: {},", name, ts.render_type(tid)?));
@ -785,10 +790,26 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result<String> {
* Declare the client object: * Declare the client object:
*/ */
a("pub struct Client {"); a("pub struct Client {");
a(" baseurl: String,");
a(" client: reqwest::Client,");
a("}"); a("}");
a(""); a("");
a("impl Client {"); a("impl Client {");
a(" pub fn new(baseurl: &str) -> Client {");
a(" let dur = std::time::Duration::from_secs(15);");
a(" let client = reqwest::ClientBuilder::new()");
a(" .connect_timeout(dur)");
a(" .timeout(dur)");
a(" .build()");
a(" .unwrap();");
a("");
a(" Client {");
a(" baseurl: baseurl.to_string(),");
a(" client,");
a(" }");
a(" }");
a("");
/* /*
* Generate a function for each Operation. * Generate a function for each Operation.
@ -817,11 +838,11 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result<String> {
let mut bounds: Vec<String> = Vec::new(); let mut bounds: Vec<String> = Vec::new();
let body_param = if let Some(b) = &o.request_body { let (body_param, body_func) = if let Some(b) = &o.request_body {
let b = b.item()?; let b = b.item()?;
if b.is_binary()? { if b.is_binary()? {
bounds.push("B: Into<reqwest::Body>".to_string()); bounds.push("B: Into<reqwest::Body>".to_string());
Some("B".to_string()) (Some("B".to_string()), Some("body".to_string()))
} else { } else {
let mt = b.content_json()?; let mt = b.content_json()?;
if !mt.encoding.is_empty() { if !mt.encoding.is_empty() {
@ -830,13 +851,16 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result<String> {
if let Some(s) = &mt.schema { if let Some(s) = &mt.schema {
let tid = ts.select(None, s)?; let tid = ts.select(None, s)?;
Some(format!("&{}", ts.render_type(&tid)?)) (
Some(format!("&{}", ts.render_type(&tid)?)),
Some("json".to_string()),
)
} else { } else {
bail!("media type encoding, no schema: {:#?}", mt); bail!("media type encoding, no schema: {:#?}", mt);
} }
} }
} else { } else {
None (None, None)
}; };
if bounds.is_empty() { if bounds.is_empty() {
@ -947,7 +971,27 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result<String> {
bail!("responses? {:#?}", o.responses); bail!("responses? {:#?}", o.responses);
} }
a(" unimplemented!();"); /*
* Generate the URL for the request.
*/
let tmp = template::parse(p)?;
a(&tmp.compile());
/*
* Perform the request.
*/
a(&format!(" let res = self.client.{}(url)",
m.to_lowercase()));
if let Some(f) = &body_func {
a(&format!(" .{}(body)", f));
}
a(" .send()");
a(" .await?");
a(" .error_for_status()?;"); /* XXX */
a("");
a(" Ok(res.json().await?)");
a(" }"); a(" }");
a(""); a("");