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.

47 lines
1.4KB

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