An unofficial collection of APIs used in FreeJam games and mods
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.3KB

  1. use reqwest::{Client, Error};
  2. use url::{Url};
  3. use crate::cardlife::{AuthenticationInfo, AuthenticationPayload, LobbyInfo, LobbyPayload};
  4. const AUTHENTICATION_DOMAIN: &str = "https://live-auth.cardlifegame.com/";
  5. const LOBBY_DOMAIN: &str = "https://live-lobby.cardlifegame.com/";
  6. pub struct LiveAPI {
  7. client: Client,
  8. auth: Option<AuthenticationInfo>,
  9. }
  10. impl LiveAPI {
  11. pub fn new() -> LiveAPI {
  12. LiveAPI {
  13. client: Client::new(),
  14. auth: None,
  15. }
  16. }
  17. pub async fn login_email(email: &str, password: &str) -> Result<LiveAPI, Error> {
  18. let mut instance = LiveAPI::new();
  19. let result = instance.authenticate_email(email, password).await;
  20. if let Ok(response) = result {
  21. instance.auth = Some(response);
  22. return Ok(instance);
  23. } else {
  24. return Err(result.err().unwrap());
  25. }
  26. }
  27. pub async fn authenticate_email(&mut self, email: &str, password: &str) -> Result<AuthenticationInfo, Error> {
  28. let url = Url::parse(AUTHENTICATION_DOMAIN)
  29. .unwrap()
  30. .join("/api/auth/authenticate")
  31. .unwrap();
  32. let payload = AuthenticationPayload {
  33. email_address: email.to_string(),
  34. password: password.to_string()
  35. };
  36. let result = self.client.post(url)
  37. .json(&payload)
  38. .send().await;
  39. if let Ok(response) = result {
  40. let res = response.json::<AuthenticationInfo>().await;
  41. if let Ok(auth) = &res {
  42. self.auth = Some(auth.clone());
  43. }
  44. return res;
  45. }
  46. Err(result.err().unwrap())
  47. }
  48. pub async fn lobbies(&self) -> Result<LobbyInfo, Error> {
  49. let url = Url::parse(LOBBY_DOMAIN)
  50. .unwrap()
  51. .join("/api/client/games")
  52. .unwrap();
  53. let public_id;
  54. if let Some(auth) = &self.auth {
  55. public_id = auth.public_id.clone();
  56. } else {
  57. public_id = "".to_string();
  58. }
  59. let payload = LobbyPayload{public_id};
  60. let result = self.client.post(url).json(&payload).send().await;
  61. if let Ok(response) = result {
  62. return response.json::<LobbyInfo>().await;
  63. }
  64. Err(result.err().unwrap())
  65. }
  66. }