|
| 1 | +use crate::multipart_related_test_builder::*; |
| 2 | +pub struct Cli<T: CliConfig> { |
| 3 | + client: Client, |
| 4 | + config: T, |
| 5 | +} |
| 6 | + |
| 7 | +impl<T: CliConfig> Cli<T> { |
| 8 | + pub fn new(client: Client, config: T) -> Self { |
| 9 | + Self { client, config } |
| 10 | + } |
| 11 | + |
| 12 | + pub fn get_command(cmd: CliCommand) -> ::clap::Command { |
| 13 | + match cmd { |
| 14 | + CliCommand::UploadFile => Self::cli_upload_file(), |
| 15 | + CliCommand::UploadSimple => Self::cli_upload_simple(), |
| 16 | + CliCommand::UploadMultipleFiles => Self::cli_upload_multiple_files(), |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + pub fn cli_upload_file() -> ::clap::Command { |
| 21 | + ::clap::Command::new("") |
| 22 | + .arg( |
| 23 | + ::clap::Arg::new("file-content-type") |
| 24 | + .long("file-content-type") |
| 25 | + .value_parser(::clap::value_parser!(::std::string::String)) |
| 26 | + .required_unless_present("json-body") |
| 27 | + .help("MIME type for the file field"), |
| 28 | + ) |
| 29 | + .arg( |
| 30 | + ::clap::Arg::new("json-body") |
| 31 | + .long("json-body") |
| 32 | + .value_name("JSON-FILE") |
| 33 | + .required(true) |
| 34 | + .value_parser(::clap::value_parser!(std::path::PathBuf)) |
| 35 | + .help("Path to a file that contains the full json body."), |
| 36 | + ) |
| 37 | + .arg( |
| 38 | + ::clap::Arg::new("json-body-template") |
| 39 | + .long("json-body-template") |
| 40 | + .action(::clap::ArgAction::SetTrue) |
| 41 | + .help("XXX"), |
| 42 | + ) |
| 43 | + .about("Upload a file with metadata using multipart/related") |
| 44 | + .long_about("Uploads a file along with JSON metadata in a multipart/related request") |
| 45 | + } |
| 46 | + |
| 47 | + pub fn cli_upload_simple() -> ::clap::Command { |
| 48 | + ::clap::Command::new("").about("Simple upload using multipart/related") |
| 49 | + } |
| 50 | + |
| 51 | + pub fn cli_upload_multiple_files() -> ::clap::Command { |
| 52 | + ::clap::Command::new("") |
| 53 | + .arg( |
| 54 | + ::clap::Arg::new("attachment-content-type") |
| 55 | + .long("attachment-content-type") |
| 56 | + .value_parser(::clap::value_parser!(::std::string::String)) |
| 57 | + .required(false) |
| 58 | + .help("MIME type for the attachment field"), |
| 59 | + ) |
| 60 | + .arg( |
| 61 | + ::clap::Arg::new("document-content-type") |
| 62 | + .long("document-content-type") |
| 63 | + .value_parser(::clap::value_parser!(::std::string::String)) |
| 64 | + .required_unless_present("json-body") |
| 65 | + .help("MIME type for the document field"), |
| 66 | + ) |
| 67 | + .arg( |
| 68 | + ::clap::Arg::new("thumbnail-content-type") |
| 69 | + .long("thumbnail-content-type") |
| 70 | + .value_parser(::clap::value_parser!(::std::string::String)) |
| 71 | + .required_unless_present("json-body") |
| 72 | + .help("MIME type for the thumbnail field"), |
| 73 | + ) |
| 74 | + .arg( |
| 75 | + ::clap::Arg::new("json-body") |
| 76 | + .long("json-body") |
| 77 | + .value_name("JSON-FILE") |
| 78 | + .required(true) |
| 79 | + .value_parser(::clap::value_parser!(std::path::PathBuf)) |
| 80 | + .help("Path to a file that contains the full json body."), |
| 81 | + ) |
| 82 | + .arg( |
| 83 | + ::clap::Arg::new("json-body-template") |
| 84 | + .long("json-body-template") |
| 85 | + .action(::clap::ArgAction::SetTrue) |
| 86 | + .help("XXX"), |
| 87 | + ) |
| 88 | + .about("Upload multiple files with metadata using multipart/related") |
| 89 | + .long_about( |
| 90 | + "Uploads multiple files along with JSON metadata in a single multipart/related \ |
| 91 | + request", |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + pub async fn execute( |
| 96 | + &self, |
| 97 | + cmd: CliCommand, |
| 98 | + matches: &::clap::ArgMatches, |
| 99 | + ) -> anyhow::Result<()> { |
| 100 | + match cmd { |
| 101 | + CliCommand::UploadFile => self.execute_upload_file(matches).await, |
| 102 | + CliCommand::UploadSimple => self.execute_upload_simple(matches).await, |
| 103 | + CliCommand::UploadMultipleFiles => self.execute_upload_multiple_files(matches).await, |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + pub async fn execute_upload_file(&self, matches: &::clap::ArgMatches) -> anyhow::Result<()> { |
| 108 | + let mut request = self.client.upload_file(); |
| 109 | + if let Some(value) = matches.get_one::<::std::string::String>("file-content-type") { |
| 110 | + request = request.body_map(|body| body.file_content_type(value.clone())) |
| 111 | + } |
| 112 | + |
| 113 | + if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") { |
| 114 | + let body_txt = std::fs::read_to_string(value).unwrap(); |
| 115 | + let body_value = |
| 116 | + serde_json::from_str::<types::UploadFileMultipartParts>(&body_txt).unwrap(); |
| 117 | + request = request.body(body_value); |
| 118 | + } |
| 119 | + |
| 120 | + self.config.execute_upload_file(matches, &mut request)?; |
| 121 | + let result = request.send().await; |
| 122 | + match result { |
| 123 | + Ok(r) => { |
| 124 | + self.config.success_item(&r); |
| 125 | + Ok(()) |
| 126 | + } |
| 127 | + Err(r) => { |
| 128 | + self.config.error(&r); |
| 129 | + Err(anyhow::Error::new(r)) |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + pub async fn execute_upload_simple(&self, matches: &::clap::ArgMatches) -> anyhow::Result<()> { |
| 135 | + let mut request = self.client.upload_simple(); |
| 136 | + self.config.execute_upload_simple(matches, &mut request)?; |
| 137 | + let result = request.send().await; |
| 138 | + match result { |
| 139 | + Ok(r) => { |
| 140 | + self.config.success_no_item(&r); |
| 141 | + Ok(()) |
| 142 | + } |
| 143 | + Err(r) => { |
| 144 | + self.config.error(&r); |
| 145 | + Err(anyhow::Error::new(r)) |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + pub async fn execute_upload_multiple_files( |
| 151 | + &self, |
| 152 | + matches: &::clap::ArgMatches, |
| 153 | + ) -> anyhow::Result<()> { |
| 154 | + let mut request = self.client.upload_multiple_files(); |
| 155 | + if let Some(value) = matches.get_one::<::std::string::String>("attachment-content-type") { |
| 156 | + request = request.body_map(|body| body.attachment_content_type(value.clone())) |
| 157 | + } |
| 158 | + |
| 159 | + if let Some(value) = matches.get_one::<::std::string::String>("document-content-type") { |
| 160 | + request = request.body_map(|body| body.document_content_type(value.clone())) |
| 161 | + } |
| 162 | + |
| 163 | + if let Some(value) = matches.get_one::<::std::string::String>("thumbnail-content-type") { |
| 164 | + request = request.body_map(|body| body.thumbnail_content_type(value.clone())) |
| 165 | + } |
| 166 | + |
| 167 | + if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") { |
| 168 | + let body_txt = std::fs::read_to_string(value).unwrap(); |
| 169 | + let body_value = |
| 170 | + serde_json::from_str::<types::UploadMultipleFilesMultipartParts>(&body_txt) |
| 171 | + .unwrap(); |
| 172 | + request = request.body(body_value); |
| 173 | + } |
| 174 | + |
| 175 | + self.config |
| 176 | + .execute_upload_multiple_files(matches, &mut request)?; |
| 177 | + let result = request.send().await; |
| 178 | + match result { |
| 179 | + Ok(r) => { |
| 180 | + self.config.success_item(&r); |
| 181 | + Ok(()) |
| 182 | + } |
| 183 | + Err(r) => { |
| 184 | + self.config.error(&r); |
| 185 | + Err(anyhow::Error::new(r)) |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +pub trait CliConfig { |
| 192 | + fn success_item<T>(&self, value: &ResponseValue<T>) |
| 193 | + where |
| 194 | + T: std::clone::Clone + schemars::JsonSchema + serde::Serialize + std::fmt::Debug; |
| 195 | + fn success_no_item(&self, value: &ResponseValue<()>); |
| 196 | + fn error<T>(&self, value: &Error<T>) |
| 197 | + where |
| 198 | + T: std::clone::Clone + schemars::JsonSchema + serde::Serialize + std::fmt::Debug; |
| 199 | + fn list_start<T>(&self) |
| 200 | + where |
| 201 | + T: std::clone::Clone + schemars::JsonSchema + serde::Serialize + std::fmt::Debug; |
| 202 | + fn list_item<T>(&self, value: &T) |
| 203 | + where |
| 204 | + T: std::clone::Clone + schemars::JsonSchema + serde::Serialize + std::fmt::Debug; |
| 205 | + fn list_end_success<T>(&self) |
| 206 | + where |
| 207 | + T: std::clone::Clone + schemars::JsonSchema + serde::Serialize + std::fmt::Debug; |
| 208 | + fn list_end_error<T>(&self, value: &Error<T>) |
| 209 | + where |
| 210 | + T: std::clone::Clone + schemars::JsonSchema + serde::Serialize + std::fmt::Debug; |
| 211 | + fn execute_upload_file( |
| 212 | + &self, |
| 213 | + matches: &::clap::ArgMatches, |
| 214 | + request: &mut builder::UploadFile, |
| 215 | + ) -> anyhow::Result<()> { |
| 216 | + Ok(()) |
| 217 | + } |
| 218 | + |
| 219 | + fn execute_upload_simple( |
| 220 | + &self, |
| 221 | + matches: &::clap::ArgMatches, |
| 222 | + request: &mut builder::UploadSimple, |
| 223 | + ) -> anyhow::Result<()> { |
| 224 | + Ok(()) |
| 225 | + } |
| 226 | + |
| 227 | + fn execute_upload_multiple_files( |
| 228 | + &self, |
| 229 | + matches: &::clap::ArgMatches, |
| 230 | + request: &mut builder::UploadMultipleFiles, |
| 231 | + ) -> anyhow::Result<()> { |
| 232 | + Ok(()) |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +#[derive(Copy, Clone, Debug)] |
| 237 | +pub enum CliCommand { |
| 238 | + UploadFile, |
| 239 | + UploadSimple, |
| 240 | + UploadMultipleFiles, |
| 241 | +} |
| 242 | + |
| 243 | +impl CliCommand { |
| 244 | + pub fn iter() -> impl Iterator<Item = CliCommand> { |
| 245 | + vec![ |
| 246 | + CliCommand::UploadFile, |
| 247 | + CliCommand::UploadSimple, |
| 248 | + CliCommand::UploadMultipleFiles, |
| 249 | + ] |
| 250 | + .into_iter() |
| 251 | + } |
| 252 | +} |
0 commit comments