-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcmd_api_call.rs
102 lines (90 loc) · 3.19 KB
/
cmd_api_call.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use anyhow::Result;
use clap::Parser;
use itertools::Itertools;
/// Perform operations on CAD files.
///
/// # convert a step file to an obj file
/// $ zoo file convert ./input.step ./output.obj
#[derive(Parser, Debug, Clone)]
#[clap(verbatim_doc_comment)]
pub struct CmdApiCall {
#[clap(subcommand)]
subcmd: SubCommand,
}
#[derive(Parser, Debug, Clone)]
enum SubCommand {
Status(CmdApiCallStatus),
}
#[async_trait::async_trait(?Send)]
impl crate::cmd::Command for CmdApiCall {
async fn run(&self, ctx: &mut crate::context::Context) -> Result<()> {
match &self.subcmd {
SubCommand::Status(cmd) => cmd.run(ctx).await,
}
}
}
/// Perform operations for API calls.
///
/// # get the status of an async API call
/// $ zoo api-call status <id>
#[derive(Parser, Debug, Clone)]
#[clap(verbatim_doc_comment)]
pub struct CmdApiCallStatus {
/// The ID of the API call.
#[clap(name = "id", required = true)]
pub id: uuid::Uuid,
/// Command output format.
#[clap(long, short, value_enum)]
pub format: Option<crate::types::FormatOutput>,
}
#[async_trait::async_trait(?Send)]
impl crate::cmd::Command for CmdApiCallStatus {
async fn run(&self, ctx: &mut crate::context::Context) -> Result<()> {
let client = ctx.api_client("")?;
let api_call = client.api_calls().get_async_operation(self.id).await?;
// If it is a file conversion and there is output, we need to save that output to a file
// for them.
if let kittycad::types::AsyncApiCallOutput::FileConversion {
completed_at: _,
created_at: _,
error: _,
id: _,
output_format: _,
output_format_options: _,
outputs,
src_format: _,
src_format_options: _,
started_at: _,
status,
updated_at: _,
user_id: _,
} = &api_call
{
if *status == kittycad::types::ApiCallStatus::Completed {
if let Some(outputs) = &outputs {
let path = std::env::current_dir()?;
for (name, output) in outputs {
if output.is_empty() {
anyhow::bail!("no output was generated for the file conversion! (this is probably a bug in the API) you should report it to [email protected]");
}
let path = path.join(name);
std::fs::write(&path, &output.0)?;
}
let paths = outputs
.keys()
.map(|k| path.join(k))
.map(|p| p.to_string_lossy().to_string())
.collect_vec();
// Tell them where we saved the file.
writeln!(ctx.io.out, "Saved file conversion output(s) to: {}", paths.join(", "))?;
// Return early.
return Ok(());
}
}
}
// Print the output of the conversion.
// TODO: make this work as a table.
ctx.io.write_output(&crate::types::FormatOutput::Json, &api_call)?;
Ok(())
}
}