CLOUD: Update BoxListDirectoryByIdRequest
It now uses special CurlJsonRequest static methods to check whether JSON is an object, has a string or integer parameter.
This commit is contained in:
parent
43c940c985
commit
cccfe7c247
3 changed files with 51 additions and 55 deletions
|
@ -125,63 +125,16 @@ void BoxListDirectoryByIdRequest::responseCallback(Networking::JsonResponse resp
|
|||
|
||||
Common::JSONArray items = responseObject.getVal("entries")->asArray();
|
||||
for (uint32 i = 0; i < items.size(); ++i) {
|
||||
if (!items[i]->isObject()) {
|
||||
warning("BoxListDirectoryByIdRequest: \"entries\" item is not an object!");
|
||||
debug(9, "%s", items[i]->stringify(true).c_str());
|
||||
continue;
|
||||
}
|
||||
if (!Networking::CurlJsonRequest::jsonIsObject(items[i], "BoxListDirectoryByIdRequest")) continue;
|
||||
|
||||
Common::JSONObject item = items[i]->asObject();
|
||||
|
||||
if (!item.contains("id") || !item.getVal("id")->isString()) {
|
||||
warning("BoxListDirectoryByIdRequest: \"entries\" item's \"id\"!");
|
||||
if (item.contains("id")) {
|
||||
debug(9, "%s", item.getVal("id")->stringify(true).c_str());
|
||||
} else {
|
||||
debug(9, "(not available)");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!item.contains("name") || !item.getVal("name")->isString()) {
|
||||
warning("BoxListDirectoryByIdRequest: \"entries\" item's \"name\"!");
|
||||
if (item.contains("name")) {
|
||||
debug(9, "%s", item.getVal("name")->stringify(true).c_str());
|
||||
} else {
|
||||
debug(9, "(not available)");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!item.contains("type") || !item.getVal("type")->isString()) {
|
||||
warning("BoxListDirectoryByIdRequest: \"entries\" item's \"type\"!");
|
||||
if (item.contains("type")) {
|
||||
debug(9, "%s", item.getVal("type")->stringify(true).c_str());
|
||||
} else {
|
||||
debug(9, "(not available)");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!item.contains("size") || (!item.getVal("size")->isString() && !item.getVal("size")->isIntegerNumber())) {
|
||||
warning("BoxListDirectoryByIdRequest: \"entries\" item's \"size\"!");
|
||||
if (item.contains("size")) {
|
||||
debug(9, "%s", item.getVal("size")->stringify(true).c_str());
|
||||
} else {
|
||||
debug(9, "(not available)");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!item.contains("modified_at") || !item.getVal("modified_at")->isString()) {
|
||||
warning("BoxListDirectoryByIdRequest: \"entries\" item's \"modified_at\"!");
|
||||
if (item.contains("modified_at")) {
|
||||
debug(9, "%s", item.getVal("modified_at")->stringify(true).c_str());
|
||||
} else {
|
||||
debug(9, "(not available)");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (!Networking::CurlJsonRequest::jsonContainsString(item, "id", "BoxListDirectoryByIdRequest")) continue;
|
||||
if (!Networking::CurlJsonRequest::jsonContainsString(item, "name", "BoxListDirectoryByIdRequest")) continue;
|
||||
if (!Networking::CurlJsonRequest::jsonContainsString(item, "type", "BoxListDirectoryByIdRequest")) continue;
|
||||
if (!Networking::CurlJsonRequest::jsonContainsString(item, "modified_at", "BoxListDirectoryByIdRequest")) continue;
|
||||
if (!Networking::CurlJsonRequest::jsonContainsString(item, "size", "BoxListDirectoryByIdRequest") &&
|
||||
!Networking::CurlJsonRequest::jsonContainsIntegerNumber(item, "size", "BoxListDirectoryByIdRequest")) continue;
|
||||
|
||||
Common::String id = item.getVal("id")->asString();
|
||||
Common::String name = item.getVal("name")->asString();
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
|
||||
namespace Networking {
|
||||
|
||||
CurlJsonRequest::CurlJsonRequest(JsonCallback cb, ErrorCallback ecb, Common::String url):
|
||||
CurlJsonRequest::CurlJsonRequest(JsonCallback cb, ErrorCallback ecb, Common::String url) :
|
||||
CurlRequest(nullptr, ecb, url), _jsonCallback(cb), _contentsStream(DisposeAfterUse::YES),
|
||||
_buffer(new byte[CURL_JSON_REQUEST_BUFFER_SIZE]) {}
|
||||
|
||||
|
@ -97,4 +97,43 @@ void CurlJsonRequest::finishJson(Common::JSONValue *json) {
|
|||
else delete json;
|
||||
}
|
||||
|
||||
bool CurlJsonRequest::jsonIsObject(Common::JSONValue *item, const char *warningPrefix) {
|
||||
if (item == nullptr) {
|
||||
warning("%s: passed item is NULL", warningPrefix);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (item->isObject()) return true;
|
||||
|
||||
warning("%s: passed item is not an object!", warningPrefix);
|
||||
debug(9, "%s", item->stringify(true).c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CurlJsonRequest::jsonContainsString(Common::JSONObject &item, const char *key, const char *warningPrefix) {
|
||||
if (!item.contains(key)) {
|
||||
warning("%s: passed item misses the \"%s\" attribute!", warningPrefix, key);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (item.getVal(key)->isString()) return true;
|
||||
|
||||
warning("%s: passed item's \"%s\" attribute is not a string!", warningPrefix, key);
|
||||
debug(9, "%s", item.getVal(key)->stringify(true).c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CurlJsonRequest::jsonContainsIntegerNumber(Common::JSONObject &item, const char *key, const char *warningPrefix) {
|
||||
if (!item.contains(key)) {
|
||||
warning("%s: passed item misses the \"%s\" attribute!", warningPrefix, key);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (item.getVal(key)->isIntegerNumber()) return true;
|
||||
|
||||
warning("%s: passed item's \"%s\" attribute is not an integer!", warningPrefix, key);
|
||||
debug(9, "%s", item.getVal(key)->stringify(true).c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
} // End of namespace Networking
|
||||
|
|
|
@ -52,6 +52,10 @@ public:
|
|||
|
||||
virtual void handle();
|
||||
virtual void restart();
|
||||
|
||||
static bool jsonIsObject(Common::JSONValue *item, const char *warningPrefix);
|
||||
static bool jsonContainsString(Common::JSONObject &item, const char *key, const char *warningPrefix);
|
||||
static bool jsonContainsIntegerNumber(Common::JSONObject &item, const char *key, const char *warningPrefix);
|
||||
};
|
||||
|
||||
} // End of namespace Networking
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue