| 41 | } |
| 42 | |
| 43 | ananas::Future<std::pair<bool, std::string>> AFCHttpClient::AsyncRequest( |
| 44 | brynet::net::http::HttpRequest::HTTP_METHOD http_method, const std::string& ip, const uint16_t port, |
| 45 | const std::string& url, std::map<std::string, std::string>& params, const std::vector<std::string>& cookies, |
| 46 | const std::string& http_body) |
| 47 | { |
| 48 | using namespace brynet::net; |
| 49 | using namespace brynet::net::http; |
| 50 | |
| 51 | http::HttpRequest request; |
| 52 | request.setMethod(http_method); |
| 53 | request.setUrl(url); |
| 54 | request.addHeadValue("Host", std::string(ip) + std::string(":") + ARK_TO_STRING(port)); |
| 55 | |
| 56 | for (auto cookie : cookies) |
| 57 | { |
| 58 | request.setCookie(cookie); |
| 59 | } |
| 60 | |
| 61 | request.setContentType("application/json"); |
| 62 | |
| 63 | switch (http_method) |
| 64 | { |
| 65 | case brynet::net::http::HttpRequest::HTTP_METHOD::HTTP_METHOD_POST: |
| 66 | case brynet::net::http::HttpRequest::HTTP_METHOD::HTTP_METHOD_PUT: |
| 67 | if (!http_body.empty()) |
| 68 | { |
| 69 | request.setBody(http_body); |
| 70 | } |
| 71 | break; |
| 72 | default: |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | HttpQueryParameter query_params; |
| 77 | for (auto iter : params) |
| 78 | { |
| 79 | query_params.add(iter.first, iter.second); |
| 80 | } |
| 81 | |
| 82 | request.setQuery(query_params.getResult()); |
| 83 | std::string req_url = request.getResult(); |
| 84 | |
| 85 | // promise |
| 86 | ananas::Promise<std::pair<bool, std::string>> promise; |
| 87 | |
| 88 | // start to connect |
| 89 | connection_builder_.configureConnector(connector_) |
| 90 | .configureService(tcp_service_) |
| 91 | .configureConnectOptions({AsyncConnector::ConnectOptions::WithAddr(ip, port), |
| 92 | AsyncConnector::ConnectOptions::WithTimeout(ARK_CONNECT_TIMEOUT), |
| 93 | AsyncConnector::ConnectOptions::WithFailedCallback( |
| 94 | [=]() mutable { promise.SetValue(std::make_pair(false, std::string())); })}) |
| 95 | .configureConnectionOptions({TcpService::AddSocketOption::WithMaxRecvBufferSize(ARK_HTTP_RECV_BUFFER_SIZE), |
| 96 | TcpService::AddSocketOption::AddEnterCallback([](const TcpConnection::Ptr& session) { |
| 97 | #ifdef ARK_RUN_MODE_DEBUG |
| 98 | CONSOLE_INFO_LOG << "http client connected!" << std::endl; |
| 99 | #endif |
| 100 | })}) |
no test coverage detected