axios.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #ifndef HV_AXIOS_H_
  2. #include "json.hpp"
  3. #include "requests.h"
  4. /*
  5. * Inspired by js axios
  6. *
  7. * @code
  8. #include "axios.h"
  9. int main() {
  10. const char* strReq = R"(
  11. {
  12. "method": "POST",
  13. "url": "http://127.0.0.1:8080/echo",
  14. "timeout": 10,
  15. "params": {
  16. "page_no": "1",
  17. "page_size": "10"
  18. },
  19. "headers": {
  20. "Content-Type": "application/json"
  21. },
  22. "body": {
  23. "app_id": "123456",
  24. "app_secret": "abcdefg"
  25. }
  26. }
  27. )";
  28. // sync
  29. auto resp = axios::axios(strReq);
  30. if (resp == NULL) {
  31. printf("request failed!\n");
  32. } else {
  33. printf("%s\n", resp->body.c_str());
  34. }
  35. // async
  36. int finished = 0;
  37. axios::axios(strReq, [&finished](const HttpResponsePtr& resp) {
  38. if (resp == NULL) {
  39. printf("request failed!\n");
  40. } else {
  41. printf("%s\n", resp->body.c_str());
  42. }
  43. finished = 1;
  44. });
  45. // wait async finished
  46. while (!finished) hv_sleep(1);
  47. return 0;
  48. }
  49. **/
  50. using nlohmann::json;
  51. using requests::Request;
  52. using requests::Response;
  53. using requests::ResponseCallback;
  54. namespace axios {
  55. HV_INLINE Request newRequestFromJson(const json& jreq) {
  56. Request req(new HttpRequest);
  57. // url
  58. if (jreq.contains("url")) {
  59. req->url = jreq["url"];
  60. }
  61. // params
  62. if (jreq.contains("params")) {
  63. req->query_params = jreq["params"].get<QueryParams>();
  64. }
  65. // headers
  66. if (jreq.contains("headers")) {
  67. req->headers = jreq["headers"].get<http_headers>();
  68. }
  69. // body/data
  70. const char* body_field = nullptr;
  71. if (jreq.contains("body")) {
  72. body_field = "body";
  73. } else if (jreq.contains("data")) {
  74. body_field = "data";
  75. }
  76. if (body_field) {
  77. const json& jbody = jreq[body_field];
  78. if (jbody.is_object() || jbody.is_array()) {
  79. req->json = jbody;
  80. } else if (jbody.is_string()) {
  81. req->body = jbody;
  82. }
  83. }
  84. // method
  85. if (jreq.contains("method")) {
  86. std::string method = jreq["method"];
  87. req->method = http_method_enum(method.c_str());
  88. } else if (body_field) {
  89. req->method = HTTP_POST;
  90. } else {
  91. req->method = HTTP_GET;
  92. }
  93. // timeout
  94. if (jreq.contains("timeout")) {
  95. req->timeout = jreq["timeout"];
  96. }
  97. return req;
  98. }
  99. HV_INLINE Request newRequestFromJsonString(const char* req_str) {
  100. return newRequestFromJson(json::parse(req_str));
  101. }
  102. // sync
  103. HV_INLINE Response axios(const json& jreq, http_method method = HTTP_GET, const char* url = nullptr) {
  104. auto req = newRequestFromJson(jreq);
  105. if (method != HTTP_GET) {
  106. req->method = method;
  107. }
  108. if (url) {
  109. req->url = url;
  110. }
  111. return req ? requests::request(req) : nullptr;
  112. }
  113. HV_INLINE Response axios(const char* req_str, http_method method = HTTP_GET, const char* url = nullptr) {
  114. return req_str ? axios(json::parse(req_str), method, url)
  115. : requests::request(method, url);
  116. }
  117. HV_INLINE Response head(const char* url, const json& jreq) {
  118. return axios(jreq, HTTP_HEAD, url);
  119. }
  120. HV_INLINE Response head(const char* url, const char* req_str = nullptr) {
  121. return axios(req_str, HTTP_HEAD, url);
  122. }
  123. HV_INLINE Response get(const char* url, const json& jreq) {
  124. return axios(jreq, HTTP_GET, url);
  125. }
  126. HV_INLINE Response get(const char* url, const char* req_str = nullptr) {
  127. return axios(req_str, HTTP_GET, url);
  128. }
  129. HV_INLINE Response post(const char* url, const json& jreq) {
  130. return axios(jreq, HTTP_POST, url);
  131. }
  132. HV_INLINE Response post(const char* url, const char* req_str = nullptr) {
  133. return axios(req_str, HTTP_POST, url);
  134. }
  135. HV_INLINE Response put(const char* url, const json& jreq) {
  136. return axios(jreq, HTTP_PUT, url);
  137. }
  138. HV_INLINE Response put(const char* url, const char* req_str = nullptr) {
  139. return axios(req_str, HTTP_PUT, url);
  140. }
  141. HV_INLINE Response patch(const char* url, const json& jreq) {
  142. return axios(jreq, HTTP_PATCH, url);
  143. }
  144. HV_INLINE Response patch(const char* url, const char* req_str = nullptr) {
  145. return axios(req_str, HTTP_PATCH, url);
  146. }
  147. HV_INLINE Response Delete(const char* url, const json& jreq) {
  148. return axios(jreq, HTTP_DELETE, url);
  149. }
  150. HV_INLINE Response Delete(const char* url, const char* req_str = nullptr) {
  151. return axios(req_str, HTTP_DELETE, url);
  152. }
  153. // async
  154. HV_INLINE int axios(const json& jreq, ResponseCallback resp_cb) {
  155. auto req = newRequestFromJson(jreq);
  156. return req ? requests::async(req, resp_cb) : -1;
  157. }
  158. HV_INLINE int axios(const char* req_str, ResponseCallback resp_cb) {
  159. return axios(json::parse(req_str), resp_cb);
  160. }
  161. }
  162. #endif // HV_AXIOS_H_