Streaming HTTP with any package?


#1

I’ve spent 3-5 hours messing with Twitter API and now I want to use their streaming API.

So far I’ve been able to OAuth and successfully pull data, but sadly, that data is just an empty string.

I’m assuming this is because I’m not waiting long enough for new data - or rather not waiting at all!

Currently I’m using Reqwest, and would prefer if someone answered using it, but if not Hyper is acceptable too.

Thank you very much!

Here is the code I’m using:

extern crate reqwest;
extern crate oauth_client as oauth;

use std::collections::HashMap;

use oauth::{
  Token
};

use reqwest::{
  Client
};

use reqwest::header::{
  Authorization,
  Connection
};

const CONSUMER_KEY: &str = "";
const CONSUMER_SECRET: &str = "";

const ACCESS_TOKEN: &str = "";
const ACCESS_TOKEN_SECRET: &str = "";


fn main() {
  query_twitter();
}

fn query_twitter() -> () {
  let query_url = format!(
    "https://stream.twitter.com/1.1/statuses/filter.json",
  );

  let consumer_token = Some(Token::new(CONSUMER_KEY, CONSUMER_SECRET));
  let access_token   = Some(Token::new(ACCESS_TOKEN, ACCESS_TOKEN_SECRET));

  let mut req_param = HashMap::new();
  req_param.insert("track".into(), "donald".into());

  let (oauth_header, body) = oauth::authorization_header(
    "GET",
    &query_url,
    &consumer_token.unwrap(),
    Some(&access_token.unwrap()),
    Some(&req_param)
  );
  
  let mut res = Client
    ::new()
    .get(&query_url)
    .header(Authorization(oauth_header))
    .header(Connection::keep_alive())
    .body(body.clone())
    .send()
    .unwrap();

  loop {
    println!("{:?}", res.text());
  }
}

#2

I have broadened my requirements. Help! :persevere:


#3

Looks like someone has already wrapped this: https://github.com/dmizuk/twitter-stream-rs