update HttpClient
This commit is contained in:
parent
eea3f92567
commit
e2b5953a75
|
|
@ -119,11 +119,7 @@ namespace
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
CURLcode code = curl_easy_getinfo(curl_, CURLINFO_RESPONSE_CODE, response_code);
|
CURLcode code = curl_easy_getinfo(curl_, CURLINFO_RESPONSE_CODE, response_code);
|
||||||
if (code != CURLE_OK || !(*response_code >= 200 && *response_code < 300))
|
return code == CURLE_OK && (*response_code >= 200 && *response_code < 300);
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename ..._Args>
|
template <typename ..._Args>
|
||||||
|
|
@ -133,41 +129,68 @@ namespace
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static inline bool GetRequest(HttpClient* client, HttpRequestPtr const& request, long* response_code, std::string* response_data, std::string* response_header, char* error_buffer)
|
static inline bool GetRequest(
|
||||||
|
HttpClient* client,
|
||||||
|
Array<std::string> const& headers,
|
||||||
|
std::string const& url,
|
||||||
|
long* response_code,
|
||||||
|
std::string* response_data,
|
||||||
|
std::string* response_header,
|
||||||
|
char* error_buffer)
|
||||||
{
|
{
|
||||||
Curl curl;
|
Curl curl;
|
||||||
return curl.Init(client, request->GetHeaders(), request->GetUrl(), response_data, response_header, error_buffer)
|
return curl.Init(client, headers, url, response_data, response_header, error_buffer)
|
||||||
&& curl.SetOption(CURLOPT_FOLLOWLOCATION, true)
|
&& curl.SetOption(CURLOPT_FOLLOWLOCATION, true)
|
||||||
&& curl.Perform(response_code);
|
&& curl.Perform(response_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool PostRequest(HttpClient* client, HttpRequestPtr const& request, long* response_code, std::string* response_data, std::string* response_header, char* error_buffer)
|
static inline bool PostRequest(
|
||||||
|
HttpClient* client,
|
||||||
|
Array<std::string> const& headers,
|
||||||
|
std::string const& url,
|
||||||
|
std::string const& request_data,
|
||||||
|
long* response_code,
|
||||||
|
std::string* response_data,
|
||||||
|
std::string* response_header,
|
||||||
|
char* error_buffer)
|
||||||
{
|
{
|
||||||
Curl curl;
|
Curl curl;
|
||||||
const auto request_data = request->GetData();
|
return curl.Init(client, headers, url, response_data, response_header, error_buffer)
|
||||||
return curl.Init(client, request->GetHeaders(), request->GetUrl(), response_data, response_header, error_buffer)
|
|
||||||
&& curl.SetOption(CURLOPT_POST, 1)
|
&& curl.SetOption(CURLOPT_POST, 1)
|
||||||
&& curl.SetOption(CURLOPT_POSTFIELDS, request_data.c_str())
|
&& curl.SetOption(CURLOPT_POSTFIELDS, request_data.c_str())
|
||||||
&& curl.SetOption(CURLOPT_POSTFIELDSIZE, request_data.size())
|
&& curl.SetOption(CURLOPT_POSTFIELDSIZE, request_data.size())
|
||||||
&& curl.Perform(response_code);
|
&& curl.Perform(response_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool PutRequest(HttpClient* client, HttpRequestPtr const& request, long* response_code, std::string* response_data, std::string* response_header, char* error_buffer)
|
static inline bool PutRequest(
|
||||||
|
HttpClient* client,
|
||||||
|
Array<std::string> const& headers,
|
||||||
|
std::string const& url,
|
||||||
|
std::string const& request_data,
|
||||||
|
long* response_code,
|
||||||
|
std::string* response_data,
|
||||||
|
std::string* response_header,
|
||||||
|
char* error_buffer)
|
||||||
{
|
{
|
||||||
Curl curl;
|
Curl curl;
|
||||||
const auto request_data = request->GetData();
|
return curl.Init(client, headers, url, response_data, response_header, error_buffer)
|
||||||
return curl.Init(client, request->GetHeaders(), request->GetUrl(), response_data, response_header, error_buffer)
|
|
||||||
&& curl.SetOption(CURLOPT_CUSTOMREQUEST, "PUT")
|
&& curl.SetOption(CURLOPT_CUSTOMREQUEST, "PUT")
|
||||||
&& curl.SetOption(CURLOPT_POSTFIELDS, request_data.c_str())
|
&& curl.SetOption(CURLOPT_POSTFIELDS, request_data.c_str())
|
||||||
&& curl.SetOption(CURLOPT_POSTFIELDSIZE, request_data.size())
|
&& curl.SetOption(CURLOPT_POSTFIELDSIZE, request_data.size())
|
||||||
&& curl.Perform(response_code);
|
&& curl.Perform(response_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool DeleteRequest(HttpClient* client, HttpRequestPtr const& request, long* response_code, std::string* response_data, std::string* response_header, char* error_buffer)
|
static inline bool DeleteRequest(
|
||||||
|
HttpClient* client,
|
||||||
|
Array<std::string> const& headers,
|
||||||
|
std::string const& url,
|
||||||
|
long* response_code,
|
||||||
|
std::string* response_data,
|
||||||
|
std::string* response_header,
|
||||||
|
char* error_buffer)
|
||||||
{
|
{
|
||||||
Curl curl;
|
Curl curl;
|
||||||
const auto request_data = request->GetData();
|
return curl.Init(client, headers, url, response_data, response_header, error_buffer)
|
||||||
return curl.Init(client, request->GetHeaders(), request->GetUrl(), response_data, response_header, error_buffer)
|
|
||||||
&& curl.SetOption(CURLOPT_CUSTOMREQUEST, "DELETE")
|
&& curl.SetOption(CURLOPT_CUSTOMREQUEST, "DELETE")
|
||||||
&& curl.SetOption(CURLOPT_FOLLOWLOCATION, true)
|
&& curl.SetOption(CURLOPT_FOLLOWLOCATION, true)
|
||||||
&& curl.Perform(response_code);
|
&& curl.Perform(response_code);
|
||||||
|
|
@ -250,21 +273,32 @@ namespace easy2d
|
||||||
{
|
{
|
||||||
bool ok = false;
|
bool ok = false;
|
||||||
long response_code = 0;
|
long response_code = 0;
|
||||||
char error_message[256];
|
char error_message[256] = { 0 };
|
||||||
|
std::string response_header;
|
||||||
|
std::string response_data;
|
||||||
|
|
||||||
|
std::string url = request->GetUrl().to_string();
|
||||||
|
std::string data = request->GetData().to_string();
|
||||||
|
Array<std::string> headers;
|
||||||
|
headers.reserve(request->GetHeaders().size());
|
||||||
|
for (const auto& header : request->GetHeaders())
|
||||||
|
{
|
||||||
|
headers.push_back(header.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
switch (request->GetType())
|
switch (request->GetType())
|
||||||
{
|
{
|
||||||
case HttpRequest::Type::Get:
|
case HttpRequest::Type::Get:
|
||||||
ok = Curl::GetRequest(this, request, &response_code, response->GetDataPtr(), response->GetHeaderPtr(), error_message);
|
ok = Curl::GetRequest(this, headers, url, &response_code, &response_data, &response_header, error_message);
|
||||||
break;
|
break;
|
||||||
case HttpRequest::Type::Post:
|
case HttpRequest::Type::Post:
|
||||||
ok = Curl::PostRequest(this, request, &response_code, response->GetDataPtr(), response->GetHeaderPtr(), error_message);
|
ok = Curl::PostRequest(this, headers, url, data, &response_code, &response_data, &response_header, error_message);
|
||||||
break;
|
break;
|
||||||
case HttpRequest::Type::Put:
|
case HttpRequest::Type::Put:
|
||||||
ok = Curl::PutRequest(this, request, &response_code, response->GetDataPtr(), response->GetHeaderPtr(), error_message);
|
ok = Curl::PutRequest(this, headers, url, data, &response_code, &response_data, &response_header, error_message);
|
||||||
break;
|
break;
|
||||||
case HttpRequest::Type::Delete:
|
case HttpRequest::Type::Delete:
|
||||||
ok = Curl::DeleteRequest(this, request, &response_code, response->GetDataPtr(), response->GetHeaderPtr(), error_message);
|
ok = Curl::DeleteRequest(this, headers, url, &response_code, &response_data, &response_header, error_message);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
E2D_ERROR_LOG(L"HttpClient: unknown request type, only GET, POST, PUT or DELETE is supported");
|
E2D_ERROR_LOG(L"HttpClient: unknown request type, only GET, POST, PUT or DELETE is supported");
|
||||||
|
|
@ -272,6 +306,8 @@ namespace easy2d
|
||||||
}
|
}
|
||||||
|
|
||||||
response->SetResponseCode(response_code);
|
response->SetResponseCode(response_code);
|
||||||
|
response->SetHeader(response_header);
|
||||||
|
response->SetData(response_data);
|
||||||
if (!ok)
|
if (!ok)
|
||||||
{
|
{
|
||||||
response->SetSucceed(false);
|
response->SetSucceed(false);
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ namespace easy2d
|
||||||
url_ = url.to_string();
|
url_ = url.to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::string const& GetUrl() const
|
inline String const& GetUrl() const
|
||||||
{
|
{
|
||||||
return url_;
|
return url_;
|
||||||
}
|
}
|
||||||
|
|
@ -76,21 +76,17 @@ namespace easy2d
|
||||||
data_ = data.to_string();
|
data_ = data.to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::string const& GetData() const
|
inline String const& GetData() const
|
||||||
{
|
{
|
||||||
return data_;
|
return data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void SetHeaders(Array<String> const& headers)
|
inline void SetHeaders(Array<String> const& headers)
|
||||||
{
|
{
|
||||||
headers_.reserve(headers.size());
|
headers_ = headers;
|
||||||
for (const auto& header : headers)
|
|
||||||
{
|
|
||||||
headers_.push_back(header.to_string());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Array<std::string> const& GetHeaders() const
|
inline Array<String> const& GetHeaders() const
|
||||||
{
|
{
|
||||||
return headers_;
|
return headers_;
|
||||||
}
|
}
|
||||||
|
|
@ -107,9 +103,9 @@ namespace easy2d
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Type type_;
|
Type type_;
|
||||||
std::string url_;
|
String url_;
|
||||||
std::string data_;
|
String data_;
|
||||||
Array<std::string> headers_;
|
Array<String> headers_;
|
||||||
ResponseCallback response_cb_;
|
ResponseCallback response_cb_;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,49 +60,34 @@ namespace easy2d
|
||||||
return response_code_;
|
return response_code_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void SetHeader(std::string const& response_header)
|
inline void SetHeader(String const& response_header)
|
||||||
{
|
{
|
||||||
response_header_ = response_header;
|
response_header_ = response_header;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline String GetHeader() const
|
inline String GetHeader() const
|
||||||
{
|
{
|
||||||
return String(response_header_);
|
return response_header_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::string* GetHeaderPtr()
|
inline void SetData(String const& response_data)
|
||||||
{
|
|
||||||
return &response_header_;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void SetData(std::string const& response_data)
|
|
||||||
{
|
{
|
||||||
response_data_ = response_data;
|
response_data_ = response_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline String GetData() const
|
inline String const& GetData() const
|
||||||
{
|
{
|
||||||
return String(response_data_);
|
return response_data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::string* GetDataPtr()
|
inline void SetError(String const& error_buffer)
|
||||||
{
|
|
||||||
return &response_data_;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void SetError(std::string const& error_buffer)
|
|
||||||
{
|
{
|
||||||
error_buffer_ = error_buffer;
|
error_buffer_ = error_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline String GetError() const
|
inline String const& GetError() const
|
||||||
{
|
{
|
||||||
return String(error_buffer_);
|
return error_buffer_;
|
||||||
}
|
|
||||||
|
|
||||||
inline std::string* GetErrorPtr()
|
|
||||||
{
|
|
||||||
return &error_buffer_;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
@ -110,9 +95,9 @@ namespace easy2d
|
||||||
long response_code_;
|
long response_code_;
|
||||||
HttpRequestPtr request_;
|
HttpRequestPtr request_;
|
||||||
|
|
||||||
std::string response_header_;
|
String response_header_;
|
||||||
std::string response_data_;
|
String response_data_;
|
||||||
std::string error_buffer_;
|
String error_buffer_;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
class Demo5
|
class Demo5
|
||||||
: public Scene
|
: public Scene
|
||||||
|
|
@ -31,6 +32,7 @@ public:
|
||||||
void OnEnter() override
|
void OnEnter() override
|
||||||
{
|
{
|
||||||
Application::ShowConsole(true);
|
Application::ShowConsole(true);
|
||||||
|
SendRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnExit() override
|
void OnExit() override
|
||||||
|
|
@ -42,25 +44,40 @@ public:
|
||||||
{
|
{
|
||||||
if (e.key.code == KeyCode::Space)
|
if (e.key.code == KeyCode::Space)
|
||||||
{
|
{
|
||||||
|
SendRequest();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SendRequest()
|
||||||
|
{
|
||||||
|
Logger::Instance().Println(L"Start to send request...");
|
||||||
|
|
||||||
HttpRequestPtr request = new HttpRequest;
|
HttpRequestPtr request = new HttpRequest;
|
||||||
request->SetUrl(L"http://httpbin.org/get/");
|
request->SetUrl(L"http://httpbin.org/get");
|
||||||
request->SetType(HttpRequest::Type::Get);
|
request->SetType(HttpRequest::Type::Get);
|
||||||
request->SetResponseCallback(Closure(this, &Demo5::Complete));
|
request->SetResponseCallback(Closure(this, &Demo5::Complete));
|
||||||
|
|
||||||
HttpClient::Instance().Send(request);
|
HttpClient::Instance().Send(request);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void Complete(HttpRequestPtr request, HttpResponsePtr response)
|
void Complete(HttpRequestPtr request, HttpResponsePtr response)
|
||||||
{
|
{
|
||||||
if (response->IsSucceed())
|
if (response->IsSucceed())
|
||||||
{
|
{
|
||||||
std::wcout << "Response: " << std::endl << "HttpCode: " << response->GetResponseCode()
|
Json response_data = Json::parse(response->GetData());
|
||||||
<< std::endl << "Data: " << response->GetData() << std::endl;
|
Json result = {
|
||||||
|
{L"HttpCode", response->GetResponseCode()},
|
||||||
|
{L"Data", response_data},
|
||||||
|
};
|
||||||
|
std::wcout << L"Response: " << std::endl << result.dump(4) << std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
std::wcout << "Error: " << response->GetError() << std::endl;
|
Json result = {
|
||||||
|
{L"HttpCode", response->GetResponseCode()},
|
||||||
|
{L"Error", response->GetError()},
|
||||||
|
};
|
||||||
|
std::wcout << L"Response: " << std::endl << result.dump(4) << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue