Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - feat: fvm API Client and updated type definitions #3566

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/fluvio-hub-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/infinyon/fluvio"
publish = false

[dependencies]
anyhow = { workspace = true }
cargo_toml = { workspace = true }
const_format = { workspace = true }
dirs = { workspace = true }
Expand All @@ -19,6 +20,7 @@ pem = "3.0"
rand = { workspace = true }
rand_core = "0.6"
sha2 = { workspace = true }
semver = { workspace = true }
serde = { workspace = true, features=["derive"] }
serde_json = { workspace = true }
serde_yaml = { workspace = true }
Expand Down
56 changes: 0 additions & 56 deletions crates/fluvio-hub-util/src/fvm.rs

This file was deleted.

115 changes: 115 additions & 0 deletions crates/fluvio-hub-util/src/fvm/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//! Hub FVM API Client

use anyhow::{Error, Result};
use surf::get;
use url::Url;

use fluvio_hub_protocol::constants::HUB_REMOTE;

use super::{Channel, PackageSet, RustTarget};

/// HTTP Client for interacting with the Hub FVM API
pub struct Client {
EstebanBorai marked this conversation as resolved.
Show resolved Hide resolved
api_url: Url,
}

impl Default for Client {
fn default() -> Self {
// Safe to `unwrap`, `API_BASE_URL` is a valid URL and it's a constant
Self::new(Url::parse(HUB_REMOTE).unwrap())
EstebanBorai marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl Client {
pub fn new(api_url: Url) -> Self {
Self { api_url }
}

/// Fetches a [`PackageSet`] from the Hub with the specific [`Channel`]
#[allow(unused)]
pub async fn fetch_package_set<S: AsRef<str>>(
&self,
name: S,
EstebanBorai marked this conversation as resolved.
Show resolved Hide resolved
channel: &Channel,
arch: &RustTarget,
) -> Result<PackageSet> {
let url = self.make_fetch_package_set_url(name, channel, arch)?;
let mut res = get(url).await.map_err(|err| {
tracing::error!("Failed to fetch package set: {}", err);
EstebanBorai marked this conversation as resolved.
Show resolved Hide resolved
Error::msg(err.to_string())
})?;
let pkg = res.body_json::<PackageSet>().await.map_err(|err| {
tracing::error!("Failed to parse package set: {}", err);
Error::msg(err.to_string())
})?;

Ok(pkg)
}

/// Builds the URL to the Hub API for fetching a [`PackageSet`] using the
/// [`Client`]'s `api_url`.
fn make_fetch_package_set_url(
&self,
name: impl AsRef<str>,
channel: &Channel,
arch: &RustTarget,
) -> Result<Url> {
let url = format!(
"{}hub/v1/fvm/pkgset/{name}/{channel}/{arch}",
self.api_url,
name = name.as_ref(),
channel = channel,
arch = arch
);

Ok(Url::parse(&url)?)
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr;

use url::Url;
use semver::Version;

use super::{Client, Channel, RustTarget};

#[test]
fn creates_a_default_client() {
let client = Client::default();

assert_eq!(
client.api_url,
Url::parse("https://hub.infinyon.cloud/hub/v1/fvm").unwrap()
);
}

#[test]
fn builds_url_for_fetching_pkgsets() {
let client = Client::default();
let url = client
.make_fetch_package_set_url(
"fluvio",
&Channel::Stable,
&RustTarget::ArmUnknownLinuxGnueabihf,
)
.unwrap();

assert_eq!(url.as_str(), "https://hub.infinyon.cloud/hub/v1/fvm/pkgset/fluvio/stable/arm-unknown-linux-gnueabihf");
}

#[test]
fn builds_url_for_fetching_pkgsets_on_version() {
let client = Client::default();
let url = client
.make_fetch_package_set_url(
"fluvio",
&Channel::Tag(Version::from_str("0.10.14-dev+123345abc").unwrap()),
&RustTarget::ArmUnknownLinuxGnueabihf,
)
.unwrap();

assert_eq!(url.as_str(), "https://hub.infinyon.cloud/hub/v1/fvm/pkgset/fluvio/0.10.14-dev+123345abc/arm-unknown-linux-gnueabihf");
}
}
Loading
Loading