Skip to content

Commit fca0477

Browse files
author
Christopher Hock
authored
Add singleton class (#18)
* Add singleton class * fix path implementation template
1 parent 52d666e commit fca0477

File tree

8 files changed

+19
-21
lines changed

8 files changed

+19
-21
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "thanix"
33
authors = ["Christopher Hock <christopher-hock@protonmail.com>"]
4-
version = "0.1.0-alpha.2"
4+
version = "0.1.0-alpha.3"
55
publish = true
66
edition = "2021"
77
description = "A yaml-to-rust code generator for generating Rust code from yaml config files e.g. as found in openAPI."

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,16 @@ cargo run -- --input-file $PATH_TO_YOUR_YAML --uri $URI_TO_YOUR_API
4949
This may look like this:
5050

5151
```bash
52-
cargo run -- --input-file ./api_config.yaml --uri https://demo.netbox.dev
52+
cargo run -- --input-file ./api_config.yaml
5353
```
5454

55-
> **NOTE:** Make sure that you **do not end your URI with a slash (/)**. As this would mangle the API paths.
56-
5755
This step will result in your `thanix_client` being generated.
5856

5957
To view the next steps please scroll **down to the Usage section**.
6058

6159
Optional:
6260

63-
3. Install Thanix using `cargo install`.
61+
1. Install Thanix using `cargo install`.
6462

6563
You can also install the crate on your system, so you always have it available.
6664
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.
9290
After you have installed Thanix in a way you see fit, you use it by passing it **two mandatory parameters** like this:
9391

9492
```bash
95-
thanix --input-file $YOUR_API_YAML --uri $YOUR_API_URI
93+
thanix --input-file $YOUR_API_YAML
9694
```
9795

9896
- The `--input-file` parameter is a path to your `.yaml`-file you want to use as an input. This is usually the API
9997
schema file your want to generate a client for.
100-
- The `--uri` is the URI to your API in a format like this: `https://demo.netbox.dev`.
101-
**Make sure this URI does not end with a `/`**

src/bindgen.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ fn if_some<F: FnOnce(&T), T>(this: Option<T>, func: F) {
288288
}
289289

290290
/// Generates the Rust bindings from a file.
291-
pub fn gen(input_path: impl AsRef<std::path::Path>, url: String) {
291+
pub fn gen(input_path: impl AsRef<std::path::Path>) {
292292
// Parse the schema.
293293
let input = std::fs::read_to_string(input_path).unwrap();
294294
let yaml: Schema = serde_yaml::from_str(&input).unwrap();
@@ -405,7 +405,7 @@ pub fn gen(input_path: impl AsRef<std::path::Path>, url: String) {
405405
}
406406
fs::write(
407407
"thanix_client/util.rs",
408-
format!(include_str!("templates/util.rs.template"), url).as_bytes(),
408+
include_str!("templates/util.rs.template").as_bytes(),
409409
)
410410
.unwrap();
411411
create_lib_dir("thanix_client").unwrap();

src/main.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ struct Args {
99
/// Path to a YAML schema file.
1010
#[arg(short, long)]
1111
input_file: Option<String>,
12-
/// URI of your netbox instance
13-
#[arg(long)]
14-
uri: String,
1512
}
1613

1714
fn main() {
@@ -35,7 +32,7 @@ fn main() {
3532
);
3633

3734
match args.input_file {
38-
Some(file) => bindgen::gen(file, args.uri),
35+
Some(file) => bindgen::gen(file),
3936
None => println!("Error: You need to provide a YAML schema to generate from."),
4037
}
4138
}

src/templates/path.template

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{}
22
{}
33
pub fn {}(query: {}{}) -> Result<reqwest::blocking::Response, reqwest::Error> {{
4-
return REQWEST_CLIENT.lock().unwrap().as_ref().unwrap().{}(format!("{{}}{}?{{}}", REQWEST_BASE_URL, serde_qs::to_string(&query).unwrap())).send();
4+
let binding = THANIX_CLIENT.lock().unwrap();
5+
let state = binding.as_ref().unwrap();
6+
return state.client.{}(format!("{{}}{}?{{}}", state.base_url, serde_qs::to_string(&query).unwrap())).header("Authorization", format!("Token {{}}", state.authentication_token)).send();
57
}}
6-

src/templates/usings.template

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
use serde_qs;
22
use reqwest::Url;
3-
use crate::util::{REQWEST_BASE_URL, REQWEST_CLIENT};
4-
3+
use crate::util::THANIX_CLIENT;

src/templates/util.rs.template

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
use std::sync::Mutex;
22

3-
pub static REQWEST_CLIENT: Mutex<Option<reqwest::blocking::Client>> = Mutex::new(None);
4-
pub static REQWEST_BASE_URL: &str = "{}";
3+
pub struct ThanixClient {
4+
pub client: reqwest::blocking::Client,
5+
pub base_url: String,
6+
pub authentication_token: String,
7+
}
8+
9+
pub static THANIX_CLIENT: Mutex<Option<ThanixClient>> = Mutex::new(None);

0 commit comments

Comments
 (0)