server_api.rs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. use crate::{
  2. entities::{SignInParams, SignInResponse, SignUpParams, SignUpResponse, UserDetail},
  3. errors::{ErrorBuilder, ErrorCode, UserError},
  4. };
  5. use crate::entities::SignOutParams;
  6. use flowy_net::{config::*, future::ResultFuture, request::HttpRequestBuilder};
  7. pub trait UserServerAPI {
  8. fn sign_up(&self, params: SignUpParams) -> ResultFuture<SignUpResponse, UserError>;
  9. fn sign_in(&self, params: SignInParams) -> ResultFuture<SignInResponse, UserError>;
  10. fn sign_out(&self, token: &str) -> ResultFuture<(), UserError>;
  11. fn get_user_info(&self, user_id: &str) -> ResultFuture<UserDetail, UserError>;
  12. }
  13. pub struct UserServer {}
  14. impl UserServer {
  15. pub fn new() -> Self { Self {} }
  16. }
  17. impl UserServerAPI for UserServer {
  18. fn sign_up(&self, params: SignUpParams) -> ResultFuture<SignUpResponse, UserError> {
  19. ResultFuture::new(async move { user_sign_up(params, SIGN_UP_URL.as_ref()).await })
  20. }
  21. fn sign_in(&self, params: SignInParams) -> ResultFuture<SignInResponse, UserError> {
  22. ResultFuture::new(async move { user_sign_in(params, SIGN_IN_URL.as_ref()).await })
  23. }
  24. fn sign_out(&self, _token: &str) -> ResultFuture<(), UserError> {
  25. ResultFuture::new(async { Err(ErrorBuilder::new(ErrorCode::Unknown).build()) })
  26. }
  27. fn get_user_info(&self, _user_id: &str) -> ResultFuture<UserDetail, UserError> {
  28. ResultFuture::new(async { Err(ErrorBuilder::new(ErrorCode::Unknown).build()) })
  29. }
  30. }
  31. pub async fn user_sign_up(params: SignUpParams, url: &str) -> Result<SignUpResponse, UserError> {
  32. let response = HttpRequestBuilder::post(&url.to_owned())
  33. .protobuf(params)?
  34. .send()
  35. .await?
  36. .response()
  37. .await?;
  38. Ok(response)
  39. }
  40. pub async fn user_sign_in(params: SignInParams, url: &str) -> Result<SignInResponse, UserError> {
  41. let response = HttpRequestBuilder::post(&url.to_owned())
  42. .protobuf(params)?
  43. .send()
  44. .await?
  45. .response()
  46. .await?;
  47. Ok(response)
  48. }
  49. pub async fn user_sign_out(params: SignOutParams, url: &str) -> Result<(), UserError> {
  50. let _ = HttpRequestBuilder::delete(&url.to_owned())
  51. .protobuf(params)?
  52. .send()
  53. .await?;
  54. Ok(())
  55. }