1use crate::client::GoatClient;
2use crate::error::{Error, ErrorKind, Result};
3use crate::output::Format;
4use crate::report::report::{Report, ReportOptions, ReportType};
5use crate::report::table::report_table;
6use futures::StreamExt;
7use std::io::Write;
8
9pub enum ReportAction {
10 Continue,
11 PrintedAndExit,
12}
13
14pub async fn fetch_report(
16 options: &ReportOptions,
17 unique_ids: Vec<String>,
18 report_type: ReportType,
19) -> Result<ReportAction> {
20 let report = Report::new(options, report_type)?;
21 let url = report.make_url(unique_ids)?;
22
23 if options.url {
24 crate::outln!("GoaT report API URL:\t{}", url)?;
25 return Ok(ReportAction::PrintedAndExit);
26 }
27
28 let header_value = match report_type {
29 ReportType::Newick => "text/x-nh",
30 _ => "application/json",
31 };
32
33 let concurrent_requests = 1;
35
36 let url_vector_api = vec![url];
40
41 let client = GoatClient::new();
42 let fetches = futures::stream::iter(url_vector_api.into_iter().map(|path| {
43 let client = client.clone();
44 async move { client.get_text(&path, header_value).await }
45 }))
46 .buffered(concurrent_requests)
47 .collect::<Vec<_>>();
48
49 let mut awaited_fetches = fetches.await;
50
51 let report = awaited_fetches.remove(0);
52
53 match report {
54 Ok(ref s) => {
55 if s.is_empty() || s == ";" {
58 let hint = match report_type {
59 ReportType::Newick if options.no_descendents => format!(
60 " With -n, the taxa are the tips, so they must be at the rank ({}).",
61 options.rank
62 ),
63 ReportType::Newick => " Try increasing the threshold.".to_string(),
64 _ => String::new(),
65 };
66 return Err(Error::new(ErrorKind::Report(format!("no data found.{}", hint))));
67 }
68
69 if report_type == ReportType::Newick || options.format == Format::Json {
70 let mut stdout = std::io::stdout();
71 writeln!(stdout, "{}", s)?;
72 } else {
73 let response: serde_json::Value = serde_json::from_str(s)?;
74 report_table(report_type, &response)?.print(options.format)?;
75 }
76 }
77 Err(e) => return Err(e),
78 }
79
80 Ok(ReportAction::Continue)
81}