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

CLI: Partially switch from wasmer-registry to wasmer-api #4433

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion lib/backend-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ This is not always sensible though, depending on which nested data you want to
fetch.

[cynic-api-docs]: https://docs.rs/cynic/latest/cynic/
[cynic-web-ui]: https://docs.rs/cynic/latest/cynic/
[cynic-web-ui]: https://generator.cynic-rs.dev/
[cynic-website]: https://cynic-rs.dev
30 changes: 25 additions & 5 deletions lib/backend-api/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ impl WasmerClient {
self.auth_token.as_deref()
}

pub fn user_agent(&self) -> &reqwest::header::HeaderValue {
&self.user_agent
}

pub fn client(&self) -> &reqwest::Client {
&self.client
}

fn parse_user_agent(user_agent: &str) -> Result<reqwest::header::HeaderValue, anyhow::Error> {
if user_agent.is_empty() {
bail!("user agent must not be empty");
Expand Down Expand Up @@ -66,6 +74,22 @@ impl WasmerClient {
self
}

fn prepare_request(&self, req: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
let req = req.header(reqwest::header::USER_AGENT, &self.user_agent);
if let Some(token) = &self.auth_token {
req.bearer_auth(token)
} else {
req
}
}

pub async fn send(
&self,
req: reqwest::RequestBuilder,
) -> Result<reqwest::Response, reqwest::Error> {
self.prepare_request(req).send().await
}

pub(crate) async fn run_graphql_raw<ResponseData, Vars>(
&self,
operation: Operation<ResponseData, Vars>,
Expand All @@ -78,12 +102,8 @@ impl WasmerClient {
.client
.post(self.graphql_endpoint.as_str())
.header(reqwest::header::USER_AGENT, &self.user_agent);
let req = if let Some(token) = &self.auth_token {
req.bearer_auth(token)
} else {
req
};

let req = self.prepare_request(req);
if self.extra_debugging {
tracing::trace!(
query=%operation.query,
Expand Down
2 changes: 0 additions & 2 deletions lib/backend-api/src/gql.rs

This file was deleted.

58 changes: 49 additions & 9 deletions lib/backend-api/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ pub async fn fetch_webc_package(
webc::compat::Container::from_bytes(data).context("failed to parse webc package")
}

/// Get the currently logged in used, together with all accessible namespaces.
///
/// You can optionally filter the namespaces by the user role.
pub async fn current_user(client: &WasmerClient) -> Result<types::User, anyhow::Error> {
client
.run_graphql(types::GetCurrentUser::build(()))
.await?
.viewer
.context("not logged in")
}

/// Get the currently logged in used, together with all accessible namespaces.
///
/// You can optionally filter the namespaces by the user role.
Expand All @@ -49,9 +60,9 @@ pub async fn current_user_with_namespaces(
namespace_role: Option<types::GrapheneRole>,
) -> Result<types::UserWithNamespaces, anyhow::Error> {
client
.run_graphql(types::GetCurrentUser::build(types::GetCurrentUserVars {
namespace_role,
}))
.run_graphql(types::GetCurrentUserWithNamespaces::build(
types::GetCurrentUserWithNamespacesVars { namespace_role },
))
.await?
.viewer
.context("not logged in")
Expand Down Expand Up @@ -401,9 +412,11 @@ pub async fn user_accessible_apps(

// Get all aps in user-accessible namespaces.
let namespace_res = client
.run_graphql(types::GetCurrentUser::build(types::GetCurrentUserVars {
namespace_role: None,
}))
.run_graphql(types::GetCurrentUserWithNamespaces::build(
types::GetCurrentUserWithNamespacesVars {
namespace_role: None,
},
))
.await?;
let active_user = namespace_res.viewer.context("not logged in")?;
let namespace_names = active_user
Expand Down Expand Up @@ -503,9 +516,11 @@ pub async fn user_namespaces(
client: &WasmerClient,
) -> Result<Vec<types::Namespace>, anyhow::Error> {
let user = client
.run_graphql(types::GetCurrentUser::build(types::GetCurrentUserVars {
namespace_role: None,
}))
.run_graphql(types::GetCurrentUserWithNamespaces::build(
types::GetCurrentUserWithNamespacesVars {
namespace_role: None,
},
))
.await?
.viewer
.context("not logged in")?;
Expand Down Expand Up @@ -546,6 +561,8 @@ pub async fn create_namespace(
.context("no namespace returned")
}

const PACKAGE_VERSION_LATEST: &'static str = "latest";

/// Retrieve a package by its name.
pub async fn get_package(
client: &WasmerClient,
Expand All @@ -571,6 +588,29 @@ pub async fn get_package_version(
.map(|x| x.get_package_version)
}

/// Retrieve language bindings for a package version.
pub async fn get_package_version_bindings(
client: &WasmerClient,
name: String,
version: Option<String>,
) -> Result<Vec<types::PackageVersionLanguageBinding>, anyhow::Error> {
let version = version.unwrap_or_else(|| PACKAGE_VERSION_LATEST.to_string());
let out = client
.run_graphql_strict(types::GetPackageVersionBindings::build(
types::GetPackageVersionVars { name, version },
))
.await?;

let bindings = out
.get_package_version
.context("could not find package version")?
.bindings
.into_iter()
.flatten()
.collect::<Vec<_>>();
Ok(bindings)
}

/// Retrieve package versions for an app.
pub async fn get_package_versions(
client: &WasmerClient,
Expand Down
68 changes: 58 additions & 10 deletions lib/backend-api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,20 @@ mod queries {
Viewer,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query")]
pub struct GetCurrentUser {
pub viewer: Option<User>,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct GetCurrentUserVars {
pub struct GetCurrentUserWithNamespacesVars {
pub namespace_role: Option<GrapheneRole>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "GetCurrentUserVars")]
pub struct GetCurrentUser {
#[cynic(graphql_type = "Query", variables = "GetCurrentUserWithNamespacesVars")]
pub struct GetCurrentUserWithNamespaces {
pub viewer: Option<UserWithNamespaces>,
}

Expand Down Expand Up @@ -92,6 +98,12 @@ mod queries {
pub package: Package,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct GetPackageVersionVars {
pub name: String,
pub version: String,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct GetPackageVars {
pub name: String,
Expand All @@ -104,12 +116,6 @@ mod queries {
pub get_package: Option<Package>,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct GetPackageVersionVars {
pub name: String,
pub version: String,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "GetPackageVersionVars")]
pub struct GetPackageVersion {
Expand All @@ -123,6 +129,48 @@ mod queries {
Oldest,
}

/// Retrieve available bindings for a package version.
#[derive(cynic::QueryFragment, Debug, Clone)]
#[cynic(graphql_type = "Query", variables = "GetPackageVersionVars")]
pub struct GetPackageVersionBindings {
#[arguments(name: $name, version: $version)]
pub get_package_version: Option<PackageVersionWithBindings>,
}

#[derive(cynic::QueryFragment, Clone, Debug)]
#[cynic(graphql_type = "PackageVersion")]
pub struct PackageVersionWithBindings {
pub bindings: Vec<Option<PackageVersionLanguageBinding>>,
}

#[derive(cynic::QueryFragment, Clone, Debug)]
pub struct PackageVersionLanguageBinding {
pub id: cynic::Id,
pub language: ProgrammingLanguage,
pub url: String,
pub generator: BindingsGenerator,
}

#[derive(cynic::QueryFragment, Clone, Debug)]
pub struct BindingsGenerator {
pub command_name: String,
}

#[derive(cynic::Enum, Clone, Copy, Debug, PartialEq, Eq)]
pub enum ProgrammingLanguage {
Python,
Javascript,
}

impl ProgrammingLanguage {
pub fn as_str(self) -> &'static str {
match self {
ProgrammingLanguage::Python => "python",
ProgrammingLanguage::Javascript => "javascript",
}
}
}

#[derive(cynic::QueryVariables, Debug, Clone, Default)]
pub struct AllPackageVersionsVars {
pub offset: Option<i32>,
Expand Down Expand Up @@ -200,7 +248,7 @@ mod queries {
}

#[derive(cynic::QueryFragment, Debug, Clone)]
#[cynic(graphql_type = "User", variables = "GetCurrentUserVars")]
#[cynic(graphql_type = "User", variables = "GetCurrentUserWithNamespacesVars")]
pub struct UserWithNamespaces {
pub id: cynic::Id,
pub username: String,
Expand Down
4 changes: 2 additions & 2 deletions lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ wasm-coredump-builder = { version = "0.1.11", optional = true }
tracing = { version = "0.1" }
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt", "json"] }
async-trait = "0.1.68"
tokio = { version = "1.28.1", features = ["macros", "rt-multi-thread"] }
tokio = { version = "1.28.1", features = ["macros", "rt-multi-thread", "fs"] }
once_cell = "1.17.1"
indicatif = "0.17.5"
indicatif = { version = "0.17.5", features = ["futures"]}
opener = "0.6.1"
hyper = { version = "0.14.27", features = ["server"] }
http = "0.2.9"
Expand Down
Loading
Loading