ctkXnatAPI.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*=============================================================================
  2. Library: XNAT/Core
  3. Copyright (c) University College London,
  4. Centre for Medical Image Computing
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. // ctkXnatAPI includes
  16. #include "ctkXnatAPI_p.h"
  17. #include "qRestResult.h"
  18. #include <QNetworkReply>
  19. #include <QRegExp>
  20. #include <QUrl>
  21. #if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
  22. #include <QUrlQuery>
  23. #endif
  24. // --------------------------------------------------------------------------
  25. // ctkXnatAPI methods
  26. // --------------------------------------------------------------------------
  27. ctkXnatAPI::ctkXnatAPI(QObject* _parent)
  28. : Superclass(_parent)
  29. {
  30. }
  31. // --------------------------------------------------------------------------
  32. ctkXnatAPI::~ctkXnatAPI()
  33. {
  34. }
  35. // --------------------------------------------------------------------------
  36. QUuid ctkXnatAPI::get(const QString& resource, const Parameters& parameters, const qRestAPI::RawHeaders& rawHeaders)
  37. {
  38. QUrl url = this->createUrl(resource, parameters);
  39. #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
  40. url.addQueryItem("format", "json");
  41. #else
  42. QUrlQuery urlQuery(url);
  43. urlQuery.addQueryItem("format", "json");
  44. url.setQuery(urlQuery);
  45. #endif
  46. QNetworkReply* queryReply = this->sendRequest(QNetworkAccessManager::GetOperation, url, rawHeaders);
  47. QUuid queryId = queryReply->property("uuid").toString();
  48. return queryId;
  49. }
  50. // --------------------------------------------------------------------------
  51. void ctkXnatAPI::parseResponse(qRestResult* restResult, const QByteArray& response)
  52. {
  53. static QRegExp identifierPattern("[a-zA-Z][a-zA-Z0-9_]*");
  54. QList<QVariantMap> result;
  55. if (response.isEmpty())
  56. {
  57. // Some operations do not return result. E.g. creating a project.
  58. }
  59. else if (response.startsWith("<html>"))
  60. {
  61. // Some operations return an XML description of an object.
  62. // E.g. GET query for a specific subject.
  63. restResult->setError(QString("Bad data: ") + response, qRestAPI::ResponseParseError);
  64. }
  65. else if (response.startsWith("<?xml "))
  66. {
  67. // Some operations return an XML description of an object.
  68. // E.g. GET query for a specific subject.
  69. result = this->parseXmlResponse(restResult, response);
  70. }
  71. else if (response[0] == '{')
  72. {
  73. // Other operations return a json description of an object.
  74. // E.g. GET query of the list of subjects
  75. result = this->parseJsonResponse(restResult, response);
  76. }
  77. else if (identifierPattern.exactMatch(response))
  78. {
  79. // Some operations return the identifier of the newly created object.
  80. // E.g. creating a subject.
  81. QVariantMap map;
  82. map["ID"] = response;
  83. map["content"] = response;
  84. result.push_back(map);
  85. }
  86. else
  87. {
  88. QVariantMap map;
  89. map["content"] = response;
  90. result.push_back(map);
  91. }
  92. restResult->setResult(result);
  93. }
  94. // --------------------------------------------------------------------------
  95. QList<QVariantMap> ctkXnatAPI::parseXmlResponse(qRestResult* /*restResult*/, const QByteArray& /*response*/)
  96. {
  97. QList<QVariantMap> result;
  98. return result;
  99. }
  100. // --------------------------------------------------------------------------
  101. QList<QVariantMap> ctkXnatAPI::parseJsonResponse(qRestResult* restResult, const QByteArray& response)
  102. {
  103. QScriptValue scriptValue = this->ScriptEngine.evaluate("(" + QString(response) + ")");
  104. QList<QVariantMap> result;
  105. // e.g. {"ResultSet":{"Result": [{"p1":"v1","p2":"v2",...}], "totalRecords":"13"}}
  106. QScriptValue resultSet = scriptValue.property("ResultSet");
  107. QScriptValue data = resultSet.property("Result");
  108. if (!data.isObject())
  109. {
  110. if (!data.toString().isEmpty())
  111. {
  112. restResult->setError(QString("Bad data: ") + data.toString(), qRestAPI::ResponseParseError);
  113. }
  114. }
  115. if (data.isArray())
  116. {
  117. quint32 length = data.property("length").toUInt32();
  118. for(quint32 i = 0; i < length; ++i)
  119. {
  120. qRestAPI::appendScriptValueToVariantMapList(result, data.property(i));
  121. }
  122. }
  123. else
  124. {
  125. qRestAPI::appendScriptValueToVariantMapList(result, data);
  126. }
  127. return result;
  128. }