server_api.rs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. use crate::{
  2. entities::{SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserParams, UserProfile},
  3. errors::FlowyError,
  4. services::server::UserServerAPI,
  5. };
  6. use backend_service::{configuration::*, http_request::*};
  7. use lib_infra::future::FutureResult;
  8. pub struct UserHttpServer {
  9. config: ClientServerConfiguration,
  10. }
  11. impl UserHttpServer {
  12. pub fn new(config: ClientServerConfiguration) -> Self {
  13. Self { config }
  14. }
  15. }
  16. impl UserServerAPI for UserHttpServer {
  17. fn sign_up(&self, params: SignUpParams) -> FutureResult<SignUpResponse, FlowyError> {
  18. let url = self.config.sign_up_url();
  19. FutureResult::new(async move {
  20. let resp = user_sign_up_request(params, &url).await?;
  21. Ok(resp)
  22. })
  23. }
  24. fn sign_in(&self, params: SignInParams) -> FutureResult<SignInResponse, FlowyError> {
  25. let url = self.config.sign_in_url();
  26. FutureResult::new(async move {
  27. let resp = user_sign_in_request(params, &url).await?;
  28. Ok(resp)
  29. })
  30. }
  31. fn sign_out(&self, token: &str) -> FutureResult<(), FlowyError> {
  32. let token = token.to_owned();
  33. let url = self.config.sign_out_url();
  34. FutureResult::new(async move {
  35. let _ = user_sign_out_request(&token, &url).await;
  36. Ok(())
  37. })
  38. }
  39. fn update_user(&self, token: &str, params: UpdateUserParams) -> FutureResult<(), FlowyError> {
  40. let token = token.to_owned();
  41. let url = self.config.user_profile_url();
  42. FutureResult::new(async move {
  43. let _ = update_user_profile_request(&token, params, &url).await?;
  44. Ok(())
  45. })
  46. }
  47. fn get_user(&self, token: &str) -> FutureResult<UserProfile, FlowyError> {
  48. let token = token.to_owned();
  49. let url = self.config.user_profile_url();
  50. FutureResult::new(async move {
  51. let profile = get_user_profile_request(&token, &url).await?;
  52. Ok(profile)
  53. })
  54. }
  55. fn ws_addr(&self) -> String {
  56. self.config.ws_addr()
  57. }
  58. }
  59. // use crate::notify::*;
  60. // use backend_service::response::FlowyResponse;
  61. // use flowy_user_data_model::errors::ErrorCode;
  62. // struct Middleware {}
  63. //
  64. //
  65. //
  66. // impl ResponseMiddleware for Middleware {
  67. // fn receive_response(&self, token: &Option<String>, response:
  68. // &FlowyResponse) { if let Some(error) = &response.error {
  69. // if error.is_unauthorized() {
  70. // log::error!("user unauthorized");
  71. // match token {
  72. // None => {},
  73. // Some(token) => {
  74. // let error =
  75. // FlowyError::new(ErrorCode::UserUnauthorized, "");
  76. // dart_notify(token, UserNotification::UserUnauthorized)
  77. // .error(error) .send()
  78. // },
  79. // }
  80. // }
  81. // }
  82. // }
  83. // }