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.

53 lines
1.6KB

  1. use reqwest::{Client, Error};
  2. use url::{Url};
  3. use crate::robocraft::{ITokenProvider, DefaultTokenProvider};
  4. use crate::robocraft2::{SearchPayload, SearchResponse};
  5. /// Community Factory Robot 2 root URL
  6. pub const FACTORY_DOMAIN: &str = "https://factory.production.robocraft2.com";
  7. /// CRF API implementation
  8. pub struct FactoryAPI {
  9. client: Client,
  10. token: Box<dyn ITokenProvider>,
  11. }
  12. impl FactoryAPI {
  13. /// Create a new instance, using `DefaultTokenProvider`.
  14. pub fn new() -> FactoryAPI {
  15. FactoryAPI {
  16. client: Client::new(),
  17. token: Box::new(DefaultTokenProvider{}),
  18. }
  19. }
  20. /// Create a new instance using the provided token provider.
  21. pub fn with_auth(token_provider: Box<dyn ITokenProvider>) -> FactoryAPI {
  22. FactoryAPI {
  23. client: Client::new(),
  24. token: token_provider,
  25. }
  26. }
  27. /// Retrieve CRF robots on the main page.
  28. ///
  29. /// For searching, use `list_builder()` instead.
  30. pub async fn list(&self) -> Result<SearchResponse, Error> {
  31. let url = Url::parse(FACTORY_DOMAIN)
  32. .unwrap()
  33. .join("/v1/foundry/search")
  34. .unwrap();
  35. let mut request_builder = self.client.get(url);
  36. if let Ok(token) = self.token.token() {
  37. request_builder = request_builder.header("Authorization", "Bearer ".to_owned() + &token);
  38. }
  39. let result = request_builder.send().await?;
  40. result.json::<SearchResponse>().await
  41. }
  42. async fn search(&self, params: SearchPayload) -> Result<SearchResponse, Error> {
  43. todo!()
  44. }
  45. }