-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from ToBinio/add_sparse_http_example
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use std::io; | ||
Check warning on line 1 in examples/sparse_http_reqwest.rs GitHub Actions / Test (beta)
|
||
use crates_index::{Crate, Error, SparseIndex}; | ||
Check warning on line 2 in examples/sparse_http_reqwest.rs GitHub Actions / Test (beta)
|
||
|
||
/// | ||
/// **important**:<br> | ||
/// dont forget to enable the **["blocking", "gzip"]** feature of **reqwest** | ||
/// | ||
/// command to run:<br> | ||
/// cargo run --example sparse_http_reqwest -F sparse-http | ||
/// | ||
|
||
const CRATE_TO_FETCH: &str = "names"; | ||
|
||
fn main() { | ||
let mut index = SparseIndex::new_cargo_default().unwrap(); | ||
|
||
print_crate(&mut index); | ||
update(&mut index); | ||
print_crate(&mut index); | ||
} | ||
|
||
fn print_crate(index: &mut SparseIndex){ | ||
match index.crate_from_cache(CRATE_TO_FETCH) { | ||
Ok(krate) => { | ||
println!("{:?}", krate.highest_normal_version().unwrap().version()); | ||
} | ||
Err(_err) => { | ||
println!("could not find crate {}",CRATE_TO_FETCH) | ||
} | ||
} | ||
} | ||
|
||
fn update(index: &mut SparseIndex){ | ||
let req = index.make_cache_request(CRATE_TO_FETCH).unwrap().body(()).unwrap(); | ||
|
||
let (parts, _) = req.into_parts(); | ||
let req = http::Request::from_parts(parts, vec![]); | ||
|
||
let req: reqwest::blocking::Request = req.try_into().unwrap(); | ||
|
||
let client = reqwest::blocking::ClientBuilder::new().gzip(true).build().unwrap(); | ||
|
||
let res = client.execute(req).unwrap(); | ||
|
||
let mut builder = http::Response::builder() | ||
.status(res.status()) | ||
.version(res.version()); | ||
|
||
builder | ||
.headers_mut() | ||
.unwrap() | ||
.extend(res.headers().iter().map(|(k, v)| (k.clone(), v.clone()))); | ||
|
||
let body = res.bytes().unwrap(); | ||
let res = builder.body(body.to_vec()).unwrap(); | ||
|
||
index.parse_cache_response(CRATE_TO_FETCH, res, true).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use std::io; | ||
use crates_index::{Crate, Error, SparseIndex}; | ||
Check warning on line 2 in examples/sparse_http_ureq.rs GitHub Actions / Test (beta)
|
||
|
||
/// | ||
/// **important**:<br> | ||
/// dont forget to enable the **http-interop** feature of **ureq** | ||
/// | ||
/// command to run:<br> | ||
/// cargo run --example sparse_http_ureq -F sparse-http | ||
/// | ||
|
||
const CRATE_TO_FETCH: &str = "inferno"; | ||
|
||
fn main() { | ||
let mut index = SparseIndex::new_cargo_default().unwrap(); | ||
|
||
print_crate(&mut index); | ||
update(&mut index); | ||
print_crate(&mut index); | ||
} | ||
|
||
fn print_crate(index: &mut SparseIndex){ | ||
match index.crate_from_cache(CRATE_TO_FETCH) { | ||
Ok(krate) => { | ||
println!("{:?}", krate.highest_normal_version().unwrap().version()); | ||
} | ||
Err(_err) => { | ||
println!("could not find crate {}",CRATE_TO_FETCH) | ||
} | ||
} | ||
} | ||
|
||
fn update(index: &mut SparseIndex){ | ||
let request: ureq::Request = index.make_cache_request(CRATE_TO_FETCH).unwrap().into(); | ||
|
||
let response: http::Response<String> = request | ||
.call() | ||
.map_err(|_e| io::Error::new(io::ErrorKind::InvalidInput, "connection error")).unwrap() | ||
.into(); | ||
|
||
let (parts, body) = response.into_parts(); | ||
let response = http::Response::from_parts(parts, body.into_bytes()); | ||
|
||
index.parse_cache_response(CRATE_TO_FETCH, response, true).unwrap(); | ||
} |