chore: refine api

This commit is contained in:
Zack Fu Zi Xiang 2024-06-17 22:55:04 +08:00
parent 3f1832444f
commit 99fc95f33a
No known key found for this signature in database
3 changed files with 96 additions and 38 deletions

View File

@ -1,25 +1,12 @@
use bytes::Bytes;
use database_entity::dto::UpdatePublishNamespace;
use reqwest::Method;
use reqwest::{Body, Method};
use shared_entity::response::{AppResponse, AppResponseError};
use crate::Client;
// Publisher API
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(
&self,
workspace_id: &str,
@ -66,6 +53,62 @@ impl Client {
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>(
&self,
publish_namespace: &str,

View File

@ -157,6 +157,8 @@ pub async fn publish_collab(
publisher_uuid: &Uuid,
metadata: &serde_json::Value,
) -> 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)
.await?;
Ok(())
@ -166,11 +168,10 @@ pub async fn put_published_collab_blob(
pg_pool: &PgPool,
workspace_id: &Uuid,
doc_name: &str,
user_uuid: &Uuid,
publisher_uuid: &Uuid,
collab_data: &[u8],
) -> Result<(), AppError> {
check_workspace_owner_or_publisher(pg_pool, user_uuid, workspace_id, doc_name).await?;
check_collab_doc_name(doc_name).await?;
check_workspace_owner_or_publisher(pg_pool, publisher_uuid, workspace_id, doc_name).await?;
insert_or_replace_published_collab_blob(pg_pool, workspace_id, doc_name, collab_data).await?;
Ok(())
}

View File

@ -1,22 +1,21 @@
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]
async fn test_set_publish_namespace_set() {
let (c, _user) = generate_unique_registered_user_client().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();
c.set_workspace_publish_namespace(&workspace_id.to_string(), &namespace)
.await
@ -79,9 +78,10 @@ async fn test_publish_doc() {
.await
.unwrap();
let my_doc_name = "my-doc";
c.publish_collab(
&workspace_id,
"my_doc",
my_doc_name,
Metadata {
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
let non_login = localhost_client();
let published_collab = non_login
.get_published_collab::<Metadata>(&my_namespace, "my_doc")
let guest_client = localhost_client();
let published_collab = guest_client
.get_published_collab::<Metadata>(&my_namespace, my_doc_name)
.await
.unwrap();
assert_eq!(published_collab.title, "my_title");
let collab_data = non_login
.get_published_collab_blob(&my_namespace, "my_doc")
let collab_data = guest_client
.get_published_collab_blob(&my_namespace, my_doc_name)
.await
.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");
}
}