Skip to content

Commit d32c92f

Browse files
committed
Spawn sync implementations
1 parent 4029f66 commit d32c92f

File tree

4 files changed

+36
-25
lines changed

4 files changed

+36
-25
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rust-s3"
3-
version = "0.18.2-beta"
3+
version = "0.18.3"
44
authors = ["Drazen Urch", "Nick Stevens"]
55
description = "Tiny Rust library for working with Amazon S3 and compatible APIs"
66
repository = "https://github.com/durch/rust-s3"
@@ -28,6 +28,7 @@ url = "^2.1.0"
2828
rust-ini = "^0.13"
2929
dirs = "^2.0.2"
3030
futures = "^0.1.28"
31+
tokio = "^0.1.22"
3132

3233
[features]
3334
no-verify-ssl = []

src/bucket.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,12 @@ impl Bucket {
199199
location_result.region.parse()?
200200
}
201201
Err(e) => {
202-
if e.to_string() == "missing field `$value`" {
203-
"us-east-1".parse()?
202+
if result.1 == 200 {
203+
Region::Custom { region: "Custom".to_string(), endpoint: "".to_string() }
204204
} else {
205-
panic!("How did we get here?")
205+
Region::Custom {region: format!("Error encountered : {}", e), endpoint: "".to_string()}
206206
}
207+
207208
}
208209
};
209210
Ok((region, result.1))
@@ -373,10 +374,10 @@ impl Bucket {
373374
}
374375

375376
pub fn list_page(&self,
376-
prefix: String,
377-
delimiter: Option<String>,
378-
continuation_token: Option<String>)
379-
-> S3Result<(ListBucketResult, u16)> {
377+
prefix: String,
378+
delimiter: Option<String>,
379+
continuation_token: Option<String>)
380+
-> S3Result<(ListBucketResult, u16)> {
380381
let command = Command::ListBucket {
381382
prefix,
382383
delimiter,
@@ -390,10 +391,10 @@ impl Bucket {
390391
}
391392

392393
pub fn list_page_async(&self,
393-
prefix: String,
394-
delimiter: Option<String>,
395-
continuation_token: Option<String>)
396-
-> impl Future<Item=(ListBucketResult, u16), Error=S3Error> + Send {
394+
prefix: String,
395+
delimiter: Option<String>,
396+
continuation_token: Option<String>)
397+
-> impl Future<Item=(ListBucketResult, u16), Error=S3Error> + Send {
397398
let command = Command::ListBucket {
398399
prefix,
399400
delimiter,

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extern crate ini;
1313
extern crate dirs;
1414
extern crate futures;
1515
extern crate core;
16+
extern crate tokio;
1617

1718
pub mod bucket;
1819
pub mod credentials;

src/request.rs

+21-13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use sha2::{Digest, Sha256};
1414
use url::Url;
1515

1616
use futures::prelude::*;
17+
use tokio::runtime::current_thread::Runtime;
1718

1819
//use serde_types::AwsError;
1920
use signing;
@@ -22,6 +23,7 @@ use EMPTY_PAYLOAD_SHA;
2223
use LONG_DATE;
2324
use reqwest::async::Response;
2425
use error::{S3Error, S3Result, err};
26+
use std::error::Error;
2527

2628
/// Collection of HTTP headers sent to S3 service, in key/value format.
2729
pub type Headers = HashMap<String, String>;
@@ -223,17 +225,24 @@ impl<'a> Request<'a> {
223225
}
224226

225227
pub fn response_data(&self) -> S3Result<(Vec<u8>, u16)> {
226-
match self.response_data_future().wait() {
227-
Ok((response_data, status_code)) => Ok((response_data, status_code)),
228-
Err(_) => Err(S3Error { src: Some("Error getting response".to_string())})
229-
}
228+
let response_data = self.response_data_future().then(|result|
229+
match result {
230+
Ok((response_data, status_code)) => Ok((response_data, status_code)),
231+
Err(e) => Err(e)
232+
});
233+
let mut runtime = Runtime::new().unwrap();
234+
runtime.block_on(response_data)
230235
}
231236

232237
pub fn response_data_to_writer<T: Write>(&self, writer: &mut T) -> S3Result<u16> {
233-
match self.response_data_to_writer_future(writer).wait() {
234-
Ok(status_code) => Ok(status_code),
235-
Err(_) => Err(err("ReqwestFuture"))
236-
}
238+
let status_code_future = self.response_data_to_writer_future(writer).then(|result| {
239+
match result {
240+
Ok(status_code) => Ok(status_code),
241+
Err(_) => Err(err("ReqwestFuture"))
242+
}
243+
});
244+
let mut runtime = Runtime::new().unwrap();
245+
runtime.block_on(status_code_future)
237246
}
238247

239248
pub fn response_future(&self) -> impl Future<Item=Response, Error=S3Error> {
@@ -263,14 +272,14 @@ impl<'a> Request<'a> {
263272
.headers(headers.to_owned())
264273
.body(content.to_owned());
265274

266-
request.send().map_err(|_| S3Error {src: None})
275+
request.send().map_err(|e| S3Error { src: Some(e.description().to_string()) })
267276
}
268277

269278
pub fn response_data_future(&self) -> impl Future<Item=(Vec<u8>, u16), Error=S3Error> {
270279
self.response_future()
271-
.and_then(|mut response| Ok((response.text(), response.status().as_u16()))).map_err(|_| S3Error {src: None})
280+
.and_then(|mut response| Ok((response.text(), response.status().as_u16())))
272281
.and_then(|(body_future, status_code)| {
273-
body_future.and_then(move |body| Ok((body.as_bytes().to_vec(), status_code))).map_err(|_| S3Error {src: None})
282+
body_future.and_then(move |body| Ok((body.as_bytes().to_vec(), status_code))).map_err(|e| S3Error { src: Some(e.description().to_string()) })
274283
})
275284
}
276285

@@ -281,15 +290,14 @@ impl<'a> Request<'a> {
281290
Ok(status_code)
282291
})
283292
}
284-
285293
}
286294

287295
#[cfg(test)]
288296
mod tests {
289297
use bucket::Bucket;
290298
use command::Command;
291299
use credentials::Credentials;
292-
use request::{Request};
300+
use request::Request;
293301
use error::S3Result;
294302

295303
// Fake keys - otherwise using Credentials::default will use actual user

0 commit comments

Comments
 (0)