2022-01-10 15:45:59 +00:00
|
|
|
use backend_service::{configuration::*, errors::ServerError, request::HttpRequestBuilder};
|
|
|
|
use flowy_error::FlowyError;
|
2022-01-13 02:53:30 +00:00
|
|
|
use flowy_user::module::UserCloudService;
|
2022-01-10 15:45:59 +00:00
|
|
|
use flowy_user_data_model::entities::{
|
2022-01-24 09:35:58 +00:00
|
|
|
SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserParams, UserProfile,
|
2022-01-10 15:45:59 +00:00
|
|
|
};
|
2022-01-13 02:53:30 +00:00
|
|
|
use lib_infra::future::FutureResult;
|
2022-01-10 15:45:59 +00:00
|
|
|
|
|
|
|
pub struct UserHttpCloudService {
|
|
|
|
config: ClientServerConfiguration,
|
|
|
|
}
|
|
|
|
impl UserHttpCloudService {
|
2022-01-24 09:35:58 +00:00
|
|
|
pub fn new(config: &ClientServerConfiguration) -> Self {
|
|
|
|
Self { config: config.clone() }
|
|
|
|
}
|
2022-01-10 15:45:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
impl UserCloudService for UserHttpCloudService {
|
|
|
|
fn sign_up(&self, params: SignUpParams) -> FutureResult<SignUpResponse, FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let url = self.config.sign_up_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let resp = user_sign_up_request(params, &url).await?;
|
|
|
|
Ok(resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn sign_in(&self, params: SignInParams) -> FutureResult<SignInResponse, FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let url = self.config.sign_in_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let resp = user_sign_in_request(params, &url).await?;
|
|
|
|
Ok(resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn sign_out(&self, token: &str) -> FutureResult<(), FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let token = token.to_owned();
|
|
|
|
let url = self.config.sign_out_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let _ = user_sign_out_request(&token, &url).await;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn update_user(&self, token: &str, params: UpdateUserParams) -> FutureResult<(), FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let token = token.to_owned();
|
|
|
|
let url = self.config.user_profile_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let _ = update_user_profile_request(&token, params, &url).await?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:53:30 +00:00
|
|
|
fn get_user(&self, token: &str) -> FutureResult<UserProfile, FlowyError> {
|
2022-01-10 15:45:59 +00:00
|
|
|
let token = token.to_owned();
|
|
|
|
let url = self.config.user_profile_url();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let profile = get_user_profile_request(&token, &url).await?;
|
|
|
|
Ok(profile)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-24 09:35:58 +00:00
|
|
|
fn ws_addr(&self) -> String {
|
|
|
|
self.config.ws_addr()
|
|
|
|
}
|
2022-01-10 15:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn user_sign_up_request(params: SignUpParams, url: &str) -> Result<SignUpResponse, ServerError> {
|
|
|
|
let response = request_builder()
|
|
|
|
.post(&url.to_owned())
|
|
|
|
.protobuf(params)?
|
|
|
|
.response()
|
|
|
|
.await?;
|
|
|
|
Ok(response)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn user_sign_in_request(params: SignInParams, url: &str) -> Result<SignInResponse, ServerError> {
|
|
|
|
let response = request_builder()
|
|
|
|
.post(&url.to_owned())
|
|
|
|
.protobuf(params)?
|
|
|
|
.response()
|
|
|
|
.await?;
|
|
|
|
Ok(response)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn user_sign_out_request(token: &str, url: &str) -> Result<(), ServerError> {
|
|
|
|
let _ = request_builder()
|
|
|
|
.delete(&url.to_owned())
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_user_profile_request(token: &str, url: &str) -> Result<UserProfile, ServerError> {
|
|
|
|
let user_profile = request_builder()
|
|
|
|
.get(&url.to_owned())
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.response()
|
|
|
|
.await?;
|
|
|
|
Ok(user_profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_user_profile_request(token: &str, params: UpdateUserParams, url: &str) -> Result<(), ServerError> {
|
|
|
|
let _ = request_builder()
|
|
|
|
.patch(&url.to_owned())
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.protobuf(params)?
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-24 09:35:58 +00:00
|
|
|
fn request_builder() -> HttpRequestBuilder {
|
|
|
|
HttpRequestBuilder::new()
|
|
|
|
}
|