Skip to content

Commit

Permalink
Adapt to new upload API in egg_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
DoumanAsh committed Nov 25, 2017
1 parent bc2e60a commit 3055df0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/api/twitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ impl Client {
}

///Uploads image to twitter.
pub fn upload_image(&self, image: &Image) -> FutureResponse<media::Media> {
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.
Expand Down
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl Commands {
}
}

type Flags = Platforms;
pub type Flags = Platforms;

impl Flags {
fn from_matches(matches: &ArgMatches<'static>) -> Option<Self> {
Expand Down
66 changes: 43 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,52 @@ mod config;
mod cli;
mod api;

fn run() -> Result<i32, String> {
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<api::gab::Client<'a>>, Option<api::twitter::Client>, Option<api::minds::Client<'a>>);
fn init_api<'a>(mut tokio_core: &mut Core, http: &'a api::http::HttpClient, config: ApiConfigs, flags: cli::Flags) -> Result<InitReturn<'a>, 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<i32, String> {
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<Box<Future<Item=(), Error=()>>> = 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))
Expand All @@ -64,8 +84,7 @@ fn run() -> Result<i32, String> {

tokio_core.run(futures::future::join_all(jobs)).unwrap();
},
cli::Commands::Post(message, Some(images)) => {
let mut jobs: Vec<Box<Future<Item=(), Error=()>>> = vec![];
cli::Commands::Post(ref message, Some(ref images)) => {
let images = {
let mut result = vec![];
for image in images {
Expand All @@ -74,29 +93,31 @@ fn run() -> Result<i32, String> {
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<Box<Future<Item=(), Error=()>>> = 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")))
.and_then(|body| serde_json::from_slice(&body).map_err(error_formatter!("Cannot parse image upload's response")))
.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);
Expand All @@ -123,7 +144,6 @@ fn run() -> Result<i32, String> {
}
};


Ok(0)
}

Expand Down

0 comments on commit 3055df0

Please sign in to comment.