1
0

axios.h 4.7 KB

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