chore: refine api
This commit is contained in:
parent
3f1832444f
commit
99fc95f33a
|
|
@ -1,25 +1,12 @@
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use database_entity::dto::UpdatePublishNamespace;
|
use database_entity::dto::UpdatePublishNamespace;
|
||||||
use reqwest::Method;
|
use reqwest::{Body, Method};
|
||||||
use shared_entity::response::{AppResponse, AppResponseError};
|
use shared_entity::response::{AppResponse, AppResponseError};
|
||||||
|
|
||||||
use crate::Client;
|
use crate::Client;
|
||||||
|
|
||||||
|
// Publisher API
|
||||||
impl Client {
|
impl Client {
|
||||||
pub async fn get_workspace_publish_namespace(
|
|
||||||
&self,
|
|
||||||
workspace_id: &str,
|
|
||||||
) -> Result<String, AppResponseError> {
|
|
||||||
let url = format!(
|
|
||||||
"{}/api/workspace/{}/publish-namespace",
|
|
||||||
self.base_url, workspace_id
|
|
||||||
);
|
|
||||||
let resp = self.cloud_client.get(&url).send().await?;
|
|
||||||
AppResponse::<String>::from_response(resp)
|
|
||||||
.await?
|
|
||||||
.into_data()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn set_workspace_publish_namespace(
|
pub async fn set_workspace_publish_namespace(
|
||||||
&self,
|
&self,
|
||||||
workspace_id: &str,
|
workspace_id: &str,
|
||||||
|
|
@ -66,6 +53,62 @@ impl Client {
|
||||||
AppResponse::<()>::from_response(resp).await?.into_error()
|
AppResponse::<()>::from_response(resp).await?.into_error()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn put_published_collab_blob<T>(
|
||||||
|
&self,
|
||||||
|
workspace_id: &str,
|
||||||
|
doc_name: &str,
|
||||||
|
data: T,
|
||||||
|
) -> Result<(), AppResponseError>
|
||||||
|
where
|
||||||
|
T: Into<Body>,
|
||||||
|
{
|
||||||
|
let url = format!(
|
||||||
|
"{}/api/workspace/{}/publish/{}/blob",
|
||||||
|
self.base_url, workspace_id, doc_name
|
||||||
|
);
|
||||||
|
let resp = self
|
||||||
|
.http_client_with_auth(Method::PUT, &url)
|
||||||
|
.await?
|
||||||
|
.body(data)
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
AppResponse::<()>::from_response(resp).await?.into_error()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_published_collab(
|
||||||
|
&self,
|
||||||
|
workspace_id: &str,
|
||||||
|
doc_name: &str,
|
||||||
|
) -> Result<(), AppResponseError> {
|
||||||
|
let url = format!(
|
||||||
|
"{}/api/workspace/{}/publish/{}",
|
||||||
|
self.base_url, workspace_id, doc_name
|
||||||
|
);
|
||||||
|
let resp = self
|
||||||
|
.http_client_with_auth(Method::DELETE, &url)
|
||||||
|
.await?
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
AppResponse::<()>::from_response(resp).await?.into_error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Guest API (no login required)
|
||||||
|
impl Client {
|
||||||
|
pub async fn get_workspace_publish_namespace(
|
||||||
|
&self,
|
||||||
|
workspace_id: &str,
|
||||||
|
) -> Result<String, AppResponseError> {
|
||||||
|
let url = format!(
|
||||||
|
"{}/api/workspace/{}/publish-namespace",
|
||||||
|
self.base_url, workspace_id
|
||||||
|
);
|
||||||
|
let resp = self.cloud_client.get(&url).send().await?;
|
||||||
|
AppResponse::<String>::from_response(resp)
|
||||||
|
.await?
|
||||||
|
.into_data()
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn get_published_collab<T>(
|
pub async fn get_published_collab<T>(
|
||||||
&self,
|
&self,
|
||||||
publish_namespace: &str,
|
publish_namespace: &str,
|
||||||
|
|
|
||||||
|
|
@ -157,6 +157,8 @@ pub async fn publish_collab(
|
||||||
publisher_uuid: &Uuid,
|
publisher_uuid: &Uuid,
|
||||||
metadata: &serde_json::Value,
|
metadata: &serde_json::Value,
|
||||||
) -> Result<(), AppError> {
|
) -> Result<(), AppError> {
|
||||||
|
check_workspace_owner_or_publisher(pg_pool, publisher_uuid, workspace_id, doc_name).await?;
|
||||||
|
check_collab_doc_name(doc_name).await?;
|
||||||
insert_or_replace_publish_collab_meta(pg_pool, workspace_id, doc_name, publisher_uuid, metadata)
|
insert_or_replace_publish_collab_meta(pg_pool, workspace_id, doc_name, publisher_uuid, metadata)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -166,11 +168,10 @@ pub async fn put_published_collab_blob(
|
||||||
pg_pool: &PgPool,
|
pg_pool: &PgPool,
|
||||||
workspace_id: &Uuid,
|
workspace_id: &Uuid,
|
||||||
doc_name: &str,
|
doc_name: &str,
|
||||||
user_uuid: &Uuid,
|
publisher_uuid: &Uuid,
|
||||||
collab_data: &[u8],
|
collab_data: &[u8],
|
||||||
) -> Result<(), AppError> {
|
) -> Result<(), AppError> {
|
||||||
check_workspace_owner_or_publisher(pg_pool, user_uuid, workspace_id, doc_name).await?;
|
check_workspace_owner_or_publisher(pg_pool, publisher_uuid, workspace_id, doc_name).await?;
|
||||||
check_collab_doc_name(doc_name).await?;
|
|
||||||
insert_or_replace_published_collab_blob(pg_pool, workspace_id, doc_name, collab_data).await?;
|
insert_or_replace_published_collab_blob(pg_pool, workspace_id, doc_name, collab_data).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,21 @@
|
||||||
use client_api_test::{generate_unique_registered_user_client, localhost_client};
|
use client_api_test::{generate_unique_registered_user_client, localhost_client};
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn test_get_publish_namespace_not_set() {
|
|
||||||
let (c, _user) = generate_unique_registered_user_client().await;
|
|
||||||
let workspace_id = get_first_workspace_string(&c).await;
|
|
||||||
let err = c
|
|
||||||
.get_workspace_publish_namespace(&workspace_id.to_string())
|
|
||||||
.await
|
|
||||||
.err()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(format!("{:?}", err.code), "PublishNamespaceNotSet");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_set_publish_namespace_set() {
|
async fn test_set_publish_namespace_set() {
|
||||||
let (c, _user) = generate_unique_registered_user_client().await;
|
let (c, _user) = generate_unique_registered_user_client().await;
|
||||||
let workspace_id = get_first_workspace_string(&c).await;
|
let workspace_id = get_first_workspace_string(&c).await;
|
||||||
|
|
||||||
|
{
|
||||||
|
// cannot get namespace if not set
|
||||||
|
let err = c
|
||||||
|
.get_workspace_publish_namespace(&workspace_id.to_string())
|
||||||
|
.await
|
||||||
|
.err()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(format!("{:?}", err.code), "PublishNamespaceNotSet");
|
||||||
|
}
|
||||||
|
|
||||||
let namespace = uuid::Uuid::new_v4().to_string();
|
let namespace = uuid::Uuid::new_v4().to_string();
|
||||||
c.set_workspace_publish_namespace(&workspace_id.to_string(), &namespace)
|
c.set_workspace_publish_namespace(&workspace_id.to_string(), &namespace)
|
||||||
.await
|
.await
|
||||||
|
|
@ -79,9 +78,10 @@ async fn test_publish_doc() {
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
let my_doc_name = "my-doc";
|
||||||
c.publish_collab(
|
c.publish_collab(
|
||||||
&workspace_id,
|
&workspace_id,
|
||||||
"my_doc",
|
my_doc_name,
|
||||||
Metadata {
|
Metadata {
|
||||||
title: "my_title".to_string(),
|
title: "my_title".to_string(),
|
||||||
},
|
},
|
||||||
|
|
@ -91,18 +91,32 @@ async fn test_publish_doc() {
|
||||||
|
|
||||||
{
|
{
|
||||||
// Non login user should be able to view the published collab metadata
|
// Non login user should be able to view the published collab metadata
|
||||||
let non_login = localhost_client();
|
let guest_client = localhost_client();
|
||||||
let published_collab = non_login
|
let published_collab = guest_client
|
||||||
.get_published_collab::<Metadata>(&my_namespace, "my_doc")
|
.get_published_collab::<Metadata>(&my_namespace, my_doc_name)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(published_collab.title, "my_title");
|
assert_eq!(published_collab.title, "my_title");
|
||||||
|
|
||||||
let collab_data = non_login
|
let collab_data = guest_client
|
||||||
.get_published_collab_blob(&my_namespace, "my_doc")
|
.get_published_collab_blob(&my_namespace, my_doc_name)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(collab_data.is_empty());
|
assert!(collab_data.is_empty()); // empty data because publisher need to set it
|
||||||
|
}
|
||||||
|
|
||||||
|
c.put_published_collab_blob(&workspace_id, my_doc_name, "some_collab_data")
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
{
|
||||||
|
// Non login user should be able to view the published collab data
|
||||||
|
let guest_client = localhost_client();
|
||||||
|
let collab_data = guest_client
|
||||||
|
.get_published_collab_blob(&my_namespace, my_doc_name)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert!(collab_data == "some_collab_data");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue