use libtea_core::{
is_json_content_type, parse_artifact, parse_collection, parse_component_release,
parse_product_release, versioned_api_prefix, Artifact, Checksum, Cle, Collection, Component,
ComponentId, ComponentRelease, ComponentReleaseId, ComponentReleaseWithCollection,
DiscoveryInfo, IntegrityOutcome, Page, Product, ProductId, ProductRelease, ProductReleaseId,
ProductReleaseWithCollection,
};
use serde::de::DeserializeOwned;
use url::Url;
use crate::error::ClientError;
use crate::transport::{header_str, map_api_error, operation_url, Call, CONTENT_TYPE_HEADER};
use crate::Client;
#[derive(Clone)]
pub struct Service {
client: Client,
root: String,
version: String,
prefix: Url,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct DownloadedArtifact {
pub bytes: Vec<u8>,
pub content_type: Option<String>,
pub content_encoding: Option<String>,
pub etag: Option<String>,
pub strong_etag: bool,
pub cache_control: Option<String>,
pub content_location: Option<String>,
pub vary: Option<String>,
pub repr_digest: Option<String>,
pub final_url: String,
pub integrity: libtea_core::IntegrityReport,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ArtifactFetch {
Body(DownloadedArtifact),
#[non_exhaustive]
NotModified {
etag: Option<String>,
cache_control: Option<String>,
vary: Option<String>,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum CleFetch {
Events(Cle),
#[non_exhaustive]
NotImplemented {
message: Option<String>,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum SignatureFetch {
Present(Box<DownloadedArtifact>),
NotPublished,
#[non_exhaustive]
NotModified {
etag: Option<String>,
},
}
impl Service {
pub(crate) fn client_ref(&self) -> &Client {
&self.client
}
pub(crate) fn prefix_ref(&self) -> &Url {
&self.prefix
}
pub(crate) fn from_parts(
client: Client,
root: Url,
version: String,
) -> Result<Self, ClientError> {
let prefix = versioned_api_prefix(root.as_str().trim_end_matches('/'), &version)?;
let prefix = Url::parse(&prefix).map_err(|_| {
ClientError::Model(libtea_core::ModelError::Invalid(
"versioned API prefix is not a URL".into(),
))
})?;
Ok(Self {
client,
root: root.as_str().trim_end_matches('/').to_owned(),
version,
prefix,
})
}
#[must_use]
pub fn root(&self) -> &str {
&self.root
}
#[must_use]
pub fn api_version(&self) -> &str {
&self.version
}
pub async fn discover_identifier(
&self,
tei: Option<&str>,
purl: Option<&str>,
) -> Result<Vec<DiscoveryInfo>, ClientError> {
match (tei, purl) {
(Some(_), Some(_)) | (None, None) => {
return Err(ClientError::Model(libtea_core::ModelError::Invalid(
"exactly one of tei or purl is required".into(),
)));
}
_ => {}
}
let mut query = Vec::new();
if let Some(tei) = tei {
query.push(("tei", tei));
}
if let Some(purl) = purl {
query.push(("purl", purl));
}
let response = self.get_raw("discovery", &query, false).await?;
if response.status.as_u16() == 404 {
if let Some(error) = libtea_core::tea_error_code(
header_str(&response.headers, CONTENT_TYPE_HEADER),
&response.body,
) {
let message = libtea_core::TeaErrorBody::parse(&response.body)
.ok()
.and_then(|body| body.message);
return Err(ClientError::Unresolved {
error: libtea_core::escape_control_chars(&error),
message: message.map(|text| libtea_core::escape_control_chars(&text)),
});
}
}
if !response.status.is_success() {
return Err(map_api_error(&response));
}
require_json(&response)?;
Ok(if self.client.inner.supported.is_lenient() {
DiscoveryInfo::parse_list_lenient(&response.body)?
} else {
DiscoveryInfo::parse_list(&response.body)?
})
}
pub async fn query_products(
&self,
query: &libtea_core::ListParams<libtea_core::enums::ProductSort>,
) -> Result<Page<Product>, ClientError> {
self.page("products", query, |sort| sort.as_str()).await
}
pub async fn get_product(&self, id: ProductId) -> Result<Product, ClientError> {
self.get_json(&format!("product/{id}"), &[]).await
}
pub async fn list_product_releases(
&self,
id: ProductId,
query: &libtea_core::ListParams<libtea_core::enums::ReleaseSort>,
) -> Result<Page<ProductRelease>, ClientError> {
self.page(&format!("product/{id}/releases"), query, |sort| {
sort.as_str()
})
.await
}
pub async fn get_product_cle(&self, id: ProductId) -> Result<CleFetch, ClientError> {
self.cle(&format!("product/{id}/cle")).await
}
pub async fn query_product_releases(
&self,
query: &libtea_core::ListParams<libtea_core::enums::ReleaseSort>,
) -> Result<Page<ProductRelease>, ClientError> {
self.page("productReleases", query, |sort| sort.as_str())
.await
}
pub async fn get_product_release(
&self,
id: ProductReleaseId,
) -> Result<ProductReleaseWithCollection, ClientError> {
let bytes = self.get_bytes(&format!("productRelease/{id}"), &[]).await?;
Ok(parse_product_release(&bytes)?)
}
pub async fn get_product_release_cle(
&self,
id: ProductReleaseId,
) -> Result<CleFetch, ClientError> {
self.cle(&format!("productRelease/{id}/cle")).await
}
pub async fn list_product_release_collections(
&self,
id: ProductReleaseId,
query: &libtea_core::ListParams<libtea_core::enums::CollectionSort>,
) -> Result<Page<Collection>, ClientError> {
let page: Page<Collection> = self
.page(&format!("productRelease/{id}/collections"), query, |sort| {
sort.as_str()
})
.await?;
for collection in &page.results {
collection.validate()?;
}
Ok(page)
}
pub async fn get_latest_product_release_collection(
&self,
id: ProductReleaseId,
) -> Result<Collection, ClientError> {
let bytes = self
.get_bytes(&format!("productRelease/{id}/collection/latest"), &[])
.await?;
Ok(parse_collection(&bytes)?)
}
pub async fn get_product_release_collection(
&self,
id: ProductReleaseId,
version: u64,
) -> Result<Collection, ClientError> {
let bytes = self
.get_bytes(&format!("productRelease/{id}/collection/{version}"), &[])
.await?;
Ok(parse_collection(&bytes)?)
}
pub async fn query_components(
&self,
query: &libtea_core::ListParams<libtea_core::enums::ComponentSort>,
) -> Result<Page<Component>, ClientError> {
self.page("components", query, |sort| sort.as_str()).await
}
pub async fn get_component(&self, id: ComponentId) -> Result<Component, ClientError> {
self.get_json(&format!("component/{id}"), &[]).await
}
pub async fn list_component_releases(
&self,
id: ComponentId,
query: &libtea_core::ListParams<libtea_core::enums::ReleaseSort>,
) -> Result<Page<ComponentRelease>, ClientError> {
self.page(&format!("component/{id}/releases"), query, |sort| {
sort.as_str()
})
.await
}
pub async fn get_component_cle(&self, id: ComponentId) -> Result<CleFetch, ClientError> {
self.cle(&format!("component/{id}/cle")).await
}
pub async fn query_component_releases(
&self,
query: &libtea_core::ListParams<libtea_core::enums::ReleaseSort>,
) -> Result<Page<ComponentRelease>, ClientError> {
self.page("componentReleases", query, |sort| sort.as_str())
.await
}
pub async fn get_component_release(
&self,
id: ComponentReleaseId,
) -> Result<ComponentReleaseWithCollection, ClientError> {
let bytes = self
.get_bytes(&format!("componentRelease/{id}"), &[])
.await?;
Ok(parse_component_release(&bytes)?)
}
pub async fn get_component_release_cle(
&self,
id: ComponentReleaseId,
) -> Result<CleFetch, ClientError> {
self.cle(&format!("componentRelease/{id}/cle")).await
}
pub async fn list_component_release_collections(
&self,
id: ComponentReleaseId,
query: &libtea_core::ListParams<libtea_core::enums::CollectionSort>,
) -> Result<Page<Collection>, ClientError> {
let page: Page<Collection> = self
.page(
&format!("componentRelease/{id}/collections"),
query,
|sort| sort.as_str(),
)
.await?;
for collection in &page.results {
collection.validate()?;
}
Ok(page)
}
pub async fn get_latest_component_release_collection(
&self,
id: ComponentReleaseId,
) -> Result<Collection, ClientError> {
let bytes = self
.get_bytes(&format!("componentRelease/{id}/collection/latest"), &[])
.await?;
Ok(parse_collection(&bytes)?)
}
pub async fn get_component_release_collection(
&self,
id: ComponentReleaseId,
version: u64,
) -> Result<Collection, ClientError> {
let bytes = self
.get_bytes(&format!("componentRelease/{id}/collection/{version}"), &[])
.await?;
Ok(parse_collection(&bytes)?)
}
pub async fn get_latest_artifact(
&self,
id: libtea_core::ArtifactId,
) -> Result<Artifact, ClientError> {
let bytes = self
.get_bytes(&format!("artifact/{id}/latest"), &[])
.await?;
Ok(parse_artifact(&bytes)?)
}
pub async fn get_artifact(
&self,
id: libtea_core::ArtifactId,
version: u64,
) -> Result<Artifact, ClientError> {
let bytes = self
.get_bytes(&format!("artifact/{id}/{version}"), &[])
.await?;
Ok(parse_artifact(&bytes)?)
}
pub async fn download_format(
&self,
artifact: &Artifact,
media_type: &str,
if_none_match: Option<&str>,
) -> Result<ArtifactFetch, ClientError> {
let format = artifact.format(media_type).ok_or_else(|| {
ClientError::Model(libtea_core::ModelError::Invalid(format!(
"artifact has no format `{media_type}`"
)))
})?;
format.validate()?;
let checksums = format.checksums.clone().unwrap_or_default();
if let Some(external) = &format.url {
let url = Url::parse(external).map_err(|_| {
ClientError::Model(libtea_core::ModelError::Invalid(
"external artifact URL".into(),
))
})?;
return self
.download_url(url, None, if_none_match, &checksums, false)
.await;
}
self.download_api(
&format!("artifact/{}/{}/download", artifact.uuid, artifact.version),
Some(media_type),
if_none_match,
&checksums,
)
.await
}
pub async fn download_latest_artifact(
&self,
id: libtea_core::ArtifactId,
media_type: Option<&str>,
checksums: &[Checksum],
if_none_match: Option<&str>,
) -> Result<ArtifactFetch, ClientError> {
self.download_api(
&format!("artifact/{id}/latest/download"),
media_type,
if_none_match,
checksums,
)
.await
}
pub async fn download_artifact(
&self,
id: libtea_core::ArtifactId,
version: u64,
media_type: Option<&str>,
checksums: &[Checksum],
if_none_match: Option<&str>,
) -> Result<ArtifactFetch, ClientError> {
self.download_api(
&format!("artifact/{id}/{version}/download"),
media_type,
if_none_match,
checksums,
)
.await
}
pub async fn download_signature_for_format(
&self,
artifact: &Artifact,
media_type: &str,
if_none_match: Option<&str>,
) -> Result<SignatureFetch, ClientError> {
let format = artifact.format(media_type).ok_or_else(|| {
ClientError::Model(libtea_core::ModelError::Invalid(format!(
"artifact has no format `{media_type}`"
)))
})?;
if let Some(external) = &format.signature_url {
let url = Url::parse(external).map_err(|_| {
ClientError::Model(libtea_core::ModelError::Invalid(
"external artifact URL".into(),
))
})?;
return match self
.download_url(url, None, if_none_match, &[], false)
.await?
{
ArtifactFetch::Body(body) => Ok(SignatureFetch::Present(Box::new(body))),
ArtifactFetch::NotModified { etag, .. } => Ok(SignatureFetch::NotModified { etag }),
};
}
self.signature_api(
&format!(
"artifact/{}/{}/signature/download",
artifact.uuid, artifact.version
),
Some(media_type),
if_none_match,
)
.await
}
pub async fn download_signature(
&self,
id: libtea_core::ArtifactId,
version: u64,
media_type: Option<&str>,
if_none_match: Option<&str>,
) -> Result<SignatureFetch, ClientError> {
self.signature_api(
&format!("artifact/{id}/{version}/signature/download"),
media_type,
if_none_match,
)
.await
}
pub async fn download_latest_signature(
&self,
id: libtea_core::ArtifactId,
media_type: Option<&str>,
if_none_match: Option<&str>,
) -> Result<SignatureFetch, ClientError> {
self.signature_api(
&format!("artifact/{id}/latest/signature/download"),
media_type,
if_none_match,
)
.await
}
pub async fn head_artifact(
&self,
id: libtea_core::ArtifactId,
version: u64,
media_type: Option<&str>,
) -> Result<std::collections::HashMap<String, String>, ClientError> {
let mut query = Vec::new();
if let Some(media_type) = media_type {
query.push(("mediaType", media_type));
}
let url = operation_url(
&self.prefix,
&format!("artifact/{id}/{version}/download"),
&query,
)?;
let response = self
.client
.inner
.send(Call {
method: reqwest::Method::HEAD,
url,
api_prefix: Some(self.prefix.clone()),
artifact: true,
body: None,
content_type: None,
basic_auth: false,
if_none_match: None,
if_modified_since: None,
max_body: 0,
})
.await?;
if !response.status.is_success() && response.status.as_u16() != 304 {
return Err(map_api_error(&response));
}
let mut headers = std::collections::HashMap::new();
for (name, value) in response.headers.iter() {
if name == reqwest::header::AUTHORIZATION {
continue;
}
if let Ok(text) = value.to_str() {
headers.insert(name.to_string(), text.to_owned());
}
}
Ok(headers)
}
async fn page<T, S, F>(
&self,
path: &str,
query: &libtea_core::ListParams<S>,
sort_name: F,
) -> Result<Page<T>, ClientError>
where
T: DeserializeOwned,
F: Fn(&S) -> &'static str,
{
query.validate()?;
let mut pairs: Vec<(String, String)> = Vec::new();
if let Some(size) = query.page_size {
pairs.push(("pageSize".into(), size.to_string()));
}
if let Some(token) = &query.page_token {
pairs.push(("pageToken".into(), token.clone()));
}
if let Some(field) = &query.sort_field {
pairs.push(("sortField".into(), sort_name(field).to_owned()));
}
if let Some(order) = query.sort_order {
pairs.push(("sortOrder".into(), order.as_str().to_owned()));
}
if let Some(id_type) = &query.id_type {
pairs.push(("idType".into(), id_type.as_str().to_owned()));
}
if let Some(id_value) = &query.id_value {
pairs.push(("idValue".into(), id_value.clone()));
}
let borrowed: Vec<(&str, &str)> = pairs
.iter()
.map(|(k, v)| (k.as_str(), v.as_str()))
.collect();
self.get_json(path, &borrowed).await
}
async fn cle(&self, path: &str) -> Result<CleFetch, ClientError> {
let response = self.get_raw(path, &[], false).await?;
if response.status.as_u16() == 404 {
if let Some(code) = libtea_core::tea_error_code(
header_str(&response.headers, CONTENT_TYPE_HEADER),
&response.body,
) {
if code == "NOT_IMPLEMENTED" {
let message = libtea_core::TeaErrorBody::parse(&response.body)
.ok()
.and_then(|body| body.message);
return Ok(CleFetch::NotImplemented { message });
}
}
return Err(map_api_error(&response));
}
if !response.status.is_success() {
return Err(map_api_error(&response));
}
require_json(&response)?;
Ok(CleFetch::Events(serde_json::from_slice(&response.body)?))
}
async fn get_json<T: DeserializeOwned>(
&self,
path: &str,
query: &[(&str, &str)],
) -> Result<T, ClientError> {
let bytes = self.get_bytes(path, query).await?;
Ok(serde_json::from_slice(&bytes)?)
}
async fn get_bytes(&self, path: &str, query: &[(&str, &str)]) -> Result<Vec<u8>, ClientError> {
let response = self.get_raw(path, query, false).await?;
if !response.status.is_success() {
return Err(map_api_error(&response));
}
require_json(&response)?;
Ok(response.body.to_vec())
}
async fn get_raw(
&self,
path: &str,
query: &[(&str, &str)],
artifact: bool,
) -> Result<crate::transport::RawResponse, ClientError> {
let url = operation_url(&self.prefix, path, query)?;
self.client
.inner
.send(Call {
method: reqwest::Method::GET,
url,
api_prefix: Some(self.prefix.clone()),
artifact,
body: None,
content_type: None,
basic_auth: false,
if_none_match: None,
if_modified_since: None,
max_body: if artifact {
self.client.inner.limits.max_artifact_bytes
} else {
self.client.inner.limits.max_json_bytes
},
})
.await
}
async fn download_api(
&self,
path: &str,
media_type: Option<&str>,
if_none_match: Option<&str>,
checksums: &[Checksum],
) -> Result<ArtifactFetch, ClientError> {
let mut query = Vec::new();
if let Some(media_type) = media_type {
query.push(("mediaType", media_type));
}
let url = operation_url(&self.prefix, path, &query)?;
self.download_url(
url,
Some(self.prefix.clone()),
if_none_match,
checksums,
true,
)
.await
}
async fn download_url(
&self,
url: Url,
api_prefix: Option<Url>,
if_none_match: Option<&str>,
checksums: &[Checksum],
under_api: bool,
) -> Result<ArtifactFetch, ClientError> {
let response = self
.client
.inner
.send(Call {
method: reqwest::Method::GET,
url,
api_prefix: if under_api { api_prefix } else { None },
artifact: true,
body: None,
content_type: None,
basic_auth: false,
if_none_match: if_none_match.map(str::to_owned),
if_modified_since: None,
max_body: self.client.inner.limits.max_artifact_bytes,
})
.await?;
if response.status.as_u16() == 304 {
return Ok(ArtifactFetch::NotModified {
etag: header_string(&response, "etag"),
cache_control: header_string(&response, "cache-control"),
vary: header_string(&response, "vary"),
});
}
if !response.status.is_success() {
return Err(map_api_error(&response));
}
let encoding = header_string(&response, "content-encoding");
let decoded = if response.content_decoded {
response.body.to_vec()
} else {
libtea_core::checksum::decode_content_coding(
&response.body,
encoding.as_deref(),
self.client.inner.limits.max_artifact_bytes,
)?
};
let integrity = libtea_core::verify_checksums_with_policy(
&decoded,
checksums,
self.client.inner.checksum_policy,
);
if !matches!(
integrity.outcome,
IntegrityOutcome::Verified | IntegrityOutcome::NotDeclared
) {
response.evict_cached().await;
return Err(ClientError::Integrity {
outcome: integrity.outcome.clone(),
report: integrity,
});
}
response.commit_cache().await;
let etag = header_string(&response, "etag");
let strong_etag = etag
.as_ref()
.is_some_and(|value| !value.trim().starts_with("W/"));
Ok(ArtifactFetch::Body(DownloadedArtifact {
bytes: decoded,
content_type: header_string(&response, "content-type"),
content_encoding: encoding,
etag,
strong_etag,
cache_control: header_string(&response, "cache-control"),
content_location: header_string(&response, "content-location"),
vary: header_string(&response, "vary"),
repr_digest: header_string(&response, "repr-digest"),
final_url: response.final_url.to_string(),
integrity,
}))
}
async fn signature_api(
&self,
path: &str,
media_type: Option<&str>,
if_none_match: Option<&str>,
) -> Result<SignatureFetch, ClientError> {
match self
.download_api(path, media_type, if_none_match, &[])
.await
{
Ok(ArtifactFetch::Body(body)) => Ok(SignatureFetch::Present(Box::new(body))),
Ok(ArtifactFetch::NotModified { etag, .. }) => Ok(SignatureFetch::NotModified { etag }),
Err(ClientError::Api {
status: 404,
tea_error: Some(code),
..
}) if code == "SIGNATURE_NOT_FOUND" => Ok(SignatureFetch::NotPublished),
Err(other) => Err(other),
}
}
}
fn require_json(response: &crate::transport::RawResponse) -> Result<(), ClientError> {
if is_json_content_type(header_str(&response.headers, CONTENT_TYPE_HEADER)) {
Ok(())
} else {
Err(ClientError::Model(libtea_core::ModelError::Invalid(
"expected application/json".into(),
)))
}
}
fn header_string(response: &crate::transport::RawResponse, name: &str) -> Option<String> {
response
.headers
.get(name)
.and_then(|value| value.to_str().ok())
.map(str::to_owned)
}