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

89 lines
2.9 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 14:50:22 +00:00
use crate::{entities::UpdateUserParams, services::server::UserServerAPI};
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
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> {
2021-09-01 14:50:22 +00:00
ResultFuture::new(async move { user_sign_up_request(params, SIGN_UP_URL.as_ref()).await })
}
2021-08-23 15:02:42 +00:00
fn sign_in(&self, params: SignInParams) -> ResultFuture<SignInResponse, UserError> {
2021-09-01 14:50:22 +00:00
ResultFuture::new(async move { user_sign_in_request(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 14:50:22 +00:00
let _ = user_sign_out_request(&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();
2021-09-01 14:50:22 +00:00
ResultFuture::new(async move { update_user_detail_request(&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 14:50:22 +00:00
ResultFuture::new(async move { get_user_detail_request(&token, USER_PROFILE_URL.as_ref()).await })
}
}
2021-09-01 14:50:22 +00:00
pub async fn user_sign_up_request(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)
}
2021-09-01 14:50:22 +00:00
pub async fn user_sign_in_request(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 14:50:22 +00:00
pub async fn user_sign_out_request(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 14:50:22 +00:00
pub async fn get_user_detail_request(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
2021-09-01 14:50:22 +00:00
pub async fn update_user_detail_request(token: &str, params: UpdateUserParams, url: &str) -> Result<(), UserError> {
2021-09-01 08:08:32 +00:00
let _ = HttpRequestBuilder::patch(&url.to_owned())
.header(HEADER_TOKEN, token)
.protobuf(params)?
.send()
.await?;
Ok(())
}