From 830f1bd49b6ef3912f8501e4edfd78647da4017d Mon Sep 17 00:00:00 2001 From: Christoph Herzog Date: Tue, 28 Feb 2023 12:41:25 +0100 Subject: [PATCH] registry: Move queries from RegistryClient methods functions in api module --- lib/registry/src/api.rs | 54 ++++++++++++++++++++++++++ lib/registry/src/client.rs | 78 ++++++++------------------------------ lib/registry/src/lib.rs | 3 +- 3 files changed, 72 insertions(+), 63 deletions(-) create mode 100644 lib/registry/src/api.rs diff --git a/lib/registry/src/api.rs b/lib/registry/src/api.rs new file mode 100644 index 00000000000..378c961ad01 --- /dev/null +++ b/lib/registry/src/api.rs @@ -0,0 +1,54 @@ +use anyhow::Context; + +use crate::RegistryClient; + +use crate::graphql::mutations; +use crate::types::{PublishDeployAppOutput, PublishDeployAppRawVars}; + +/// Generate a Deploy token for for the given Deploy app version id. +pub async fn generate_deploy_token( + client: &RegistryClient, + app_version_id: String, +) -> Result { + let vars = mutations::generate_deploy_token::Variables { app_version_id }; + let res = client + .execute::(vars) + .await?; + let token = res + .generate_deploy_token + .context("Query did not return a token")? + .token; + + Ok(token) +} + +/// Publish a Deploy app. +/// +/// Takes a raw, unvalidated deployment config. +// TODO: Add a variant of this query that takes a typed DeployV1 config. +pub async fn publish_deploy_app_raw( + client: &RegistryClient, + data: PublishDeployAppRawVars, +) -> Result { + let vars2 = mutations::publish_deploy_app::Variables { + name: data.name, + owner: data.namespace, + config: serde_json::to_string(&data.config)?, + }; + + let version = client + .execute::(vars2) + .await? + .publish_deploy_app + .context("Query did not return data")? + .deploy_app_version; + let app = version.app.context("Query did not return expected data")?; + + Ok(PublishDeployAppOutput { + app_id: app.id, + app_name: app.name, + version_id: version.id, + version_name: version.version, + owner_name: app.owner.global_name, + }) +} diff --git a/lib/registry/src/client.rs b/lib/registry/src/client.rs index 05aea785fd0..3ae909bc4b1 100644 --- a/lib/registry/src/client.rs +++ b/lib/registry/src/client.rs @@ -2,11 +2,6 @@ use anyhow::Context; use graphql_client::GraphQLQuery; use url::Url; -use crate::{ - graphql, - types::{PublishDeployAppOutput, PublishDeployAppRawVars}, -}; - /// API client for the Wasmer registry. #[derive(Clone)] pub struct RegistryClient { @@ -60,7 +55,7 @@ impl RegistryClient { } /// Execute a GraphQL query. - async fn execute( + pub(crate) async fn execute_unchecked( &self, vars: Q::Variables, ) -> Result, reqwest::Error> { @@ -82,65 +77,24 @@ impl RegistryClient { } /// Execute a GraphQL query, and convert a response with errors to a Rust error. - async fn execute_checked( + pub(crate) async fn execute( &self, vars: Q::Variables, ) -> Result { - let res = self.execute::(vars).await?; - - if let Some(data) = res.data { - Ok(data) - } else { - // TODO: Better error forwaring with a custom error type. - anyhow::bail!("GraphQL error: {:?}", res.errors); + let res = self.execute_unchecked::(vars).await?; + + match (res.data, res.errors) { + (_, Some(errors)) => { + // TODO: Better error forwaring with a custom error type. + let errors = errors + .iter() + .map(|x| x.to_string()) + .collect::>() + .join(", "); + anyhow::bail!("GraphQL error: {errors}"); + } + (Some(data), None) => Ok(data), + (None, None) => anyhow::bail!("GraphQL response contained no data"), } } - - /// Generate a Deploy token for for the given Deploy app version id. - pub async fn generate_deploy_token( - &self, - app_version_id: String, - ) -> Result { - let vars = graphql::mutations::generate_deploy_token::Variables { app_version_id }; - let res = self - .execute_checked::(vars) - .await?; - let token = res - .generate_deploy_token - .context("Query did not return a token")? - .token; - - Ok(token) - } - - /// Publish a Deploy app. - /// - /// Takes a raw, unvalidated deployment config. - // TODO: Add a variant of this query that takes a typed DeployV1 config. - pub async fn publish_deploy_app_raw( - &self, - data: PublishDeployAppRawVars, - ) -> Result { - let vars2 = graphql::mutations::publish_deploy_app::Variables { - name: data.name, - owner: data.namespace, - config: serde_json::to_string(&data.config)?, - }; - - let version = self - .execute_checked::(vars2) - .await? - .publish_deploy_app - .context("Query did not return data")? - .deploy_app_version; - let app = version.app.context("Query did not return expected data")?; - - Ok(PublishDeployAppOutput { - app_id: app.id, - app_name: app.name, - version_id: version.id, - version_name: version.version, - owner_name: app.owner.global_name, - }) - } } diff --git a/lib/registry/src/lib.rs b/lib/registry/src/lib.rs index 17edd8aad14..02fdd2fe89d 100644 --- a/lib/registry/src/lib.rs +++ b/lib/registry/src/lib.rs @@ -8,6 +8,7 @@ //! curl -sSfL https://registry.wapm.io/graphql/schema.graphql > lib/registry/graphql/schema.graphql //! ``` +pub mod api; mod client; pub mod config; pub mod graphql; @@ -18,7 +19,7 @@ pub mod publish; pub mod types; pub mod utils; -pub use crate::client::RegistryClient; +pub use client::RegistryClient; use anyhow::Context; use core::ops::Range;