Initial stabilization #1
1 changed files with 79 additions and 0 deletions
add: cache downloading
commit
d2e683de28
79
src/lib.rs
79
src/lib.rs
|
|
@ -168,4 +168,83 @@ impl Client {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load_cache(&self, key: &CacheKey) -> Result<Option<Box<[u8]>>> {
|
||||
let Some(cache_location) = self.get_cache_entry(key).context("getting cache entry")? else {
|
||||
// cache miss
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
let cache = self
|
||||
.download_cache(cache_location)
|
||||
.context("downloading the cache")?;
|
||||
|
||||
Ok(Some(cache))
|
||||
}
|
||||
|
||||
fn get_cache_entry(&self, key: &CacheKey) -> Result<Option<String>> {
|
||||
let uri = format!("{}_apis/artifactcache/cache/{}", self.base_url, key.0);
|
||||
let request = Request::builder()
|
||||
.uri(uri)
|
||||
.method(Method::GET)
|
||||
.body(())
|
||||
.context("building the request")?;
|
||||
let mut response = self
|
||||
.http_client
|
||||
.send(request)
|
||||
.context("sending the request")?;
|
||||
let status = response.status();
|
||||
if status == StatusCode::NO_CONTENT {
|
||||
// cache miss
|
||||
return Ok(None);
|
||||
}
|
||||
let mut body = Vec::with_capacity(100);
|
||||
response
|
||||
.body_mut()
|
||||
.read_to_end(&mut body)
|
||||
.context("reading response body")?;
|
||||
if status != StatusCode::OK {
|
||||
bail!(
|
||||
"Failed reading a cache entry (status {status}): {:?}",
|
||||
str::from_utf8(&body)
|
||||
);
|
||||
}
|
||||
|
||||
let body_val =
|
||||
serde_json::from_slice::<serde_json::Value>(&body).context("parsing response body")?;
|
||||
|
||||
let location = body_val
|
||||
.get("archiveLocation")
|
||||
.context("not cache location field")?
|
||||
.as_str()
|
||||
.context("archiveLocation is not a string")?;
|
||||
|
||||
Ok(Some(location.to_string()))
|
||||
}
|
||||
|
||||
fn download_cache(&self, location: String) -> Result<Box<[u8]>> {
|
||||
let request = Request::builder()
|
||||
.uri(location)
|
||||
.method(Method::GET)
|
||||
.body(())
|
||||
.context("building the request")?;
|
||||
let mut response = self
|
||||
.http_client
|
||||
.send(request)
|
||||
.context("sending the request")?;
|
||||
let status = response.status();
|
||||
let mut body = Vec::with_capacity(100);
|
||||
response
|
||||
.body_mut()
|
||||
.read_to_end(&mut body)
|
||||
.context("reading response body")?;
|
||||
if status == StatusCode::OK {
|
||||
bail!(
|
||||
"Failed downloading the cache (status {status}): {:?}",
|
||||
str::from_utf8(&body)
|
||||
);
|
||||
}
|
||||
|
||||
Ok(body.into_boxed_slice())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue