From 3055df0f14e2bae32888a040eb8178d61f202124 Mon Sep 17 00:00:00 2001 From: Douman Date: Sat, 25 Nov 2017 15:37:19 +0300 Subject: [PATCH] Adapt to new upload API in egg_mode --- src/api/twitter.rs | 4 +-- src/cli.rs | 2 +- src/main.rs | 66 ++++++++++++++++++++++++++++++---------------- 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/src/api/twitter.rs b/src/api/twitter.rs index ce551d6..e4e11a9 100644 --- a/src/api/twitter.rs +++ b/src/api/twitter.rs @@ -42,8 +42,8 @@ impl Client { } ///Uploads image to twitter. - pub fn upload_image(&self, image: &Image) -> FutureResponse { - media::upload_image(&image.content, &self.token, &self.handle) + pub fn upload_image<'a>(&self, image: &'a Image) -> media::UploadFuture<'a> { + media::UploadBuilder::new(&image.content[..], image.mime.clone()).call(&self.token, &self.handle) } ///Posts new tweet. diff --git a/src/cli.rs b/src/cli.rs index 259b28c..db37cac 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -92,7 +92,7 @@ impl Commands { } } -type Flags = Platforms; +pub type Flags = Platforms; impl Flags { fn from_matches(matches: &ArgMatches<'static>) -> Option { diff --git a/src/main.rs b/src/main.rs index 6725eed..76cf821 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,32 +23,52 @@ mod config; mod cli; mod api; -fn run() -> Result { - let config = config::Config::from_file(&utils::get_config())?; - let args = cli::Args::new(config.platforms)?; - - let mut tokio_core = Core::new().map_err(error_formatter!("Unable to create tokio's event loop."))?; +fn init_http() -> Result<(Core, api::http::HttpClient), String> { + let tokio_core = Core::new().map_err(error_formatter!("Unable to create tokio's event loop."))?; let http = api::http::create_client(&tokio_core.handle()); - let gab = match args.flags.gab { - true => Some(api::gab::Client::new(&http, config.gab)), + + Ok((tokio_core, http)) +} + +struct ApiConfigs { + gab: config::Gab, + twitter: config::Twitter, + minds: config::Minds +} + +type InitReturn<'a> = (Option>, Option, Option>); +fn init_api<'a>(mut tokio_core: &mut Core, http: &'a api::http::HttpClient, config: ApiConfigs, flags: cli::Flags) -> Result, String> { + let gab = match flags.gab { + true => Some(api::gab::Client::new(http, config.gab)), false => None }; - let twitter = match args.flags.twitter { + let twitter = match flags.twitter { true => Some(api::twitter::Client::new(tokio_core.handle(), config.twitter)), false => None }; - let minds = match args.flags.minds { - true => Some(api::minds::Client::new(&http, &mut tokio_core, config.minds)?), + let minds = match flags.minds { + true => Some(api::minds::Client::new(http, &mut tokio_core, config.minds)?), false => None }; + Ok((gab, twitter, minds)) +} + +fn run() -> Result { + let config = config::Config::from_file(&utils::get_config())?; + let args = cli::Args::new(config.platforms)?; + let config = ApiConfigs { + gab: config.gab, + twitter: config.twitter, + minds: config.minds, + }; + match args.command { - cli::Commands::Post(message, None) => { - //TODO: Find a better way to schedule futures. - //Boxing requires allocations and dynamic dispatch after all. - //But using Handle::spawn() has restrictions on lifetimes which needs to be worked out - //somehow + cli::Commands::Post(ref message, None) => { + let (mut tokio_core, http) = init_http()?; + let (gab, twitter, minds) = init_api(&mut tokio_core, &http, config, args.flags)?; let mut jobs: Vec>> = vec![]; + if let Some(ref gab) = gab { let gab_post = gab.post(&message, &[]).map_err(error_formatter!("Cannot post.")).then(api::gab::Client::handle_post); jobs.push(Box::new(gab_post)) @@ -64,8 +84,7 @@ fn run() -> Result { tokio_core.run(futures::future::join_all(jobs)).unwrap(); }, - cli::Commands::Post(message, Some(images)) => { - let mut jobs: Vec>> = vec![]; + cli::Commands::Post(ref message, Some(ref images)) => { let images = { let mut result = vec![]; for image in images { @@ -74,9 +93,13 @@ fn run() -> Result { result }; + let (mut tokio_core, http) = init_http()?; + let (gab, twitter, minds) = init_api(&mut tokio_core, &http, config, args.flags)?; + let mut jobs: Vec>> = vec![]; + if let Some(gab) = gab.as_ref() { let mut gab_images: Vec<_> = vec![]; - for image in images.iter() { + for image in &images { gab_images.push(gab.upload_image(&image).map_err(error_formatter!("Cannot upload image.")) .and_then(handle_bad_hyper_response!("Cannot upload image.")) .and_then(|response| response.body().concat2().map_err(error_formatter!("Cannot read image upload's response"))) @@ -84,19 +107,17 @@ fn run() -> Result { .map(|response: api::gab::payload::UploadResponse| response.id)); } - let message = message.as_str(); let gab_post = futures::future::join_all(gab_images).and_then(move |images| gab.post(&message, &images).map_err(error_formatter!("Cannot post."))) .then(api::gab::Client::handle_post); jobs.push(Box::new(gab_post)) } if let Some(twitter) = twitter.as_ref() { let mut tweet_images: Vec<_> = vec![]; - for image in images.iter() { + for image in &images { tweet_images.push(twitter.upload_image(&image).map_err(error_formatter!("Cannot upload image.")) - .map(|rsp| rsp.response.id)); + .map(|rsp| rsp.media_id)); } - let message = message.as_str(); let tweet = futures::future::join_all(tweet_images) .and_then(move |images| twitter.post(&message, &images).map_err(error_formatter!("Cannot tweet."))) .then(api::twitter::Client::handle_post); @@ -123,7 +144,6 @@ fn run() -> Result { } }; - Ok(0) }