restclient.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @file restclient.cpp
  3. * @brief implementation of the restclient class
  4. *
  5. * This just provides static wrappers around the Connection class REST
  6. * methods. However since I didn't want to have to pull in a whole URI parsing
  7. * library just now, the Connection constructor is passed an empty string and
  8. * the full URL is passed to the REST methods. All those methods to is
  9. * concatenate them anyways. So this should do for now.
  10. *
  11. * @author Daniel Schauenberg <d@unwiredcouch.com>
  12. */
  13. #include "restclient-cpp/restclient.h"
  14. #include <string.h>
  15. #include <curl/curl.h>
  16. #include "restclient-cpp/connection.h"
  17. #include "system/Thread.h"
  18. #include "manager/ConfigManager.h"
  19. /**
  20. * @brief global init function. Call this before you start any threads.
  21. */
  22. int RestClient::init() {
  23. CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
  24. if (res == CURLE_OK) {
  25. return 0;
  26. } else {
  27. return 1;
  28. }
  29. }
  30. /**
  31. * @brief global disable function. Call this before you terminate your
  32. * program.
  33. */
  34. void RestClient::disable() {
  35. curl_global_cleanup();
  36. }
  37. /**
  38. * @brief HTTP GET method
  39. *
  40. * @param url to query
  41. *
  42. * @return response struct
  43. */
  44. RestClient::Response RestClient::get(const std::string& url) {
  45. RestClient::Response ret;
  46. RestClient::Connection *conn = new RestClient::Connection("");
  47. conn->SetTimeout(3);
  48. if (strstr(url.c_str(), "https://") == url.c_str()) {
  49. conn->SetCAInfoFilePath(ConfigManager::getInstance()->getResFilePath("cacert.pem"));
  50. }
  51. ret = conn->get(url);
  52. delete conn;
  53. return ret;
  54. }
  55. /**
  56. * @brief HTTP POST method
  57. *
  58. * @param url to query
  59. * @param ctype content type as string
  60. * @param data HTTP POST body
  61. *
  62. * @return response struct
  63. */
  64. RestClient::Response RestClient::post(const std::string& url,
  65. const std::string& ctype,
  66. const std::string& data) {
  67. RestClient::Response ret;
  68. RestClient::Connection *conn = new RestClient::Connection("");
  69. conn->SetTimeout(3);
  70. if (strstr(url.c_str(), "https://") == url.c_str()) {
  71. conn->SetCAInfoFilePath(ConfigManager::getInstance()->getResFilePath("cacert.pem"));
  72. }
  73. conn->AppendHeader("Content-Type", ctype);
  74. ret = conn->post(url, data);
  75. delete conn;
  76. return ret;
  77. }
  78. /**
  79. * @brief HTTP PUT method
  80. *
  81. * @param url to query
  82. * @param ctype content type as string
  83. * @param data HTTP PUT body
  84. *
  85. * @return response struct
  86. */
  87. RestClient::Response RestClient::put(const std::string& url,
  88. const std::string& ctype,
  89. const std::string& data) {
  90. RestClient::Response ret;
  91. RestClient::Connection *conn = new RestClient::Connection("");
  92. if (strstr(url.c_str(), "https://") == url.c_str()) {
  93. conn->SetCAInfoFilePath(ConfigManager::getInstance()->getResFilePath("cacert.pem"));
  94. }
  95. conn->AppendHeader("Content-Type", ctype);
  96. ret = conn->put(url, data);
  97. delete conn;
  98. return ret;
  99. }
  100. /**
  101. * @brief HTTP DELETE method
  102. *
  103. * @param url to query
  104. *
  105. * @return response struct
  106. */
  107. RestClient::Response RestClient::del(const std::string& url) {
  108. RestClient::Response ret;
  109. RestClient::Connection *conn = new RestClient::Connection("");
  110. if (strstr(url.c_str(), "https://") == url.c_str()) {
  111. conn->SetCAInfoFilePath(ConfigManager::getInstance()->getResFilePath("cacert.pem"));
  112. }
  113. ret = conn->del(url);
  114. delete conn;
  115. return ret;
  116. }
  117. /**
  118. * @brief HTTP HEAD method
  119. *
  120. * @param url to query
  121. *
  122. * @return response struct
  123. */
  124. RestClient::Response RestClient::head(const std::string& url) {
  125. RestClient::Response ret;
  126. RestClient::Connection *conn = new RestClient::Connection("");
  127. if (strstr(url.c_str(), "https://") == url.c_str()) {
  128. conn->SetCAInfoFilePath(ConfigManager::getInstance()->getResFilePath("cacert.pem"));
  129. }
  130. ret = conn->head(url);
  131. delete conn;
  132. return ret;
  133. }
  134. RestClient::Response RestClient::download(const std::string& url,
  135. std::string path_to_save) {
  136. RestClient::Response ret;
  137. RestClient::Connection *conn = new RestClient::Connection("");
  138. conn->SetTimeout(10);
  139. if (strstr(url.c_str(), "https://") == url.c_str()) {
  140. conn->SetCAInfoFilePath(ConfigManager::getInstance()->getResFilePath("cacert.pem"));
  141. }
  142. ret = conn->download(url, path_to_save);
  143. delete conn;
  144. return ret;
  145. }