Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * main.cpp
- *
- * Created on: Mar 25, 2017
- * Author: vegorov
- */
- #include <cpprest/http_client.h>
- #include <pplx/pplxtasks.h>
- #include <iostream>
- #include <string>
- using web::http::methods;
- using web::http::client::http_client;
- using web::http::http_response;
- using web::http::http_request;
- using web::json::value;
- using pplx::task;
- #define API_KEY "RGAPI-*-*-*-*-*"
- class LoLAPI{
- public:
- LoLAPI() = delete;
- LoLAPI(const std::string& api_key):
- _api_key(api_key),
- _client(http_client(U("https://ru.api.riotgames.com/"))){
- }
- bool getIdByName(const std::string& summonerName, int& id){
- http_request request(methods::GET);
- request.set_request_uri(std::string("/api/lol/RU/v1.4/summoner/by-name/") + summonerName + "?api_key=" + API_KEY);
- auto test = _client.request(request).then([&id](http_response response){
- if (response.status_code() == 200){
- web::json::object summoners = response.extract_json().get().as_object();
- for (auto it = summoners.begin(); it != summoners.end(); it++){
- id = it->second.at("id").as_integer();
- }
- return true;
- }else{
- return false;
- }
- });
- test.wait();
- if (test.get()){
- return true;
- }else{
- return false;
- }
- }
- private:
- std::string _api_key;
- http_client _client;
- };
- int main(){
- LoLAPI api(API_KEY);
- int id = -1;
- auto names = std::initializer_list<std::string>{"ddimir", "Liegeois", "NFS%20Lee%20Sin", "faker"};
- for (const auto& name:names){
- if (api.getIdByName(name, id)){
- std::cout << name <<" id is " << id << std::endl;
- }else{
- std::cout << "can't get id for " << name << std::endl;
- }
- }
- return 0;
- }
- Console output:
- ddimir id is 6462296
- Liegeois id is 2541085
- NFS%20Lee%20Sin id is 10510262
- faker id is 5281476
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement