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