diff --git a/Cargo.lock b/Cargo.lock index c320eb4..a3bd763 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -215,7 +215,7 @@ dependencies = [ [[package]] name = "thanix" -version = "0.1.0-alpha.1" +version = "0.1.0-alpha.3" dependencies = [ "clap", "convert_case", diff --git a/Cargo.toml b/Cargo.toml index 3155f3f..1d664ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "thanix" authors = ["Christopher Hock "] -version = "0.1.0-alpha.2" +version = "0.1.0-alpha.3" publish = true edition = "2021" description = "A yaml-to-rust code generator for generating Rust code from yaml config files e.g. as found in openAPI." diff --git a/README.md b/README.md index 82a4723..61bb04c 100644 --- a/README.md +++ b/README.md @@ -49,18 +49,16 @@ cargo run -- --input-file $PATH_TO_YOUR_YAML --uri $URI_TO_YOUR_API This may look like this: ```bash -cargo run -- --input-file ./api_config.yaml --uri https://demo.netbox.dev +cargo run -- --input-file ./api_config.yaml ``` -> **NOTE:** Make sure that you **do not end your URI with a slash (/)**. As this would mangle the API paths. - This step will result in your `thanix_client` being generated. To view the next steps please scroll **down to the Usage section**. Optional: -3. Install Thanix using `cargo install`. +1. Install Thanix using `cargo install`. You can also install the crate on your system, so you always have it available. To do so, run this command while in the Thanix project directory: @@ -92,10 +90,8 @@ We will update you as soon as we have news. After you have installed Thanix in a way you see fit, you use it by passing it **two mandatory parameters** like this: ```bash -thanix --input-file $YOUR_API_YAML --uri $YOUR_API_URI +thanix --input-file $YOUR_API_YAML ``` - The `--input-file` parameter is a path to your `.yaml`-file you want to use as an input. This is usually the API schema file your want to generate a client for. -- The `--uri` is the URI to your API in a format like this: `https://demo.netbox.dev`. - **Make sure this URI does not end with a `/`** \ No newline at end of file diff --git a/src/bindgen.rs b/src/bindgen.rs index 61e455a..b648a74 100644 --- a/src/bindgen.rs +++ b/src/bindgen.rs @@ -288,7 +288,7 @@ fn if_some(this: Option, func: F) { } /// Generates the Rust bindings from a file. -pub fn gen(input_path: impl AsRef, url: String) { +pub fn gen(input_path: impl AsRef) { // Parse the schema. let input = std::fs::read_to_string(input_path).unwrap(); let yaml: Schema = serde_yaml::from_str(&input).unwrap(); @@ -405,7 +405,7 @@ pub fn gen(input_path: impl AsRef, url: String) { } fs::write( "thanix_client/util.rs", - format!(include_str!("templates/util.rs.template"), url).as_bytes(), + include_str!("templates/util.rs.template").as_bytes(), ) .unwrap(); create_lib_dir("thanix_client").unwrap(); diff --git a/src/main.rs b/src/main.rs index 829b8f4..d7f40b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,9 +9,6 @@ struct Args { /// Path to a YAML schema file. #[arg(short, long)] input_file: Option, - /// URI of your netbox instance - #[arg(long)] - uri: String, } fn main() { @@ -35,7 +32,7 @@ fn main() { ); match args.input_file { - Some(file) => bindgen::gen(file, args.uri), + Some(file) => bindgen::gen(file), None => println!("Error: You need to provide a YAML schema to generate from."), } } diff --git a/src/templates/path.template b/src/templates/path.template index 5713146..f110ffc 100644 --- a/src/templates/path.template +++ b/src/templates/path.template @@ -1,6 +1,7 @@ {} {} pub fn {}(query: {}{}) -> Result {{ - return REQWEST_CLIENT.lock().unwrap().as_ref().unwrap().{}(format!("{{}}{}?{{}}", REQWEST_BASE_URL, serde_qs::to_string(&query).unwrap())).send(); + let binding = THANIX_CLIENT.lock().unwrap(); + let state = binding.as_ref().unwrap(); + return state.client.{}(format!("{{}}{}?{{}}", state.base_url, serde_qs::to_string(&query).unwrap())).header("Authorization", format!("Token {{}}", state.authentication_token)).send(); }} - diff --git a/src/templates/usings.template b/src/templates/usings.template index e6c782a..2205e93 100644 --- a/src/templates/usings.template +++ b/src/templates/usings.template @@ -1,4 +1,3 @@ use serde_qs; use reqwest::Url; -use crate::util::{REQWEST_BASE_URL, REQWEST_CLIENT}; - +use crate::util::THANIX_CLIENT; diff --git a/src/templates/util.rs.template b/src/templates/util.rs.template index a2a0841..a5db8f3 100644 --- a/src/templates/util.rs.template +++ b/src/templates/util.rs.template @@ -1,4 +1,9 @@ use std::sync::Mutex; -pub static REQWEST_CLIENT: Mutex> = Mutex::new(None); -pub static REQWEST_BASE_URL: &str = "{}"; +pub struct ThanixClient { + pub client: reqwest::blocking::Client, + pub base_url: String, + pub authentication_token: String, +} + +pub static THANIX_CLIENT: Mutex> = Mutex::new(None);