Skip to content

Commit

Permalink
Update to tokio version 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RAnders00 authored and adwhit committed Feb 14, 2021
1 parent 259a12e commit 6a11c89
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Pending

### Changed
- Updated tokio to version 1.0
- The `direct` module has been overhauled to switch it over to the DM Events API
- `sent` and `received` have been merged into `list`, to match the endpoint
- `send` has been changed into the `DraftMessage` type, to reflect the new capabilities
Expand Down
15 changes: 8 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ readme = "README.md"
edition = "2018"

[dependencies]
base64 = "0.12"
base64 = "0.13"
chrono = { version = "0.4", features = ["serde"] }
futures = "0.3"
derive_more = "0.99"
hmac = "0.8"
hyper = "0.13"
hyper-rustls = { version = "0.20", optional = true, default-features = false }
hyper-tls = { version = "0.4", optional = true }
hmac = "0.10"
hyper = { version = "0.14", features = ["http1", "http2", "client", "stream"] }
hyper-rustls = { version = "0.22", optional = true, default-features = false }
hyper-tls = { version = "0.5", optional = true }
lazy_static = "1.4"
native-tls = { version = "0.2", optional = true }
mime = "0.3"
percent-encoding = "2.1"
rand = "0.7"
rand = "0.8"
regex = "1.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
sha-1 = "0.9"
thiserror = "1.0.11"
tokio = { version = "0.2.8", features = ["time", "rt-core", "macros"] }
tokio = { version = "1.0", features = ["time"] }
url = "2.1.1"

[features]
Expand All @@ -42,3 +42,4 @@ rustls_webpki = ["hyper-rustls", "hyper-rustls/webpki-tokio"]
[dev-dependencies]
yansi = "0.5.0"
structopt = "0.3.13"
tokio = { version = "1.0", features = ["rt", "rt-multi-thread", "macros"] }
4 changes: 2 additions & 2 deletions examples/create_tweet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::path::PathBuf;
use std::time::Duration;

use structopt::StructOpt;
use tokio::time::delay_for;
use tokio::time::sleep;

#[derive(StructOpt)]
/// A simple CLI for uploading a tweet, optionally with media attched
Expand Down Expand Up @@ -62,7 +62,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Some(ProgressInfo::Pending(_)) | Some(ProgressInfo::InProgress(_)) => {
print!(".");
stdout().flush()?;
delay_for(Duration::from_secs(1)).await;
sleep(Duration::from_secs(1)).await;
}
Some(ProgressInfo::Failed(err)) => Err(err)?,
}
Expand Down
1 change: 1 addition & 0 deletions src/auth/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ impl OAuthParams {
let mut rng = rand::thread_rng();
let nonce = ::std::iter::repeat(())
.map(|()| rng.sample(rand::distributions::Alphanumeric))
.map(char::from)
.take(32)
.collect::<String>();
OAuthParams {
Expand Down
39 changes: 32 additions & 7 deletions src/common/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
use crate::error::Error::{self, *};
use crate::error::{Result, TwitterErrors};

use hyper::client::ResponseFuture;
use hyper::client::{ResponseFuture, HttpConnector};
use hyper::{self, Body, Request};
#[cfg(feature = "hyper-rustls")]
use hyper_rustls::HttpsConnector;
#[cfg(feature = "native_tls")]
use hyper_tls::HttpsConnector;
use serde::{de::DeserializeOwned, Deserialize};
use serde_json;

Expand Down Expand Up @@ -148,10 +144,39 @@ impl<T: Iterator> Iterator for ResponseIter<T> {
}
}

#[cfg(not(any(feature = "native_tls", feature = "rustls", feature = "rustls_webpki")))]
compile_error!("Crate `egg_mode` must be compiled with exactly one of the three \
feature flags `native_tls`, `rustls` or `rustls_webpki` enabled, you attempted to \
compile `egg_mode` with none of them enabled");

#[cfg(any(
all(feature = "native_tls", any(feature = "rustls", feature = "rustls_webpki")),
all(feature = "rustls", any(feature = "native_tls", feature = "rustls_webpki")),
all(feature = "rustls_webpki", any(feature = "native_tls", feature = "rustls")),
))]
compile_error!("features `egg_mode/native_tls`, `egg_mode/rustls` and \
`egg_mode/rustls_webpki` are mutually exclusive, you attempted to compile `egg_mode` \
with more than one of these feature flags enabled at the same time");

#[cfg(feature = "native_tls")]
fn new_https_connector() -> hyper_tls::HttpsConnector<HttpConnector> {
hyper_tls::HttpsConnector::new()
}

#[cfg(feature = "rustls")]
fn new_https_connector() -> hyper_rustls::HttpsConnector<HttpConnector> {
hyper_rustls::HttpsConnector::with_native_roots()
}

#[cfg(feature = "rustls_webpki")]
fn new_https_connector() -> hyper_rustls::HttpsConnector<HttpConnector> {
hyper_rustls::HttpsConnector::with_webpki_roots()
}

// n.b. this function is re-exported in the `raw` module - these docs are public!
/// Converts the given request into a raw `ResponseFuture` from hyper.
pub fn get_response(request: Request<Body>) -> ResponseFuture {
let connector = HttpsConnector::new();
let connector = new_https_connector();
let client = hyper::Client::builder().build(connector);
client.request(request)
}
Expand All @@ -160,7 +185,7 @@ pub fn get_response(request: Request<Body>) -> ResponseFuture {
/// Loads the given request, parses the headers and response for potential errors given by Twitter,
/// and returns the headers and raw bytes returned from the response.
pub async fn raw_request(request: Request<Body>) -> Result<(Headers, Vec<u8>)> {
let connector = HttpsConnector::new();
let connector = new_https_connector();
let client = hyper::Client::builder().build(connector);
let resp = client.request(request).await?;
let (parts, body) = resp.into_parts();
Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub enum Error {
BadStatus(hyper::StatusCode),
///The web request experienced an error. The enclosed error was returned from hyper.
#[error("Network error: {}", _0)]
NetError(#[from] hyper::error::Error),
NetError(#[from] hyper::Error),
///The `native_tls` implementation returned an error. The enclosed error was returned from
///`native_tls`.
#[cfg(feature = "native_tls")]
Expand All @@ -155,7 +155,7 @@ pub enum Error {
///The tokio `Timer` instance was shut down while waiting on a timer, for example while waiting
///for media to be processed by Twitter. The enclosed error was returned from `tokio`.
#[error("Timer runtime shutdown: {}", _0)]
TimerShutdownError(#[from] tokio::time::Error),
TimerShutdownError(#[from] tokio::time::error::Error),
///An error occurred when reading the value from a response header. The enclused error was
///returned from hyper.
///
Expand Down

0 comments on commit 6a11c89

Please sign in to comment.