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

90 lines
2.8 KiB
Rust
Raw Normal View History

use crate::{
2021-11-08 02:57:33 +00:00
entities::{SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserParams, UserProfile},
2021-09-01 08:37:46 +00:00
errors::UserError,
2021-11-08 02:57:33 +00:00
services::server::UserServerAPI,
};
2021-11-19 07:00:51 +00:00
use backend_api::user_request::*;
2021-11-08 02:57:33 +00:00
use flowy_net::config::*;
use lib_infra::future::ResultFuture;
2021-09-01 08:08:32 +00:00
2021-09-27 15:23:23 +00:00
pub struct UserServer {
config: ServerConfig,
}
2021-08-29 14:00:42 +00:00
impl UserServer {
2021-09-27 15:23:23 +00:00
pub fn new(config: ServerConfig) -> Self { Self { config } }
}
2021-08-29 14:00:42 +00:00
impl UserServerAPI for UserServer {
fn sign_up(&self, params: SignUpParams) -> ResultFuture<SignUpResponse, UserError> {
2021-09-27 15:23:23 +00:00
let url = self.config.sign_up_url();
2021-11-08 02:57:33 +00:00
ResultFuture::new(async move {
let resp = user_sign_up_request(params, &url).await?;
Ok(resp)
})
}
2021-08-23 15:02:42 +00:00
fn sign_in(&self, params: SignInParams) -> ResultFuture<SignInResponse, UserError> {
2021-09-27 15:23:23 +00:00
let url = self.config.sign_in_url();
2021-11-08 02:57:33 +00:00
ResultFuture::new(async move {
let resp = user_sign_in_request(params, &url).await?;
Ok(resp)
})
}
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-09-27 15:23:23 +00:00
let url = self.config.sign_out_url();
2021-08-31 15:01:46 +00:00
ResultFuture::new(async move {
2021-09-27 15:23:23 +00:00
let _ = user_sign_out_request(&token, &url).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-27 15:23:23 +00:00
let url = self.config.user_profile_url();
2021-11-08 02:57:33 +00:00
ResultFuture::new(async move {
let _ = update_user_profile_request(&token, params, &url).await?;
Ok(())
})
2021-09-01 08:08:32 +00:00
}
2021-09-04 08:53:58 +00:00
fn get_user(&self, token: &str) -> ResultFuture<UserProfile, UserError> {
2021-09-01 08:08:32 +00:00
let token = token.to_owned();
2021-09-27 15:23:23 +00:00
let url = self.config.user_profile_url();
2021-11-08 02:57:33 +00:00
ResultFuture::new(async move {
let profile = get_user_profile_request(&token, &url).await?;
Ok(profile)
})
}
2021-09-27 15:23:23 +00:00
fn ws_addr(&self) -> String { self.config.ws_addr() }
}
2021-11-08 02:57:33 +00:00
// use crate::notify::*;
// use flowy_net::response::FlowyResponse;
// use flowy_user_infra::errors::ErrorCode;
// struct Middleware {}
//
//
//
// impl ResponseMiddleware for Middleware {
// fn receive_response(&self, token: &Option<String>, response:
// &FlowyResponse) { if let Some(error) = &response.error {
// if error.is_unauthorized() {
// log::error!("user unauthorized");
// match token {
// None => {},
// Some(token) => {
// let error =
// UserError::new(ErrorCode::UserUnauthorized, "");
// dart_notify(token, UserNotification::UserUnauthorized)
// .error(error) .send()
// },
// }
// }
// }
// }
// }