]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/convert_json.cpp
Add function to get server info.
[dragonfireclient.git] / src / convert_json.cpp
index 917d6fcb31ddd093f24f21ae6cef4907bd39a4fd..e548c45f54ea61a697844b883f53e595832d281f 100644 (file)
@@ -25,57 +25,48 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "mods.h"
 #include "config.h"
 #include "log.h"
-#include "main.h" // for g_settings
 #include "settings.h"
-#include "version.h"
 #include "httpfetch.h"
+#include "porting.h"
 
-Json::Value                 fetchJsonValue(const std::string url,
-                                                                                                       struct curl_slist *chunk) {
-#if USE_CURL
+Json::Value fetchJsonValue(const std::string &url,
+               std::vector<std::string> *extra_headers)
+{
+       HTTPFetchRequest fetch_request;
+       HTTPFetchResult fetch_result;
+       fetch_request.url = url;
+       fetch_request.caller = HTTPFETCH_SYNC;
 
-       HTTPFetchRequest fetchrequest;
-       HTTPFetchResult fetchresult;
-       fetchrequest.url = url;
-       fetchrequest.caller = HTTPFETCH_SYNC;
+       if (extra_headers != NULL)
+               fetch_request.extra_headers = *extra_headers;
 
-       struct curl_slist* runptr = chunk;
-       while(runptr) {
-               fetchrequest.extra_headers.push_back(runptr->data);
-               runptr = runptr->next;
-       }
-       httpfetch_sync(fetchrequest,fetchresult);
+       httpfetch_sync(fetch_request, fetch_result);
 
-       if (!fetchresult.succeeded) {
+       if (!fetch_result.succeeded) {
                return Json::Value();
        }
        Json::Value root;
        Json::Reader reader;
-       std::istringstream stream(fetchresult.data);
+       std::istringstream stream(fetch_result.data);
 
-       if (!reader.parse( stream, root ) )
-       {
+       if (!reader.parse(stream, root)) {
                errorstream << "URL: " << url << std::endl;
                errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages();
-               errorstream << "data: \"" << fetchresult.data << "\"" << std::endl;
+               if (fetch_result.data.size() > 100) {
+                       errorstream << "Data (" << fetch_result.data.size()
+                               << " bytes) printed to warningstream." << std::endl;
+                       warningstream << "data: \"" << fetch_result.data << "\"" << std::endl;
+               } else {
+                       errorstream << "data: \"" << fetch_result.data << "\"" << std::endl;
+               }
                return Json::Value();
        }
 
-       if (root.isArray()) {
-               return root;
-       }
-       if ((root["list"].isArray())) {
-               return root["list"];
-       }
-       else {
-               return root;
-       }
-#endif
-       return Json::Value();
+       return root;
 }
 
 std::vector<ModStoreMod>    readModStoreList(Json::Value& modlist) {
-       std::vector<ModStoreMod> retval;
+               std::vector<ModStoreMod> retval;
 
        if (modlist.isArray()) {
                for (unsigned int i = 0; i < modlist.size(); i++)
@@ -371,18 +362,10 @@ ModStoreModDetails          readModStoreModDetails(Json::Value& details) {
        }
 
        //value
-       if (details["rating"].asString().size()) {
-
-               std::string id_raw = details["rating"].asString();
-               char* endptr = 0;
-               float numbervalue = strtof(id_raw.c_str(),&endptr);
-
-               if ((id_raw != "") && (*endptr == 0)) {
-                       retval.rating = numbervalue;
-               }
-       }
-       else {
-               retval.rating = 0.0;
+       if (details["value"].isInt()) {
+               retval.rating = details["value"].asInt();
+       } else {
+               retval.rating = 0;
        }
 
        //depends