Skip to content

Commit

Permalink
Merge pull request #63 from Julien-cpsn/30-support-the-input-file-for…
Browse files Browse the repository at this point in the history
…mat-to-yaml

Added yaml support for collection files
  • Loading branch information
Julien-cpsn authored May 3, 2024
2 parents 5a88339 + f3a267c commit 5500fe6
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 106 deletions.
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ image = "0.24.9"
syntect = "5.2.0"
serde = { version = "1.0.197", features = ["derive", "rc"] }
serde_json = "1.0.114"
serde_yaml = "0.9.34"
jsonxf = "1.1.1"
toml = "0.8.11"
boa_engine = { version = "0.18.0", default-features = false }
Expand Down
167 changes: 83 additions & 84 deletions README.md

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions base_collections/yaml_collection.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Yaml collection
requests:
- name: Test Get
url: https://httpbin.org/get
method: GET
params: []
headers:
- data:
- cache-control
- no-cache
enabled: true
- data:
- user-agent
- ATAC/v0.15.1
enabled: true
- data:
- accept
- '*/*'
enabled: true
- data:
- accept-encoding
- gzip, deflate, br
enabled: true
- data:
- connection
- keep-alive
enabled: true
body: no_body
auth: no_auth
scripts:
pre_request_script: null
post_request_script: null
settings:
use_config_proxy: true
allow_redirects: true
store_received_cookies: true
pretty_print_response_content: true
9 changes: 7 additions & 2 deletions src/app/app_logic/collection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::sync::Arc;

use parking_lot::RwLock;

use crate::app::app::App;
use crate::app::startup::args::ARGS;
use crate::request::auth::Auth;
Expand Down Expand Up @@ -181,11 +183,14 @@ impl App<'_> {
return;
}
}


let file_format = self.config.get_preferred_collection_file_format();

let new_collection = Collection {
name: new_collection_name.clone(),
requests: vec![],
path: ARGS.directory.join(format!("{}.json", new_collection_name.clone()))
path: ARGS.directory.join(format!("{}.{}", new_collection_name.clone(), file_format.to_string())),
file_format,
};

self.collections.push(new_collection);
Expand Down
25 changes: 18 additions & 7 deletions src/app/files/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use std::path::PathBuf;
use crate::app::app::App;
use crate::app::startup::args::ARGS;
use crate::panic_error;
use crate::request::collection::Collection;
use crate::request::collection::{Collection, CollectionFileFormat};
use crate::request::collection::CollectionFileFormat::{Json, Yaml};

impl App<'_> {
/// Set the app request to the requests found in the collection file
pub fn set_collections_from_file(&mut self, path_buf: PathBuf) {
pub fn set_collections_from_file(&mut self, path_buf: PathBuf, file_format: CollectionFileFormat) {
let mut file_content = String::new();

let mut collection_file = OpenOptions::new()
Expand All @@ -22,12 +23,19 @@ impl App<'_> {

collection_file.read_to_string(&mut file_content).expect("\tCould not read collection file");

let mut collection: Collection = match serde_json::from_str(&file_content) {
Ok(collection) => collection,
Err(e) => panic_error(format!("Could not parse collection\n\t{e}"))
let mut collection: Collection = match file_format {
Json => match serde_json::from_str(&file_content) {
Ok(collection) => collection,
Err(e) => panic_error(format!("Could not parse JSON collection\n\t{e}"))
},
Yaml => match serde_yaml::from_str(&file_content) {
Ok(collection) => collection,
Err(e) => panic_error(format!("Could not parse YAML collection\n\t{e}"))
}
};

collection.path = path_buf;
collection.file_format = file_format;

self.collections.push(collection);

Expand All @@ -53,9 +61,12 @@ impl App<'_> {
.open(&temp_file_path)
.expect("Could not open temp file");

let collection_json = serde_json::to_string_pretty(collection).expect("Could not serialize collection");
let collection_stringed = match collection.file_format {
Json => serde_json::to_string_pretty(collection).expect("Could not serialize collection to JSON"),
Yaml => serde_yaml::to_string(collection).expect("Could not serialize collection to YAML")
};

temp_file.write_all(collection_json.as_bytes()).expect("Could not write to temp file");
temp_file.write_all(collection_stringed.as_bytes()).expect("Could not write to temp file");
temp_file.flush().unwrap();

fs::rename(temp_file_path, &collection.path).expect("Could not move temp file to collection file");
Expand Down
14 changes: 14 additions & 0 deletions src/app/files/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use crate::app::app::App;
use crate::panic_error;
use crate::request::collection::CollectionFileFormat;

#[derive(Default, Serialize, Deserialize)]
pub struct Config {
#[serde(default)]
pub disable_syntax_highlighting: Option<bool>,

#[serde(default)]
pub disable_cors: Option<bool>,

#[serde(default)]
pub disable_images_preview: Option<bool>,

#[serde(default)]
pub preferred_collection_file_format: Option<CollectionFileFormat>,

pub proxy: Option<Proxy>
}

Expand All @@ -34,6 +41,13 @@ impl Config {
pub fn is_image_preview_disabled(&self) -> bool {
return self.disable_images_preview.unwrap_or(false)
}

pub fn get_preferred_collection_file_format(&self) -> CollectionFileFormat {
return match &self.preferred_collection_file_format {
None => CollectionFileFormat::default(),
Some(file_format) => file_format.clone()
};
}
}

impl App<'_> {
Expand Down
18 changes: 12 additions & 6 deletions src/app/files/postman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::app::startup::args::ARGS;
use crate::panic_error;
use crate::request::auth::Auth;
use crate::request::body::ContentType;
use crate::request::collection::Collection;
use crate::request::collection::{Collection, CollectionFileFormat};
use crate::request::method::Method;
use crate::request::request::{DEFAULT_HEADERS, KeyValue, Request};
use crate::request::settings::RequestSettings;
Expand All @@ -34,11 +34,14 @@ impl App<'_> {
}
}

let file_format = self.config.get_preferred_collection_file_format();

let mut collections: Vec<Collection> = vec![
Collection {
name: collection_name.clone(),
requests: vec![],
path: ARGS.directory.join(format!("{}.json", collection_name))
path: ARGS.directory.join(format!("{}.{}", collection_name, file_format.to_string())),
file_format,
}
];

Expand All @@ -60,7 +63,9 @@ impl App<'_> {
let mut temp_nesting_prefix = String::new();
let new_collections: Vec<Collection> = vec![];

recursive_has_requests(&mut item, &mut collections, &mut temp_nesting_prefix, &mut depth_level, max_depth);
let file_format = self.config.get_preferred_collection_file_format();

recursive_has_requests(&mut item, &mut collections, &mut temp_nesting_prefix, &mut depth_level, max_depth, file_format);

collections.extend(new_collections);
} else {
Expand All @@ -84,7 +89,7 @@ impl App<'_> {
}
}

fn recursive_has_requests(item: &mut Items, collections: &mut Vec<Collection>, mut nesting_prefix: &mut String, mut depth_level: &mut u16, max_depth: u16) -> Option<Arc<RwLock<Request>>> {
fn recursive_has_requests(item: &mut Items, collections: &mut Vec<Collection>, mut nesting_prefix: &mut String, mut depth_level: &mut u16, max_depth: u16, file_format: CollectionFileFormat) -> Option<Arc<RwLock<Request>>> {
return if is_folder(&item) {
let mut requests: Vec<Arc<RwLock<Request>>> = vec![];

Expand All @@ -107,7 +112,7 @@ fn recursive_has_requests(item: &mut Items, collections: &mut Vec<Collection>, m
let mut has_sub_folders = false;

for mut sub_item in item.item.clone().unwrap() {
if let Some(request) = recursive_has_requests(&mut sub_item, collections, &mut nesting_prefix, &mut depth_level, max_depth) {
if let Some(request) = recursive_has_requests(&mut sub_item, collections, &mut nesting_prefix, &mut depth_level, max_depth, file_format) {
requests.push(request);
} else {
has_sub_folders = true;
Expand All @@ -125,7 +130,8 @@ fn recursive_has_requests(item: &mut Items, collections: &mut Vec<Collection>, m
let collection = Collection {
name: collection_name.clone(),
requests,
path: ARGS.directory.join(format!("{}.json", collection_name)),
path: ARGS.directory.join(format!("{}.{}", collection_name, file_format.to_string())),
file_format,
};

collections.push(collection);
Expand Down
6 changes: 5 additions & 1 deletion src/app/startup/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fs::OpenOptions;
use crate::app::app::App;
use crate::app::startup::args::{ARGS, Command};
use crate::panic_error;
use crate::request::collection::CollectionFileFormat;

impl App<'_> {
/// Method called before running the app
Expand Down Expand Up @@ -44,7 +45,10 @@ impl App<'_> {
println!("Checking: {}", path.display());

if file_name.ends_with(".json") {
self.set_collections_from_file(path);
self.set_collections_from_file(path, CollectionFileFormat::Json);
}
else if file_name.ends_with(".yaml") {
self.set_collections_from_file(path, CollectionFileFormat::Yaml);
}
else if file_name.starts_with(".env.") {
self.add_environment_from_file(path)
Expand Down
17 changes: 16 additions & 1 deletion src/request/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::Arc;
use parking_lot::RwLock;
use ratatui::text::{Line, Span};
use serde::{Deserialize, Serialize};
use strum::Display;
use tui_tree_widget::TreeItem;
use crate::request::request::Request;

Expand All @@ -12,7 +13,21 @@ pub struct Collection {
pub requests: Vec<Arc<RwLock<Request>>>,

#[serde(skip)]
pub path: PathBuf
pub path: PathBuf,

#[serde(skip)]
pub file_format: CollectionFileFormat
}

#[derive(Debug, Default, Copy, Clone, Display, Serialize, Deserialize)]
pub enum CollectionFileFormat {
#[default]
#[serde(alias="json", alias="JSON")]
#[strum(to_string = "json")]
Json,
#[serde(alias="yaml", alias="YAML")]
#[strum(to_string = "yaml")]
Yaml
}

impl Collection {
Expand Down
2 changes: 1 addition & 1 deletion src/request/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub struct Request {

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct KeyValue {
pub data: (String, String),
pub enabled: bool,
pub data: (String, String)
}

impl App<'_> {
Expand Down

0 comments on commit 5500fe6

Please sign in to comment.