Skip to content

Commit

Permalink
Merge pull request #131 from ToBinio/add_sparse_http_example
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski authored Jul 21, 2023
2 parents 0faf3c8 + 8042c98 commit c44bea2
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ toml = "0.7.3"
[dev-dependencies]
tempfile = "3.5.0"
cap = "0.1.2"
ureq = { version = "2.7.1", features = ["http-interop"] }
reqwest = { version = "0.11.18", features = ["blocking", "gzip"] }

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
Expand Down
58 changes: 58 additions & 0 deletions examples/sparse_http_reqwest.rs
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

View workflow job for this annotation

GitHub Actions / Test (beta)

unused import: `std::io`

Check warning on line 1 in examples/sparse_http_reqwest.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

unused import: `std::io`
use crates_index::{Crate, Error, SparseIndex};

Check warning on line 2 in examples/sparse_http_reqwest.rs

View workflow job for this annotation

GitHub Actions / Test (beta)

unused imports: `Crate`, `Error`

Check warning on line 2 in examples/sparse_http_reqwest.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

unused imports: `Crate`, `Error`

///
/// **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();
}
45 changes: 45 additions & 0 deletions examples/sparse_http_ureq.rs
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

View workflow job for this annotation

GitHub Actions / Test (beta)

unused imports: `Crate`, `Error`

Check warning on line 2 in examples/sparse_http_ureq.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

unused imports: `Crate`, `Error`

///
/// **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();

Check failure on line 34 in examples/sparse_http_ureq.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

no method named `make_cache_request` found for mutable reference `&mut SparseIndex` in the current scope

let response: http::Response<String> = request

Check failure on line 36 in examples/sparse_http_ureq.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

failed to resolve: use of undeclared crate or module `http`
.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();

Check failure on line 44 in examples/sparse_http_ureq.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

no method named `parse_cache_response` found for mutable reference `&mut SparseIndex` in the current scope
}

0 comments on commit c44bea2

Please sign in to comment.