Advertisement
alivenets

Sample cURL C++ client

Mar 19th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1.  
  2. #include "curl/curl.h"
  3.  
  4. class Response
  5. {
  6. typedef uint16_t HttpCode;
  7.  
  8. std::string response;
  9. HttpCode code;
  10. public:
  11. explicit Response(const std::string &str):
  12. response(str)
  13. {}
  14. };
  15.  
  16. static const uint32_t REQUEST_TIMEOUT_MS = 30000;
  17. static const uint32_t CONNECT_TIMEOUT_MS = 5000;
  18. static const char *DOCKER_URL_BASE = "http://localhost";
  19. static const char *DOCKER_SOCK = "unix:/var/run/docker.sock";
  20.  
  21. class DockerClient
  22. {
  23. static void cleanupCurlContext(void *ptr) {
  24. curl_easy_cleanup(static_cast<CURL*>(ptr));
  25. }
  26.  
  27. std::unique_ptr<CURL, cleanupCurlContext> m_ctx;
  28. std::string socketPath;
  29.  
  30. private:
  31. DockerClient(const Client &) = delete;
  32. DockerClient(const Client &&) = delete;
  33. DockerClient &operator =(const Client &) = delete;
  34. DockerClient &&operator =(const Client &&) = delete;
  35.  
  36. typedef CURLerror ClientError;
  37. typedef CURLopt CurlOption;
  38.  
  39. bool isCurlSuccess(ClientError e) {
  40. return e != CURLE_OK;
  41. }
  42.  
  43. inline CurlError setOption(CurlOption opt, void *val) {
  44. return curl_easy_setopt(m_ctx, opt, param);
  45. }
  46.  
  47. inline CurlError perform() {
  48. return curl_easy_perform(m_ctx);
  49. }
  50. public:
  51. DockerClient(std::string socketPath) {
  52. m_socketPath = socketPath;
  53. m_ctx = curl_easy_init();
  54. reset();
  55. }
  56.  
  57. reset() {
  58. curl_easy_reset(m_ctx);
  59. setOption(CURLOPT_UNIX_SOCKET_PATH, m_socketPath)
  60. setOption(CURLOPT_TIMEOUT_MS, REQUEST_TIMEOUT_MS);
  61. setOption(CURLOPT_CONNECTTIMEOUT_MS, CONNECT_TIMEOUT_MS);
  62. }
  63.  
  64. void setVerbose(bool verbose) {
  65. const CurlError res = setOption(CURLOPT_VERBOSE, verbose ? 1L : 0L);
  66. }
  67.  
  68. Response get(std::string path) {
  69. return sendRequest("GET", path);
  70. }
  71.  
  72. Response post(std::string path, std::string body) {
  73. return sendRequest("POST", path, body);
  74. }
  75.  
  76. Response del(std::string path) {
  77. return sendRequest("DELETE", path);
  78. }
  79.  
  80. Response sendRequest(std::string method, std::string path, std::string body = "") {
  81. if (method == "GET") {
  82. setOption(CURLOPT_HTTPGET, 1L);
  83. }
  84. else if (method == "POST") {
  85. setOption(CURLOPT_POST, 1L);
  86. setOption(CURLOPT_POSTFIELDS, body);
  87. }
  88. else if (method == "DELETE") {
  89. setOption(CURLOPT_CUSTOMREQUEST, "DELETE")
  90. }
  91.  
  92. res = setOption(CURLOPT_URL, (std::string(DOCKER_URL_BASE) + "/" + path).c_str());
  93. res = perform();
  94. }
  95. };
  96.  
  97.  
  98. class GlobalCurl
  99. {
  100. public:
  101. GlobalCurl() {
  102. curl_global_init();
  103. }
  104. ~GlobalCurl() {
  105. curl_global_cleanup();
  106. }
  107. }
  108.  
  109. int main()
  110. {
  111. GlobalCurl curlGlobal;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement