Advertisement
GrandaThePanda

REST mini app

Aug 7th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include "cpprest/http_client.h"
  2. #include "cpprest/filestream.h"
  3. #include "cpprest/json.h"
  4. #include "string"
  5.  
  6. using namespace utility;
  7. using namespace web;
  8. using namespace web::http;
  9. using namespace web::http::client;
  10. using namespace concurrency::streams;
  11. using namespace web::json;
  12.  
  13.  
  14.  
  15. pplx::task<void> WoWRealmCheck(std::string& realm) {
  16.     uri_builder uribuild(U"http://us.battle.net");
  17.     uribuild.append_path(U"/api/wow/realm/status?locale=en_US");
  18.     auto uri = uribuild.to_uri();
  19.     std::cout << uri.to_string() << std::endl;
  20.     http_client client(uri);
  21.     return client.request(methods::GET)
  22.         .then([](http_response response) -> pplx::task<json::value>
  23.     {
  24.         std::cout << "Received response code: " << response.status_code() << std::endl;
  25.         if (response.status_code() == status_codes::OK)
  26.             return response.extract_json();
  27.         else
  28.             return pplx::task_from_result(web::json::value());
  29.  
  30.     })
  31.         .then([realm](pplx::task<web::json::value> jsonResponse)
  32.     {
  33.         try
  34.         {
  35.             bool found = false;
  36.             const web::json::value v = jsonResponse.get();
  37.             const web::json::value realms = v.at(U"realms");
  38.             for (size_t i = 0; i < realms.size(); i++)
  39.             {
  40.                 if (realm.compare(realms.at(i).at(U"name").as_string()) == 0) {
  41.                     found = true;
  42.                     std::cout << "------------------------------------------------------\n";
  43.                     std::cout << realms.at(i).at(U"name").as_string() << std::endl
  44.                         << "Status: " << realms.at(i).at(U"status").as_bool() << std::endl
  45.                         << "Population: " << realms.at(i).at(U"population").as_string() << std::endl
  46.                         << "-------------------------------------------------------\n";
  47.                 }
  48.             }
  49.             if (found == false) std::cout << "Realm not found perhaps try again? \n";
  50.  
  51.            
  52.         }
  53.         catch (const web::http::http_exception& e)
  54.         {
  55.             std::cout << e.what();
  56.         }
  57.     });
  58. };
  59.  
  60. int main() {
  61.     while (true) {
  62.         char input[30];
  63.         std::string realmName;
  64.         std::cout << "Enter realm name: ";
  65.         std::cin.getline(input, 30);
  66.         realmName = input;
  67.         std::cout << "Checking current status of realm...\n";
  68.         WoWRealmCheck(realmName).wait();
  69.     }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement