Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. impl<T> Future for CacheFuture<T>
  2. where
  3. T: Debug + DeserializeOwned,
  4. {
  5. type Item = T;
  6. type Error = ClientError;
  7.  
  8. fn poll(&mut self) -> Result<Async<Self::Item>, Self::Error> {
  9. let cache = self.cache.lock().unwrap();
  10. let url = self.url.clone();
  11. let url2 = url.clone();
  12. let req = match &self.api_key {
  13. //Riot api request
  14. Some(api_key) => {
  15. dbg!("got api key: {}", api_key);
  16. Request::builder()
  17. .header("X-Riot-Token", HeaderValue::from_str(api_key).unwrap())
  18. .uri(url)
  19. .body(Body::from(""))
  20. .unwrap()
  21. }
  22. //DDragon request
  23. None => Request::builder().uri(url).body(Body::from("")).unwrap(),
  24. };
  25. match cache.get(&url2) {
  26. Some(resp) => {
  27. let deserialized: T = serde_json::from_str(resp).unwrap();
  28. return Ok(Async::Ready(deserialized));
  29. }
  30. None => {
  31. dbg!("got no cache");
  32. dbg!("beginning poll");
  33. match self.client.request(req).poll() {
  34. Ok(Async::Ready(resp)) => {
  35. dbg!("ready");
  36. let body = resp.into_body();
  37. match body.concat2().poll() {
  38. Ok(Async::Ready(chunk)) => {
  39. dbg!("ready");
  40. let string_resp = String::from_utf8(chunk.to_vec()).unwrap();
  41. let deserialized: T = serde_json::from_str(&string_resp).unwrap();
  42. return Ok(Async::Ready(deserialized));
  43. }
  44. Ok(Async::NotReady) => {
  45. dbg!("not ready");
  46. return Ok(Async::NotReady);
  47. }
  48. Err(e) => {
  49. dbg!(&e);
  50. return Err(ClientError::Other { source: e });
  51. }
  52. }
  53. }
  54. Ok(Async::NotReady) => {
  55. dbg!("not ready");
  56. return Ok(Async::NotReady);
  57. }
  58. Err(e) => {
  59. dbg!(&e);
  60. return Err(ClientError::Other { source: e });
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement