Magic_Game/src/kiwano-network/HttpRequest.h

221 lines
5.4 KiB
C
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
2020-01-21 10:09:55 +08:00
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
2020-01-17 16:55:47 +08:00
#include <kiwano/core/Common.h>
2019-11-13 14:33:15 +08:00
#include <kiwano/core/ObjectBase.h>
2020-02-15 17:32:32 +08:00
#include <kiwano/core/Json.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
namespace network
{
class HttpResponse;
2020-01-21 10:09:55 +08:00
KGE_DECLARE_SMART_PTR(HttpRequest);
2020-01-21 10:09:55 +08:00
/**
* \addtogroup Network
* @{
*/
2019-12-31 10:37:29 +08:00
2020-02-06 16:54:47 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief HTTP请求类型
2020-02-06 16:54:47 +08:00
enum class HttpType
{
2020-02-10 17:32:04 +08:00
Unknown, ///< 未知
Get, ///< HTTP GET请求
Post, ///< HTTP POST请求
Put, ///< HTTP PUT请求
Delete ///< HTTP DELETE请求
2020-02-06 16:54:47 +08:00
};
2020-01-21 10:09:55 +08:00
/**
* \~chinese
2020-02-10 17:32:04 +08:00
* @brief HTTP请求
2020-01-21 10:09:55 +08:00
*/
class KGE_API HttpRequest : public virtual ObjectBase
{
public:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 响应回调函数
2020-01-21 10:09:55 +08:00
using ResponseCallback = Function<void(HttpRequest* /* request */, HttpResponse* /* response */)>;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建HTTP请求
/// @param url 请求地址
/// @param type 请求类型
/// @param callback 响应回调函数
2020-02-19 12:09:50 +08:00
static HttpRequestPtr Create(const String& url, HttpType type, const ResponseCallback& callback);
2020-02-06 16:54:47 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建HTTP请求
/// @param url 请求地址
/// @param type 请求类型
/// @param data 请求数据
/// @param callback 响应回调函数
2020-02-19 12:09:50 +08:00
static HttpRequestPtr Create(const String& url, HttpType type, const String& data, const ResponseCallback& callback);
2020-02-06 16:54:47 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建HTTP请求
/// @param url 请求地址
/// @param type 请求类型
/// @param json 请求的JSON数据
/// @param callback 响应回调函数
2020-02-19 12:09:50 +08:00
static HttpRequestPtr Create(const String& url, HttpType type, const Json& json, const ResponseCallback& callback);
2020-01-21 10:09:55 +08:00
HttpRequest();
2020-02-06 16:54:47 +08:00
HttpRequest(HttpType type);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置请求地址
2020-02-19 12:09:50 +08:00
void SetUrl(const String& url);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置请求类型
2020-02-06 16:54:47 +08:00
void SetType(HttpType type);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置请求数据
2020-02-19 12:09:50 +08:00
void SetData(const String& data);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置请求的JSON数据
2020-02-19 12:09:50 +08:00
void SetJsonData(const Json& json);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置HTTP头
2020-02-19 12:09:50 +08:00
void SetHeaders(const Map<String, String>& headers);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置HTTP头
2020-02-19 12:09:50 +08:00
void SetHeader(const String& field, const String& content);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置响应回调函数
2020-02-19 12:09:50 +08:00
void SetResponseCallback(const ResponseCallback& callback);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取请求地址
2020-02-19 12:09:50 +08:00
const String& GetUrl() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取请求类型
2020-02-06 16:54:47 +08:00
HttpType GetType() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取请求数据
2020-02-19 12:09:50 +08:00
const String& GetData() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取HTTP头
2020-01-21 10:09:55 +08:00
Map<String, String>& GetHeaders();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取HTTP头
2020-02-19 12:09:50 +08:00
const String& GetHeader(const String& header) const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取响应回调函数
2020-02-19 12:09:50 +08:00
const ResponseCallback& GetResponseCallback() const;
2020-01-21 10:09:55 +08:00
private:
2020-02-06 16:54:47 +08:00
HttpType type_;
2020-01-21 10:09:55 +08:00
String url_;
String data_;
Map<String, String> headers_;
ResponseCallback response_cb_;
};
/** @} */
inline HttpRequest::HttpRequest()
2020-02-06 16:54:47 +08:00
: type_(HttpType::Unknown)
2020-01-21 10:09:55 +08:00
{
}
2020-02-06 16:54:47 +08:00
inline HttpRequest::HttpRequest(HttpType type)
2020-01-21 10:09:55 +08:00
: type_(type)
{
}
2020-02-19 12:09:50 +08:00
inline void HttpRequest::SetUrl(const String& url)
2020-01-21 10:09:55 +08:00
{
url_ = url;
}
2020-02-19 12:09:50 +08:00
inline const String& HttpRequest::GetUrl() const
2020-01-21 10:09:55 +08:00
{
return url_;
}
2020-02-06 16:54:47 +08:00
inline void HttpRequest::SetType(HttpType type)
2020-01-21 10:09:55 +08:00
{
type_ = type;
}
2020-02-06 16:54:47 +08:00
inline HttpType HttpRequest::GetType() const
2020-01-21 10:09:55 +08:00
{
return type_;
}
2020-02-19 12:09:50 +08:00
inline void HttpRequest::SetData(const String& data)
2020-01-21 10:09:55 +08:00
{
data_ = data;
}
2020-02-19 12:09:50 +08:00
inline const String& HttpRequest::GetData() const
2020-01-21 10:09:55 +08:00
{
return data_;
}
2019-11-13 14:33:15 +08:00
2020-02-19 12:09:50 +08:00
inline void HttpRequest::SetHeaders(const Map<String, String>& headers)
2020-01-21 10:09:55 +08:00
{
headers_ = headers;
}
2019-11-13 14:33:15 +08:00
2020-02-19 12:09:50 +08:00
inline void HttpRequest::SetHeader(const String& field, const String& content)
2020-01-21 10:09:55 +08:00
{
headers_[field] = content;
}
2019-11-13 14:33:15 +08:00
2020-01-21 10:09:55 +08:00
inline Map<String, String>& HttpRequest::GetHeaders()
{
return headers_;
}
2019-11-13 14:33:15 +08:00
2020-02-19 12:09:50 +08:00
inline const String& HttpRequest::GetHeader(const String& header) const
2020-01-21 10:09:55 +08:00
{
return headers_.at(header);
}
2019-11-13 14:33:15 +08:00
2020-02-19 12:09:50 +08:00
inline void HttpRequest::SetResponseCallback(const ResponseCallback& callback)
2020-01-21 10:09:55 +08:00
{
response_cb_ = callback;
}
2019-11-13 14:33:15 +08:00
2020-02-19 12:09:50 +08:00
inline const HttpRequest::ResponseCallback& HttpRequest::GetResponseCallback() const
2020-01-21 10:09:55 +08:00
{
return response_cb_;
}
2020-01-21 10:09:55 +08:00
} // namespace network
} // namespace kiwano