request.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2009 Andrew Stromme <astromme@chatonka.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Library General Public License as
  6. * published by the Free Software Foundation; either version 2, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this program; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "request.h"
  20. #include <QTime>
  21. #include <QMapIterator>
  22. #include <QDateTime>
  23. #include <QCryptographicHash>
  24. #include <QCoreApplication>
  25. #include <KDebug>
  26. #include <KIO/NetAccess>
  27. #include <QTimer>
  28. const int RTM::Request::MAX_RETRIES = 10;
  29. RTM::Request::Request(const QString &method, const QString &apiKey, const QString &sharedSecret)
  30. : currentJob(0)
  31. {
  32. arguments.insert("method", method);
  33. this->sharedSecret = sharedSecret;
  34. arguments.insert("api_key", apiKey);
  35. m_state = RTM::Mutable;
  36. retries = 0;
  37. }
  38. void RTM::Request::addArgument(const QString &name, const QString &value) {
  39. arguments.insert(name, value);
  40. }
  41. void RTM::Request::sendRequest()
  42. {
  43. static QDateTime lastRequest;
  44. // Follow RTM's TOS and only do 1 request per second.
  45. if (lastRequest.secsTo(QDateTime::currentDateTime()) <= 1) {
  46. QTimer::singleShot(1000, this, SLOT(sendRequest()));
  47. //kDebug() << "Postponing Job for 1 second";
  48. return;
  49. }
  50. QString url = requestUrl();
  51. kDebug() << "Request ready. Url is: " << url;
  52. currentJob = KIO::get(KUrl(url), KIO::NoReload, KIO::HideProgressInfo);
  53. connect(currentJob, SIGNAL(data(KIO::Job*,QByteArray)), SLOT(dataIncrement(KIO::Job*,QByteArray)));
  54. connect(currentJob, SIGNAL(result(KJob*)), this, SLOT(finished(KJob*)));
  55. lastRequest = QDateTime::currentDateTime();
  56. }
  57. QString RTM::Request::method() const {
  58. return arguments.value("method");
  59. }
  60. QByteArray RTM::Request::sendSynchronousRequest() {
  61. KIO::Job *job = KIO::get(KUrl(requestUrl()), KIO::NoReload, KIO::HideProgressInfo);
  62. job->setAutoDelete(true);
  63. QByteArray data = this->data();
  64. KIO::NetAccess::synchronousRun(job, 0, &data);
  65. return data; // TODO ERROR HANDLING
  66. }
  67. void RTM::Request::dataIncrement(KIO::Job* job, QByteArray data) {
  68. Q_UNUSED(job)
  69. //kDebug() << data;
  70. buffer().append(data);
  71. }
  72. void RTM::Request::finished(KJob* job) {
  73. if (job->error()) {
  74. kDebug() << "Network Job Error: " << job->errorString();
  75. if (retries >= MAX_RETRIES) {
  76. kDebug() << "ABORT: Maximum Retries reached for " << currentJob->url();
  77. currentJob = 0;
  78. return;
  79. }
  80. switch (job->error()) {
  81. case KIO::ERR_CONNECTION_BROKEN: // If the connection is broken, resend the request
  82. kDebug() << "Connection Error, retrying connection";
  83. disconnect(currentJob);
  84. retries++;
  85. currentJob = 0;
  86. sendRequest();
  87. return;
  88. //TODO: Handle other error cases.
  89. }
  90. }
  91. emit (replyReceived(this));
  92. }
  93. void RTM::Request::sign() {
  94. QString unistring = sharedSecret;
  95. QMapIterator<QString, QString> i(arguments);
  96. while (i.hasNext()) {
  97. i.next();
  98. unistring.append(i.key());
  99. unistring.append(i.value());
  100. }
  101. QString hash = QCryptographicHash::hash(unistring.toAscii(), QCryptographicHash::Md5).toHex();
  102. arguments.insert("api_sig", hash);
  103. m_state = RTM::Hashed;
  104. }
  105. void RTM::Request::unsign() {
  106. arguments.remove("api_sig");
  107. }
  108. QString RTM::Request::requestUrl()
  109. {
  110. switch(m_state) {
  111. case RTM::Mutable:
  112. sign();
  113. case RTM::Hashed:
  114. unsign();
  115. sign();
  116. break;
  117. case RTM::RequestSent:
  118. break;
  119. case RTM::RequestReceived:
  120. break;
  121. }
  122. //kDebug() << "Creating url";
  123. QString url = RTM::baseMethodUrl;
  124. foreach(const QString &key, arguments.keys())
  125. url.append('&' + key + '=' + arguments.value(key));
  126. return url;
  127. }
  128. RTM::Request::~Request() {
  129. }