2022-02-07 06:40:45 +00:00
|
|
|
use crate::{
|
2022-01-10 15:45:59 +00:00
|
|
|
configuration::{ClientServerConfiguration, HEADER_TOKEN},
|
|
|
|
request::{HttpRequestBuilder, ResponseMiddleware},
|
|
|
|
};
|
2022-01-27 12:39:54 +00:00
|
|
|
use flowy_error::FlowyError;
|
|
|
|
use flowy_folder_data_model::entities::{
|
2022-01-13 02:53:30 +00:00
|
|
|
app::{App, AppId, CreateAppParams, UpdateAppParams},
|
2022-01-10 15:45:59 +00:00
|
|
|
trash::{RepeatedTrash, RepeatedTrashId},
|
2022-01-13 02:53:30 +00:00
|
|
|
view::{CreateViewParams, RepeatedViewId, UpdateViewParams, View, ViewId},
|
2022-01-10 15:45:59 +00:00
|
|
|
workspace::{CreateWorkspaceParams, RepeatedWorkspace, UpdateWorkspaceParams, Workspace, WorkspaceId},
|
|
|
|
};
|
|
|
|
|
2022-01-30 02:33:21 +00:00
|
|
|
use flowy_folder::event_map::FolderCouldServiceV1;
|
2022-02-07 06:40:45 +00:00
|
|
|
use http_flowy::errors::ServerError;
|
|
|
|
use http_flowy::response::FlowyResponse;
|
2022-01-10 15:45:59 +00:00
|
|
|
use lazy_static::lazy_static;
|
2022-01-13 02:53:30 +00:00
|
|
|
use lib_infra::future::FutureResult;
|
2022-01-10 15:45:59 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::sync::broadcast;
|
|
|
|
|
2022-02-07 02:37:01 +00:00
|
|
|
pub struct FolderHttpCloudService {
|
2022-01-10 15:45:59 +00:00
|
|
|
config: ClientServerConfiguration,
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:37:01 +00:00
|
|
|
impl FolderHttpCloudService {
|
|
|
|
pub fn new(config: ClientServerConfiguration) -> FolderHttpCloudService {
|
2022-01-24 09:35:58 +00:00
|
|
|
Self { config }
|
|
|
|
}
|
2022-01-10 15:45:59 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:37:01 +00:00
|
|
|
impl FolderCouldServiceV1 for FolderHttpCloudService {
|
2022-01-13 02:53:30 +00:00
|
|
|
fn init(&self) {}
|
2022-01-10 15:45:59 +00:00
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn create_workspace(&self, token: &str, params: CreateWorkspaceParams) -> FutureResult<Workspace, FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let token = token.to_owned();
|
|
|
|
let url = self.config.workspace_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let workspace = create_workspace_request(&token, params, &url).await?;
|
|
|
|
Ok(workspace)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn read_workspace(&self, token: &str, params: WorkspaceId) -> FutureResult<RepeatedWorkspace, FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let token = token.to_owned();
|
|
|
|
let url = self.config.workspace_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let repeated_workspace = read_workspaces_request(&token, params, &url).await?;
|
|
|
|
Ok(repeated_workspace)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn update_workspace(&self, token: &str, params: UpdateWorkspaceParams) -> FutureResult<(), FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let token = token.to_owned();
|
|
|
|
let url = self.config.workspace_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let _ = update_workspace_request(&token, params, &url).await?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn delete_workspace(&self, token: &str, params: WorkspaceId) -> FutureResult<(), FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let token = token.to_owned();
|
|
|
|
let url = self.config.workspace_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let _ = delete_workspace_request(&token, params, &url).await?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn create_view(&self, token: &str, params: CreateViewParams) -> FutureResult<View, FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let token = token.to_owned();
|
|
|
|
let url = self.config.view_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let view = create_view_request(&token, params, &url).await?;
|
|
|
|
Ok(view)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn read_view(&self, token: &str, params: ViewId) -> FutureResult<Option<View>, FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let token = token.to_owned();
|
|
|
|
let url = self.config.view_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let view = read_view_request(&token, params, &url).await?;
|
|
|
|
Ok(view)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn delete_view(&self, token: &str, params: RepeatedViewId) -> FutureResult<(), FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let token = token.to_owned();
|
|
|
|
let url = self.config.view_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let _ = delete_view_request(&token, params, &url).await?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn update_view(&self, token: &str, params: UpdateViewParams) -> FutureResult<(), FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let token = token.to_owned();
|
|
|
|
let url = self.config.view_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let _ = update_view_request(&token, params, &url).await?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn create_app(&self, token: &str, params: CreateAppParams) -> FutureResult<App, FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let token = token.to_owned();
|
|
|
|
let url = self.config.app_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let app = create_app_request(&token, params, &url).await?;
|
|
|
|
Ok(app)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn read_app(&self, token: &str, params: AppId) -> FutureResult<Option<App>, FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let token = token.to_owned();
|
|
|
|
let url = self.config.app_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let app = read_app_request(&token, params, &url).await?;
|
|
|
|
Ok(app)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn update_app(&self, token: &str, params: UpdateAppParams) -> FutureResult<(), FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let token = token.to_owned();
|
|
|
|
let url = self.config.app_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let _ = update_app_request(&token, params, &url).await?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn delete_app(&self, token: &str, params: AppId) -> FutureResult<(), FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let token = token.to_owned();
|
|
|
|
let url = self.config.app_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let _ = delete_app_request(&token, params, &url).await?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn create_trash(&self, token: &str, params: RepeatedTrashId) -> FutureResult<(), FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let token = token.to_owned();
|
|
|
|
let url = self.config.trash_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let _ = create_trash_request(&token, params, &url).await?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn delete_trash(&self, token: &str, params: RepeatedTrashId) -> FutureResult<(), FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let token = token.to_owned();
|
|
|
|
let url = self.config.trash_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let _ = delete_trash_request(&token, params, &url).await?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn read_trash(&self, token: &str) -> FutureResult<RepeatedTrash, FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let token = token.to_owned();
|
|
|
|
let url = self.config.trash_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let repeated_trash = read_trash_request(&token, &url).await?;
|
|
|
|
Ok(repeated_trash)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 09:35:58 +00:00
|
|
|
fn request_builder() -> HttpRequestBuilder {
|
|
|
|
HttpRequestBuilder::new().middleware(MIDDLEWARE.clone())
|
|
|
|
}
|
2022-01-10 15:45:59 +00:00
|
|
|
|
|
|
|
pub async fn create_workspace_request(
|
|
|
|
token: &str,
|
|
|
|
params: CreateWorkspaceParams,
|
|
|
|
url: &str,
|
|
|
|
) -> Result<Workspace, ServerError> {
|
|
|
|
let workspace = request_builder()
|
|
|
|
.post(&url.to_owned())
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.protobuf(params)?
|
|
|
|
.response()
|
|
|
|
.await?;
|
|
|
|
Ok(workspace)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn read_workspaces_request(
|
|
|
|
token: &str,
|
|
|
|
params: WorkspaceId,
|
|
|
|
url: &str,
|
|
|
|
) -> Result<RepeatedWorkspace, ServerError> {
|
|
|
|
let repeated_workspace = request_builder()
|
|
|
|
.get(&url.to_owned())
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.protobuf(params)?
|
|
|
|
.response::<RepeatedWorkspace>()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(repeated_workspace)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_workspace_request(
|
|
|
|
token: &str,
|
|
|
|
params: UpdateWorkspaceParams,
|
|
|
|
url: &str,
|
|
|
|
) -> Result<(), ServerError> {
|
|
|
|
let _ = request_builder()
|
|
|
|
.patch(&url.to_owned())
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.protobuf(params)?
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn delete_workspace_request(token: &str, params: WorkspaceId, url: &str) -> Result<(), ServerError> {
|
|
|
|
let _ = request_builder()
|
|
|
|
.delete(url)
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.protobuf(params)?
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// App
|
|
|
|
pub async fn create_app_request(token: &str, params: CreateAppParams, url: &str) -> Result<App, ServerError> {
|
|
|
|
let app = request_builder()
|
|
|
|
.post(&url.to_owned())
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.protobuf(params)?
|
|
|
|
.response()
|
|
|
|
.await?;
|
|
|
|
Ok(app)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn read_app_request(token: &str, params: AppId, url: &str) -> Result<Option<App>, ServerError> {
|
|
|
|
let app = request_builder()
|
|
|
|
.get(&url.to_owned())
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.protobuf(params)?
|
|
|
|
.option_response()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(app)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_app_request(token: &str, params: UpdateAppParams, url: &str) -> Result<(), ServerError> {
|
|
|
|
let _ = request_builder()
|
|
|
|
.patch(&url.to_owned())
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.protobuf(params)?
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn delete_app_request(token: &str, params: AppId, url: &str) -> Result<(), ServerError> {
|
|
|
|
let _ = request_builder()
|
|
|
|
.delete(&url.to_owned())
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.protobuf(params)?
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// View
|
|
|
|
pub async fn create_view_request(token: &str, params: CreateViewParams, url: &str) -> Result<View, ServerError> {
|
|
|
|
let view = request_builder()
|
|
|
|
.post(&url.to_owned())
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.protobuf(params)?
|
|
|
|
.response()
|
|
|
|
.await?;
|
|
|
|
Ok(view)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn read_view_request(token: &str, params: ViewId, url: &str) -> Result<Option<View>, ServerError> {
|
|
|
|
let view = request_builder()
|
|
|
|
.get(&url.to_owned())
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.protobuf(params)?
|
|
|
|
.option_response()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(view)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_view_request(token: &str, params: UpdateViewParams, url: &str) -> Result<(), ServerError> {
|
|
|
|
let _ = request_builder()
|
|
|
|
.patch(&url.to_owned())
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.protobuf(params)?
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn delete_view_request(token: &str, params: RepeatedViewId, url: &str) -> Result<(), ServerError> {
|
|
|
|
let _ = request_builder()
|
|
|
|
.delete(&url.to_owned())
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.protobuf(params)?
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_trash_request(token: &str, params: RepeatedTrashId, url: &str) -> Result<(), ServerError> {
|
|
|
|
let _ = request_builder()
|
|
|
|
.post(&url.to_owned())
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.protobuf(params)?
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn delete_trash_request(token: &str, params: RepeatedTrashId, url: &str) -> Result<(), ServerError> {
|
|
|
|
let _ = request_builder()
|
|
|
|
.delete(&url.to_owned())
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.protobuf(params)?
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn read_trash_request(token: &str, url: &str) -> Result<RepeatedTrash, ServerError> {
|
|
|
|
let repeated_trash = request_builder()
|
|
|
|
.get(&url.to_owned())
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.response::<RepeatedTrash>()
|
|
|
|
.await?;
|
|
|
|
Ok(repeated_trash)
|
|
|
|
}
|
2022-01-13 02:53:30 +00:00
|
|
|
|
|
|
|
lazy_static! {
|
2022-02-07 06:40:45 +00:00
|
|
|
static ref MIDDLEWARE: Arc<FolderResponseMiddleware> = Arc::new(FolderResponseMiddleware::new());
|
2022-01-13 02:53:30 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 06:40:45 +00:00
|
|
|
pub struct FolderResponseMiddleware {
|
2022-01-13 02:53:30 +00:00
|
|
|
invalid_token_sender: broadcast::Sender<String>,
|
|
|
|
}
|
|
|
|
|
2022-02-07 06:40:45 +00:00
|
|
|
impl FolderResponseMiddleware {
|
2022-01-13 02:53:30 +00:00
|
|
|
fn new() -> Self {
|
|
|
|
let (sender, _) = broadcast::channel(10);
|
2022-02-07 06:40:45 +00:00
|
|
|
FolderResponseMiddleware {
|
2022-01-13 02:53:30 +00:00
|
|
|
invalid_token_sender: sender,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2022-01-24 09:35:58 +00:00
|
|
|
fn invalid_token_subscribe(&self) -> broadcast::Receiver<String> {
|
|
|
|
self.invalid_token_sender.subscribe()
|
|
|
|
}
|
2022-01-13 02:53:30 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 06:40:45 +00:00
|
|
|
impl ResponseMiddleware for FolderResponseMiddleware {
|
2022-01-13 02:53:30 +00:00
|
|
|
fn receive_response(&self, token: &Option<String>, response: &FlowyResponse) {
|
|
|
|
if let Some(error) = &response.error {
|
|
|
|
if error.is_unauthorized() {
|
|
|
|
tracing::error!("user is unauthorized");
|
|
|
|
match token {
|
2022-01-24 09:35:58 +00:00
|
|
|
None => {}
|
2022-01-13 02:53:30 +00:00
|
|
|
Some(token) => match self.invalid_token_sender.send(token.clone()) {
|
2022-01-24 09:35:58 +00:00
|
|
|
Ok(_) => {}
|
2022-01-13 02:53:30 +00:00
|
|
|
Err(e) => tracing::error!("{:?}", e),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|