An unofficial collection of APIs used in FreeJam games and mods
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

51 wiersze
1.5KB

  1. use reqwest::{Client, IntoUrl, Error};
  2. use url::{Origin, Url};
  3. use crate::cardlife::{GameInfo, StatusInfo};
  4. /// CLre_server web server API implemenation
  5. pub struct CLreServer {
  6. client: Client,
  7. addr: Url,
  8. }
  9. impl CLreServer {
  10. /// Create a new instance
  11. pub fn new<U: IntoUrl>(url: U) -> Result<CLreServer, ()> {
  12. let url_result = url.into_url();
  13. if let Ok(uri) = url_result {
  14. if let Origin::Tuple(scheme, host, port) = uri.origin() {
  15. if let Ok(addr) = Url::parse(&format!("{}://{}:{}", scheme, host.to_string(), port)) {
  16. return Ok(
  17. CLreServer {
  18. client: Client::new(),
  19. addr,
  20. }
  21. );
  22. }
  23. }
  24. }
  25. Err(())
  26. }
  27. /// Retrieve the current game info
  28. pub async fn game_info(&self) -> Result<GameInfo, Error> {
  29. let response = self.client.get(self.addr.join("/c/game.json").unwrap())
  30. .send().await;
  31. if let Ok(resp) = response {
  32. return resp.json::<GameInfo>().await
  33. }
  34. Err(response.err().unwrap())
  35. }
  36. /// Retrieve CLre_server information
  37. pub async fn status_info(&self) -> Result<StatusInfo, Error> {
  38. let response = self.client.get(self.addr.join("/status.json").unwrap())
  39. .send().await;
  40. if let Ok(resp) = response {
  41. return resp.json::<StatusInfo>().await
  42. }
  43. Err(response.err().unwrap())
  44. }
  45. }