Advertisement
dimir

LoLApi test [c++, cassablanca] v2

Mar 24th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. /*
  2.  * main.cpp
  3.  *
  4.  *  Created on: Mar 25, 2017
  5.  *      Author: vegorov
  6.  */
  7.  
  8.  
  9. #include <cpprest/http_client.h>
  10. #include <pplx/pplxtasks.h>
  11. #include <iostream>
  12. #include <string>
  13.  
  14. using web::http::methods;
  15. using web::http::client::http_client;
  16. using web::http::http_response;
  17. using web::http::http_request;
  18. using web::json::value;
  19. using pplx::task;
  20.  
  21. #define API_KEY "RGAPI-*-*-*-*-*"
  22.  
  23. class LoLAPI{
  24. public:
  25.     LoLAPI() = delete;
  26.     LoLAPI(const std::string& api_key):
  27.         _api_key(api_key),
  28.         _client(http_client(U("https://ru.api.riotgames.com/"))){
  29.  
  30.     }
  31.     bool getIdByName(const std::string& summonerName, int& id){
  32.         http_request request(methods::GET);
  33.         request.set_request_uri(std::string("/api/lol/RU/v1.4/summoner/by-name/") + summonerName + "?api_key=" + API_KEY);
  34.         auto test = _client.request(request).then([&id](http_response response){
  35.             if (response.status_code() == 200){
  36.                 web::json::object summoners = response.extract_json().get().as_object();
  37.                 for (auto it = summoners.begin(); it != summoners.end(); it++){
  38.                     id = it->second.at("id").as_integer();
  39.                 }
  40.                 return true;
  41.             }else{
  42.                 return false;
  43.             }
  44.  
  45.         });
  46.         test.wait();
  47.         if (test.get()){
  48.             return true;
  49.         }else{
  50.             return false;
  51.         }
  52.  
  53.     }
  54.  
  55. private:
  56.     std::string _api_key;
  57.     http_client _client;
  58. };
  59.  
  60.  
  61.  
  62. int main(){
  63.     LoLAPI api(API_KEY);
  64.     int id = -1;
  65.     auto names = std::initializer_list<std::string>{"ddimir", "Liegeois", "NFS%20Lee%20Sin", "faker"};
  66.     for (const auto& name:names){
  67.         if (api.getIdByName(name, id)){
  68.             std::cout << name <<" id is " << id << std::endl;
  69.         }else{
  70.             std::cout << "can't get id for " << name << std::endl;
  71.         }
  72.  
  73.     }
  74.     return 0;
  75. }
  76.  
  77. Console output:
  78.  
  79. ddimir id is 6462296
  80. Liegeois id is 2541085
  81. NFS%20Lee%20Sin id is 10510262
  82. faker id is 5281476
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement