Skip to content

Commit

Permalink
refactor: optimize some functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ebedthan committed Apr 28, 2024
1 parent bd086be commit 88ac028
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 336 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ Full help is available from `xgt --help`.

`xgt` through `ureq` performs peer SSL certificate verification by default.
To tell `xgt` to _not_ verify the peer, use `-k/--insecure` option.
Currently (as of Apr 28, 2024), you should add this options to your command to get the desired result as the GTDB API have a certificate issue.
Currently (as of Apr 28, 2024), you should add this options to your command to get the desired result as GTDB API's server have currently a certificate issue.


### Minimum supported Rust version
## Minimum supported Rust version
`xgt` minimum [Rust](https://www.rust-lang.org/) version is 1.70.0.

### Semver
## Semver
`xgt` is following [Semantic Versioning 2.0](https://semver.org/).

### Licence
## Licence
`xgt` is distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See [LICENSE-APACHE](https://github.com/Ebedthan/xgt/blob/main/LICENSE-APACHE) and [LICENSE-MIT](https://github.com/Ebedthan/xgt/blob/main/LICENSE-MIT) for details.

### Note
## Note
Unstable work is on dev branch.
10 changes: 1 addition & 9 deletions src/api/taxon_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ impl TaxonAPI {
pub fn get_name_request(&self) -> String {
format!("https://api.gtdb.ecogenomic.org/taxon/{}", self.name)
}
pub fn get_name(&self) -> String {
self.name.clone()
}

pub fn get_search_request(&self) -> String {
format!(
"https://api.gtdb.ecogenomic.org/taxon/search/{}?limit=1000000",
Expand Down Expand Up @@ -50,12 +48,6 @@ mod test {
assert_eq!(taxon_api.get_name_request(), expected);
}

#[test]
fn test_get_name() {
let taxon_api = TaxonAPI::from("s__Escherichia coli".to_string());
assert_eq!(taxon_api.get_name(), "s__Escherichia coli");
}

#[test]
fn test_get_search_request() {
let taxon_api = TaxonAPI::from("s__Escherichia coli".to_string());
Expand Down
200 changes: 72 additions & 128 deletions src/cmd/genome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use super::utils::GenomeArgs;
use crate::api::genome_api::GenomeAPI;
use crate::api::genome_api::GenomeRequestType;

use anyhow::{bail, Context, Result};
use anyhow::anyhow;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::fs::OpenOptions;
use std::io::{self, Write};
Expand Down Expand Up @@ -185,52 +186,33 @@ pub fn get_genome_metadata(args: GenomeArgs) -> Result<()> {
for accession in genome_api {
let request_url = accession.request(GenomeRequestType::Metadata);

let response = match agent.get(&request_url).call() {
Ok(r) => r,
Err(ureq::Error::Status(code, _)) => {
bail!("The server returned an unexpected status code ({})", code);
let response = agent.get(&request_url).call().map_err(|e| match e {
ureq::Error::Status(code, _) => {
anyhow!("The server returned an unexpected status code ({})", code)
}
Err(_) => {
bail!("There was an error making the request or receiving the response.");
}
};
_ => anyhow!("There was an error making the request or receiving the response."),
})?;

let genome: GenomeMetadata = response.into_json()?;

match raw {
true => {
let genome_string = serde_json::to_string(&genome)?;
let output = args.get_output();
if let Some(path) = output {
let path_clone = path.clone();
let mut file = OpenOptions::new()
.append(true)
.create(true)
.open(path)
.with_context(|| format!("Failed to create file {path_clone}"))?;
file.write_all(genome_string.as_bytes())
.with_context(|| format!("Failed to write to {path_clone}"))?;
} else {
writeln!(io::stdout(), "{genome_string}")?;
}
}
false => {
let genome_string = serde_json::to_string_pretty(&genome)?;
let output = args.get_output();
if let Some(path) = output {
let path_clone = path.clone();
let mut file = OpenOptions::new()
.append(true)
.create(true)
.open(path)
.with_context(|| format!("Failed to create file {path_clone}"))?;
file.write_all(genome_string.as_bytes())
.with_context(|| format!("Failed to write to {path_clone}"))?;
} else {
writeln!(io::stdout(), "{genome_string}")?;
}
}
let genome_card: GenomeMetadata = response.into_json()?;

let genome_string = if raw {
serde_json::to_string(&genome_card)?
} else {
serde_json::to_string_pretty(&genome_card)?
};

let output = args.get_output();
if let Some(path) = output {
let mut file = OpenOptions::new()
.append(true)
.create(true)
.open(&path)
.with_context(|| format!("Failed to create file {}", path))?;
writeln!(file, "{}", genome_string)
.with_context(|| format!("Failed to write to {}", path))?;
} else {
writeln!(io::stdout(), "{}", genome_string)?;
}
}

Ok(())
Expand All @@ -249,52 +231,33 @@ pub fn get_genome_card(args: GenomeArgs) -> Result<()> {
for accession in genome_api {
let request_url = accession.request(GenomeRequestType::Card);

let response = match agent.get(&request_url).call() {
Ok(r) => r,
Err(ureq::Error::Status(code, _)) => {
bail!("The server returned an unexpected status code ({})", code);
}
Err(_) => {
bail!("There was an error making the request or receiving the response");
let response = agent.get(&request_url).call().map_err(|e| match e {
ureq::Error::Status(code, _) => {
anyhow!("The server returned an unexpected status code ({})", code)
}
};
_ => anyhow!("There was an error making the request or receiving the response."),
})?;

let genome: GenomeCard = response.into_json()?;

match raw {
true => {
let genome_string = serde_json::to_string(&genome)?;
let output = args.get_output();
if let Some(path) = output {
let path_clone = path.clone();
let mut file = OpenOptions::new()
.append(true)
.create(true)
.open(path)
.with_context(|| format!("Failed to create file {path_clone}"))?;
file.write_all(genome_string.as_bytes())
.with_context(|| format!("Failed to write to {path_clone}"))?;
} else {
writeln!(io::stdout(), "{genome_string}")?;
}
}
false => {
let genome_string = serde_json::to_string_pretty(&genome)?;
let output = args.get_output();
if let Some(path) = output {
let path_clone = path.clone();
let mut file = OpenOptions::new()
.append(true)
.create(true)
.open(path)
.with_context(|| format!("Failed to create file {path_clone}"))?;
file.write_all(genome_string.as_bytes())
.with_context(|| format!("Failed to write to {path_clone}"))?;
} else {
writeln!(io::stdout(), "{genome_string}")?;
}
}
let genome_card: GenomeCard = response.into_json()?;

let genome_string = if raw {
serde_json::to_string(&genome_card)?
} else {
serde_json::to_string_pretty(&genome_card)?
};

let output = args.get_output();
if let Some(path) = output {
let mut file = OpenOptions::new()
.append(true)
.create(true)
.open(&path)
.with_context(|| format!("Failed to create file {}", path))?;
writeln!(file, "{}", genome_string)
.with_context(|| format!("Failed to write to {}", path))?;
} else {
writeln!(io::stdout(), "{}", genome_string)?;
}
}

Ok(())
Expand All @@ -313,52 +276,33 @@ pub fn get_genome_taxon_history(args: GenomeArgs) -> Result<()> {
for accession in genome_api {
let request_url = accession.request(GenomeRequestType::TaxonHistory);

let response = match agent.get(&request_url).call() {
Ok(r) => r,
Err(ureq::Error::Status(code, _)) => {
bail!("The server returned an unexpected status code ({})", code);
}
Err(_) => {
bail!("There was an error making the request or receiving the response.");
let response = agent.get(&request_url).call().map_err(|e| match e {
ureq::Error::Status(code, _) => {
anyhow!("The server returned an unexpected status code ({})", code)
}
};
_ => anyhow!("There was an error making the request or receiving the response."),
})?;

let genome: GenomeTaxonHistory = response.into_json()?;

match raw {
true => {
let genome_string = serde_json::to_string(&genome)?;
let output = args.get_output();
if let Some(path) = output {
let path_clone = path.clone();
let mut file = OpenOptions::new()
.append(true)
.create(true)
.open(path)
.with_context(|| format!("Failed to create file {path_clone}"))?;
file.write_all(genome_string.as_bytes())
.with_context(|| format!("Failed to write to {path_clone}"))?;
} else {
writeln!(io::stdout(), "{genome_string}")?;
}
}
false => {
let genome_string = serde_json::to_string_pretty(&genome)?;
let output = args.get_output();
if let Some(path) = output {
let path_clone = path.clone();
let mut file = OpenOptions::new()
.append(true)
.create(true)
.open(path)
.with_context(|| format!("Failed to create file {path_clone}"))?;
file.write_all(genome_string.as_bytes())
.with_context(|| format!("Failed to write to {path_clone}"))?;
} else {
writeln!(io::stdout(), "{genome_string}")?;
}
}
let genome_string = if raw {
serde_json::to_string(&genome)?
} else {
serde_json::to_string_pretty(&genome)?
};

let output = args.get_output();
if let Some(path) = output {
let mut file = OpenOptions::new()
.append(true)
.create(true)
.open(&path)
.with_context(|| format!("Failed to create file {}", path))?;
writeln!(file, "{}", genome_string)
.with_context(|| format!("Failed to write to {}", path))?;
} else {
writeln!(io::stdout(), "{}", genome_string)?;
}
}

Ok(())
Expand Down
Loading

0 comments on commit 88ac028

Please sign in to comment.