Skip to content

Commit

Permalink
fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
adwhit committed Mar 14, 2020
1 parent 1366186 commit ff5acd7
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 52 deletions.
11 changes: 7 additions & 4 deletions examples/bearer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@

mod common;

use egg_mode::error::Result;

#[tokio::main]
async fn main() {
async fn main() -> Result<()> {
let con_key = include_str!("common/consumer_key").trim();
let con_secret = include_str!("common/consumer_secret").trim();

let con_token = egg_mode::KeyPair::new(con_key, con_secret);

println!("Pulling up the bearer token...");
let token = egg_mode::bearer_token(&con_token).await.unwrap();
let token = egg_mode::bearer_token(&con_token).await?;

println!("Pulling up a user timeline...");
let timeline =
egg_mode::tweet::user_timeline("rustlang", false, true, &token).with_page_size(5);

let (_timeline, feed) = timeline.start().await.unwrap();
for tweet in feed {
let (_timeline, feed) = timeline.start().await?;
for tweet in feed.response {
println!("");
common::print_tweet(&tweet);
}
Ok(())
}
36 changes: 14 additions & 22 deletions examples/reciprocal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,28 @@

mod common;

use futures::future;
use futures::StreamExt;
use futures::TryStreamExt;

use egg_mode::error::Result;
use egg_mode::user;
use std::collections::HashSet;

// IMPORTANT: see common.rs for instructions on making
// sure this properly authenticates with Twitter.
#[tokio::main]
async fn main() {
async fn main() -> Result<()> {
let config = common::Config::load().await;

println!("");
let mut friends = HashSet::new();
let mut friends: HashSet<u64> = user::friends_ids(config.user_id, &config.token)
.map(|r| r.unwrap().response)
.await
.collect();

let mut followers = HashSet::new();
user::followers_ids(config.user_id, &config.token)
.map(|r| r.unwrap().response)
.for_each(|id| {
followers.insert(id);
future::ready(())
})
.await;
let friends: HashSet<u64> = user::friends_ids(config.user_id, &config.token)
.map_ok(|r| r.response)
.try_collect()
.await?;

let followers: HashSet<u64> = user::followers_ids(config.user_id, &config.token)
.map_ok(|r| r.response)
.try_collect()
.await?;

let reciprocals = friends
.intersection(&followers)
Expand All @@ -43,12 +38,9 @@ async fn main() {
);

if reciprocals_ct > 0 {
for user in user::lookup(reciprocals, &config.token)
.await
.unwrap()
.iter()
{
for user in user::lookup(reciprocals, &config.token).await?.iter() {
println!("{} (@{})", user.name, user.screen_name);
}
}
Ok(())
}
15 changes: 8 additions & 7 deletions examples/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

mod common;

use egg_mode::tweet;
use egg_mode::{error::Result, tweet};
use std::collections::{HashSet, VecDeque};

#[tokio::main]
async fn main() {
async fn main() -> Result<()> {
let c = common::Config::load().await;

//Thread Reconstruction
Expand Down Expand Up @@ -42,14 +42,14 @@ async fn main() {
let mut thread = VecDeque::with_capacity(21);
let mut thread_ids = HashSet::new();

let start_tweet = tweet::show(start_id, &c.token).await.unwrap();
let start_tweet = tweet::show(start_id, &c.token).await?;
let thread_user = start_tweet.user.as_ref().unwrap().id;
thread_ids.insert(start_tweet.id);
thread.push_front(start_tweet.response);

for _ in 0usize..10 {
if let Some(id) = thread.front().and_then(|t| t.in_reply_to_status_id) {
let parent = tweet::show(id, &c.token).await.unwrap();
let parent = tweet::show(id, &c.token).await?;
thread_ids.insert(parent.id);
thread.push_front(parent.response);
} else {
Expand All @@ -61,15 +61,15 @@ async fn main() {

for tweet in replies
.call(Some(start_id), None)
.await
.unwrap()
.await?
.response
.into_iter()
.rev()
{
if let Some(reply_id) = tweet.in_reply_to_status_id {
if thread_ids.contains(&reply_id) {
thread_ids.insert(tweet.id);
thread.push_back(tweet.response);
thread.push_back(tweet);
}
}

Expand All @@ -85,4 +85,5 @@ async fn main() {
}
common::print_tweet(&tweet);
}
Ok(())
}
21 changes: 11 additions & 10 deletions examples/tweets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@

mod common;

use egg_mode::error::Result;

#[tokio::main]
async fn main() {
async fn main() -> Result<()> {
let config = common::Config::load().await;
let tweet_id = 766678057788829697;

println!("");
println!("Load up an individual tweet:");
let status = egg_mode::tweet::show(tweet_id, &config.token)
.await
.unwrap();
let status = egg_mode::tweet::show(tweet_id, &config.token).await?;
common::print_tweet(&status);

println!("");
println!("Loading retweets of an individual tweet:");
for rt in &egg_mode::tweet::retweets_of(tweet_id, 5, &config.token)
.await
.unwrap()
for rt in egg_mode::tweet::retweets_of(tweet_id, 5, &config.token)
.await?
.iter()
{
if let Some(ref user) = rt.user {
println!("{} (@{})", user.name, user.screen_name);
Expand All @@ -30,7 +30,7 @@ async fn main() {
println!("");
println!("Loading the user's home timeline:");
let home = egg_mode::tweet::home_timeline(&config.token).with_page_size(5);
let (_home, feed) = home.start().await.unwrap();
let (_home, feed) = home.start().await?;
for status in feed.iter() {
common::print_tweet(&status);
println!("");
Expand All @@ -39,7 +39,7 @@ async fn main() {
println!("");
println!("Loading the user's mentions timeline:");
let mentions = egg_mode::tweet::mentions_timeline(&config.token).with_page_size(5);
let (_mentions, feed) = mentions.start().await.unwrap();
let (_mentions, feed) = mentions.start().await?;
for status in feed.iter() {
common::print_tweet(&status);
println!("");
Expand All @@ -49,9 +49,10 @@ async fn main() {
println!("Loading the user's timeline:");
let user =
egg_mode::tweet::user_timeline(config.user_id, true, true, &config.token).with_page_size(5);
let (_user, feed) = user.start().await.unwrap();
let (_user, feed) = user.start().await?;
for status in feed.iter() {
common::print_tweet(&status);
println!("");
}
Ok(())
}
2 changes: 1 addition & 1 deletion src/common/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ pub async fn make_parsed_future<T: for<'de> Deserialize<'de>>(
make_future(request, make_response).await
}

#[derive(Clone, Debug, Deserialize)]
#[derive(Copy, Clone, Debug, Deserialize)]
pub struct RateLimit {
///The rate limit ceiling for the given request.
pub rate_limit: i32,
Expand Down
16 changes: 9 additions & 7 deletions src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
//! module. The rest of it is available to make sure consumers of the API can understand precisely
//! what types come out of functions that return `CursorIter`.
use futures::Stream;
use serde::{de::DeserializeOwned, Deserialize};
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::vec::IntoIter as VecIter;

use futures::Stream;
use serde::{de::DeserializeOwned, Deserialize};

use crate::common::*;
use crate::error::Result;
Expand Down Expand Up @@ -255,7 +253,7 @@ where
///pagination.
pub next_cursor: i64,
loader: Option<FutureResponse<T>>,
iter: Option<VecIter<T::Item>>,
iter: Option<Box<dyn Iterator<Item = Response<T::Item>>>>,
}

impl<T> CursorIter<T>
Expand Down Expand Up @@ -327,7 +325,7 @@ where
T: Cursor + DeserializeOwned + 'static,
T::Item: Unpin,
{
type Item = Result<T::Item>;
type Item = Result<Response<T::Item>>;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
if let Some(mut fut) = self.loader.take() {
Expand All @@ -341,8 +339,12 @@ where
self.next_cursor = resp.next_cursor_id();

let resp = Response::map(resp, |r| r.into_inner());
let rate = resp.rate_limit_status;

let mut iter = resp.response.into_iter();
let mut iter = Box::new(resp.response.into_iter().map(move |item| Response {
rate_limit_status: rate,
response: item,
}));
let first = iter.next();
self.iter = Some(iter);

Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::{self, fmt};
use tokio;

/// Convenient alias to a Result containing a local Error type
pub(crate) type Result<T> = std::result::Result<T, Error>;
pub type Result<T> = std::result::Result<T, Error>;

///Represents a collection of errors returned from a Twitter API call.
///
Expand Down

0 comments on commit ff5acd7

Please sign in to comment.