AppFlowy/rust-lib/flowy-user/src/services/server/server_api.rs

106 lines
3.3 KiB
Rust
Raw Normal View History

use crate::{
entities::{SignInParams, SignInResponse, SignUpParams, SignUpResponse, UserDetail},
2021-09-01 08:37:46 +00:00
errors::UserError,
};
2021-09-01 08:37:46 +00:00
use crate::entities::UpdateUserParams;
2021-08-31 15:01:46 +00:00
use flowy_infra::future::ResultFuture;
use flowy_net::{config::*, request::HttpRequestBuilder};
2021-09-01 08:08:32 +00:00
use std::sync::Arc;
pub type Server = Arc<dyn UserServerAPI + Send + Sync>;
2021-08-29 14:00:42 +00:00
pub trait UserServerAPI {
fn sign_up(&self, params: SignUpParams) -> ResultFuture<SignUpResponse, UserError>;
fn sign_in(&self, params: SignInParams) -> ResultFuture<SignInResponse, UserError>;
2021-08-31 09:25:08 +00:00
fn sign_out(&self, token: &str) -> ResultFuture<(), UserError>;
2021-09-01 08:37:46 +00:00
fn update_user(&self, token: &str, params: UpdateUserParams) -> ResultFuture<(), UserError>;
2021-08-31 15:01:46 +00:00
fn get_user_detail(&self, token: &str) -> ResultFuture<UserDetail, UserError>;
}
2021-08-29 14:00:42 +00:00
pub struct UserServer {}
impl UserServer {
pub fn new() -> Self { Self {} }
}
2021-08-29 14:00:42 +00:00
impl UserServerAPI for UserServer {
fn sign_up(&self, params: SignUpParams) -> ResultFuture<SignUpResponse, UserError> {
ResultFuture::new(async move { user_sign_up(params, SIGN_UP_URL.as_ref()).await })
}
2021-08-23 15:02:42 +00:00
fn sign_in(&self, params: SignInParams) -> ResultFuture<SignInResponse, UserError> {
ResultFuture::new(async move { user_sign_in(params, SIGN_IN_URL.as_ref()).await })
}
2021-08-31 15:01:46 +00:00
fn sign_out(&self, token: &str) -> ResultFuture<(), UserError> {
2021-09-01 08:37:46 +00:00
let token = token.to_owned();
2021-08-31 15:01:46 +00:00
ResultFuture::new(async move {
2021-09-01 08:37:46 +00:00
let _ = user_sign_out(&token, SIGN_OUT_URL.as_ref()).await;
2021-08-31 15:01:46 +00:00
Ok(())
})
}
2021-09-01 08:37:46 +00:00
fn update_user(&self, token: &str, params: UpdateUserParams) -> ResultFuture<(), UserError> {
let token = token.to_owned();
ResultFuture::new(async move {
update_user_detail(&token, params, USER_PROFILE_URL.as_ref()).await
})
2021-09-01 08:08:32 +00:00
}
2021-08-31 15:01:46 +00:00
fn get_user_detail(&self, token: &str) -> ResultFuture<UserDetail, UserError> {
2021-09-01 08:08:32 +00:00
let token = token.to_owned();
2021-09-01 08:37:46 +00:00
ResultFuture::new(async move { get_user_detail(&token, USER_PROFILE_URL.as_ref()).await })
}
}
pub async fn user_sign_up(params: SignUpParams, url: &str) -> Result<SignUpResponse, UserError> {
let response = HttpRequestBuilder::post(&url.to_owned())
.protobuf(params)?
.send()
.await?
2021-08-25 09:34:20 +00:00
.response()
.await?;
Ok(response)
}
pub async fn user_sign_in(params: SignInParams, url: &str) -> Result<SignInResponse, UserError> {
let response = HttpRequestBuilder::post(&url.to_owned())
.protobuf(params)?
.send()
.await?
2021-08-25 09:34:20 +00:00
.response()
.await?;
Ok(response)
}
2021-08-31 09:25:08 +00:00
2021-09-01 08:37:46 +00:00
pub async fn user_sign_out(token: &str, url: &str) -> Result<(), UserError> {
2021-08-31 09:25:08 +00:00
let _ = HttpRequestBuilder::delete(&url.to_owned())
2021-09-01 08:37:46 +00:00
.header(HEADER_TOKEN, token)
2021-08-31 09:25:08 +00:00
.send()
.await?;
Ok(())
}
2021-08-31 09:56:38 +00:00
2021-09-01 08:08:32 +00:00
pub async fn get_user_detail(token: &str, url: &str) -> Result<UserDetail, UserError> {
2021-08-31 09:56:38 +00:00
let user_detail = HttpRequestBuilder::get(&url.to_owned())
2021-09-01 08:08:32 +00:00
.header(HEADER_TOKEN, token)
2021-08-31 09:56:38 +00:00
.send()
.await?
.response()
.await?;
Ok(user_detail)
}
2021-09-01 08:08:32 +00:00
pub async fn update_user_detail(
token: &str,
params: UpdateUserParams,
url: &str,
) -> Result<(), UserError> {
let _ = HttpRequestBuilder::patch(&url.to_owned())
.header(HEADER_TOKEN, token)
.protobuf(params)?
.send()
.await?;
Ok(())
}