Initial stabilization #1

Merged
dzu merged 10 commits from stabilize-v1 into main 2026-02-20 16:23:43 +01:00
Showing only changes of commit d2e683de28 - Show all commits

add: cache downloading

Artem Diachenko 2026-02-20 08:01:37 +00:00

View file

@ -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())
}
}