Skip to content

Commit

Permalink
refactor: remove rust-s3, sqlx from client-api (#245)
Browse files Browse the repository at this point in the history
* refactor: remove rust-s3, sqlx from client-api

* chore: fmt

* chore: add ci check

* chore: add ci check

* chore: add ci check
  • Loading branch information
appflowy authored Jan 4, 2024
1 parent 748d504 commit 8e38ae7
Show file tree
Hide file tree
Showing 25 changed files with 151 additions and 117 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/client_api_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ jobs:
workspaces: |
AppFlowy-Cloud
- name: Check
- name: Install cargo-tree
run: cargo install cargo-tree

- name: Build ClientAPI
working-directory: ./libs/client-api
run: cargo build

- name: Check ClientAPI Dependencies
run: bash ./build/client_api_deps_check.sh

11 changes: 3 additions & 8 deletions Cargo.lock

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

17 changes: 17 additions & 0 deletions build/client_api_deps_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# Generate the current dependency list
cargo tree > current_deps.txt

BASELINE_COUNT=1676
CURRENT_COUNT=$(cat current_deps.txt | wc -l)

echo "Expected dependency count (baseline): $BASELINE_COUNT"
echo "Current dependency count: $CURRENT_COUNT"

if [ "$CURRENT_COUNT" -gt "$BASELINE_COUNT" ]; then
echo "Dependency count has increased!"
exit 1
else
echo "No increase in dependencies."
fi
2 changes: 1 addition & 1 deletion libs/client-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
reqwest = { version = "0.11.20", default-features = false, features = ["json","multipart"] }
reqwest = { version = "0.11.20", default-features = false, features = ["stream","json","multipart"] }
anyhow = "1.0.75"
serde_json.workspace = true
serde_repr = "0.1.17"
Expand Down
20 changes: 10 additions & 10 deletions libs/client-api/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use prost::Message as ProstMessage;
use app_error::AppError;
use bytes::Bytes;
use database_entity::dto::{
AFBlobMetadata, AFBlobRecord, AFCollabMember, AFCollabMembers, AFSnapshotMeta, AFSnapshotMetas,
AFUserProfile, AFUserWorkspaceInfo, AFWorkspace, AFWorkspaceMember, AFWorkspaces,
BatchCreateCollabParams, BatchQueryCollabParams, BatchQueryCollabResult, CollabMemberIdentify,
CollabParams, CreateCollabParams, DeleteCollabParams, InsertCollabMemberParams, QueryCollab,
AFBlobRecord, AFCollabMember, AFCollabMembers, AFSnapshotMeta, AFSnapshotMetas, AFUserProfile,
AFUserWorkspaceInfo, AFWorkspace, AFWorkspaceMember, AFWorkspaces, BatchCreateCollabParams,
BatchQueryCollabParams, BatchQueryCollabResult, CollabMemberIdentify, CollabParams,
CreateCollabParams, DeleteCollabParams, InsertCollabMemberParams, QueryCollab,
QueryCollabMembers, QueryCollabParams, QuerySnapshotParams, SnapshotData,
UpdateCollabMemberParams,
};
Expand All @@ -31,8 +31,8 @@ use reqwest::RequestBuilder;
use shared_entity::dto::auth_dto::SignInTokenResponse;
use shared_entity::dto::auth_dto::UpdateUserParams;
use shared_entity::dto::workspace_dto::{
CreateWorkspaceMembers, WorkspaceBlobMetadata, WorkspaceMemberChangeset, WorkspaceMembers,
WorkspaceSpaceUsage,
BlobMetadata, CreateWorkspaceMembers, RepeatedBlobMetaData, WorkspaceMemberChangeset,
WorkspaceMembers, WorkspaceSpaceUsage,
};
use shared_entity::response::{AppResponse, AppResponseError};
use std::sync::atomic::{AtomicBool, Ordering};
Expand Down Expand Up @@ -1092,15 +1092,15 @@ impl Client {
pub async fn get_blob_metadata<T: AsRef<str>>(
&self,
url: T,
) -> Result<AFBlobMetadata, AppResponseError> {
) -> Result<BlobMetadata, AppResponseError> {
let resp = self
.http_client_with_auth(Method::GET, url.as_ref())
.await?
.send()
.await?;

log_request_id(&resp);
AppResponse::<AFBlobMetadata>::from_response(resp)
AppResponse::<BlobMetadata>::from_response(resp)
.await?
.into_data()
}
Expand Down Expand Up @@ -1134,15 +1134,15 @@ impl Client {
pub async fn get_workspace_all_blob_metadata(
&self,
workspace_id: &str,
) -> Result<WorkspaceBlobMetadata, AppResponseError> {
) -> Result<RepeatedBlobMetaData, AppResponseError> {
let url = format!("{}/api/file_storage/{}/blobs", self.base_url, workspace_id);
let resp = self
.http_client_with_auth(Method::GET, &url)
.await?
.send()
.await?;
log_request_id(&resp);
AppResponse::<WorkspaceBlobMetadata>::from_response(resp)
AppResponse::<RepeatedBlobMetaData>::from_response(resp)
.await?
.into_data()
}
Expand Down
1 change: 0 additions & 1 deletion libs/database-entity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ edition = "2021"
[dependencies]
serde.workspace = true
serde_json.workspace = true
sqlx = { version = "0.7", default-features = false, features = ["macros"] }
collab-entity = { version = "0.1.0" }
validator = { version = "0.16", features = ["validator_derive", "derive"] }
chrono = {version="0.4",features = ["serde"]}
Expand Down
59 changes: 1 addition & 58 deletions libs/database-entity/src/dto.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use crate::pg_row::{AFBlobMetadataRow, AFUserProfileRow, AFWorkspaceRow};
use anyhow::anyhow;
use app_error::AppError;
use chrono::{DateTime, Utc};
use collab_entity::CollabType;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -409,33 +406,6 @@ pub struct AFUserProfile {
pub updated_at: i64,
}

impl TryFrom<AFUserProfileRow> for AFUserProfile {
type Error = AppError;

fn try_from(value: AFUserProfileRow) -> Result<Self, Self::Error> {
let uid = value
.uid
.ok_or(AppError::Internal(anyhow!("Unexpect empty uid")))?;
let uuid = value
.uuid
.ok_or(AppError::Internal(anyhow!("Unexpect empty uuid")))?;
let latest_workspace_id = value.latest_workspace_id.ok_or(AppError::Internal(anyhow!(
"Unexpect empty latest_workspace_id"
)))?;
Ok(Self {
uid,
uuid,
email: value.email,
password: value.password,
name: value.name,
metadata: value.metadata,
encryption_sign: value.encryption_sign,
latest_workspace_id,
updated_at: value.updated_at.map(|v| v.timestamp()).unwrap_or(0),
})
}
}

#[derive(Serialize, Deserialize)]
pub struct AFWorkspace {
pub workspace_id: Uuid,
Expand All @@ -446,31 +416,6 @@ pub struct AFWorkspace {
pub created_at: DateTime<Utc>,
}

impl TryFrom<AFWorkspaceRow> for AFWorkspace {
type Error = AppError;

fn try_from(value: AFWorkspaceRow) -> Result<Self, Self::Error> {
let owner_uid = value
.owner_uid
.ok_or(AppError::Internal(anyhow!("Unexpect empty owner_uid")))?;
let database_storage_id = value
.database_storage_id
.ok_or(AppError::Internal(anyhow!("Unexpect empty workspace_id")))?;

let workspace_name = value.workspace_name.unwrap_or_default();
let created_at = value.created_at.unwrap_or_else(Utc::now);

Ok(Self {
workspace_id: value.workspace_id,
database_storage_id,
owner_uid,
workspace_type: value.workspace_type,
workspace_name,
created_at,
})
}
}

#[derive(Serialize, Deserialize)]
pub struct AFWorkspaces(pub Vec<AFWorkspace>);

Expand All @@ -489,6 +434,4 @@ pub struct AFWorkspaceMember {
pub avatar_url: Option<String>,
}

/// ***************************************************************
/// Make alias for the database entity. Hiding the Sqlx Rows type.
pub type AFBlobMetadata = AFBlobMetadataRow;
// pub type AFBlobMetadata = AFBlobMetadataRow;
2 changes: 0 additions & 2 deletions libs/database-entity/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
pub mod dto;
// pub mod error;
pub mod pg_row;
4 changes: 2 additions & 2 deletions libs/database/src/collab/collab_db_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use database_entity::dto::{
};

use crate::collab::SNAPSHOT_PER_HOUR;
use crate::pg_row::AFCollabMemerAccessLevelRow;
use crate::pg_row::AFSnapshotRow;
use app_error::AppError;
use chrono::{Duration, Utc};
use database_entity::pg_row::AFCollabMemerAccessLevelRow;
use database_entity::pg_row::AFSnapshotRow;
use futures_util::stream::BoxStream;
use sqlx::postgres::PgRow;
use sqlx::{Error, Executor, PgPool, Postgres, Row, Transaction};
Expand Down
2 changes: 1 addition & 1 deletion libs/database/src/file/file_storage.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::file::utils::BlobStreamReader;
use crate::pg_row::AFBlobMetadataRow;
use crate::resource_usage::{
delete_blob_metadata, get_blob_metadata, get_workspace_usage_size, insert_blob_metadata,
is_blob_metadata_exists,
};
use app_error::AppError;
use async_trait::async_trait;
use database_entity::pg_row::AFBlobMetadataRow;
use sqlx::PgPool;
use tokio::io::AsyncRead;
use tracing::{event, instrument};
Expand Down
2 changes: 2 additions & 0 deletions libs/database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ pub mod file;
pub mod resource_usage;
pub mod user;
pub mod workspace;
// pub mod error;
pub mod pg_row;
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::dto::{AFAccessLevel, AFRole};
use anyhow::anyhow;
use app_error::AppError;
use chrono::{DateTime, Utc};
use database_entity::dto::{AFAccessLevel, AFRole, AFUserProfile, AFWorkspace};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;
Expand All @@ -16,6 +18,31 @@ pub struct AFWorkspaceRow {
pub workspace_name: Option<String>,
}

impl TryFrom<AFWorkspaceRow> for AFWorkspace {
type Error = AppError;

fn try_from(value: AFWorkspaceRow) -> Result<Self, Self::Error> {
let owner_uid = value
.owner_uid
.ok_or(AppError::Internal(anyhow!("Unexpect empty owner_uid")))?;
let database_storage_id = value
.database_storage_id
.ok_or(AppError::Internal(anyhow!("Unexpect empty workspace_id")))?;

let workspace_name = value.workspace_name.unwrap_or_default();
let created_at = value.created_at.unwrap_or_else(Utc::now);

Ok(Self {
workspace_id: value.workspace_id,
database_storage_id,
owner_uid,
workspace_type: value.workspace_type,
workspace_name,
created_at,
})
}
}

/// Represent the row of the af_user table
#[derive(Debug, FromRow, Deserialize, Serialize, Clone)]
pub struct AFUserRow {
Expand Down Expand Up @@ -47,6 +74,33 @@ pub struct AFUserProfileRow {
pub latest_workspace_id: Option<Uuid>,
}

impl TryFrom<AFUserProfileRow> for AFUserProfile {
type Error = AppError;

fn try_from(value: AFUserProfileRow) -> Result<Self, Self::Error> {
let uid = value
.uid
.ok_or(AppError::Internal(anyhow!("Unexpect empty uid")))?;
let uuid = value
.uuid
.ok_or(AppError::Internal(anyhow!("Unexpect empty uuid")))?;
let latest_workspace_id = value.latest_workspace_id.ok_or(AppError::Internal(anyhow!(
"Unexpect empty latest_workspace_id"
)))?;
Ok(Self {
uid,
uuid,
email: value.email,
password: value.password,
name: value.name,
metadata: value.metadata,
encryption_sign: value.encryption_sign,
latest_workspace_id,
updated_at: value.updated_at.map(|v| v.timestamp()).unwrap_or(0),
})
}
}

#[derive(FromRow, Serialize, Deserialize)]
pub struct AFWorkspaceMemberPermRow {
pub uid: i64,
Expand Down
2 changes: 1 addition & 1 deletion libs/database/src/resource_usage.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::pg_row::AFBlobMetadataRow;
use app_error::AppError;
use database_entity::pg_row::AFBlobMetadataRow;
use rust_decimal::prelude::ToPrimitive;
use sqlx::types::Decimal;
use sqlx::PgPool;
Expand Down
7 changes: 3 additions & 4 deletions libs/database/src/workspace.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use database_entity::{dto::AFRole, pg_row::AFWorkspaceMemberPermRow};
use database_entity::dto::AFRole;
use futures_util::stream::BoxStream;
use sqlx::{
types::{uuid, Uuid},
Expand All @@ -7,11 +7,10 @@ use sqlx::{
use std::ops::DerefMut;
use tracing::{event, instrument};

use crate::pg_row::AFWorkspaceMemberPermRow;
use crate::pg_row::{AFPermissionRow, AFUserProfileRow, AFWorkspaceMemberRow, AFWorkspaceRow};
use crate::user::select_uid_from_email;
use app_error::AppError;
use database_entity::pg_row::{
AFPermissionRow, AFUserProfileRow, AFWorkspaceMemberRow, AFWorkspaceRow,
};

/// Checks whether a user, identified by a UUID, is an 'Owner' of a workspace, identified by its
/// workspace_id.
Expand Down
1 change: 0 additions & 1 deletion libs/gotrue-entity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ edition = "2021"
serde.workspace = true
serde_json.workspace = true
anyhow = "1.0.75"
reqwest = "0.11.20"
lazy_static = "1.4.0"
jsonwebtoken = "8.3.0"
app-error = { workspace = true, features = ["gotrue_error"] }
2 changes: 1 addition & 1 deletion libs/realtime/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use database::collab::CollabStorage;
use std::ops::Deref;
use std::time::{Duration, Instant};

use database_entity::pg_row::AFUserNotification;
use database::pg_row::AFUserNotification;
use realtime_entity::user::{AFUserChange, UserMessage};
use tracing::{debug, error, trace};

Expand Down
Loading

0 comments on commit 8e38ae7

Please sign in to comment.