Magic_Game/src/kiwano-network/HttpRequest.h

113 lines
3.6 KiB
C
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
//
// 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:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// 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
2019-12-23 18:05:08 +08:00
#include <kiwano/core/common.h>
2019-11-13 14:33:15 +08:00
#include <kiwano/core/ObjectBase.h>
#include <kiwano/core/SmartPtr.hpp>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
namespace network
{
2019-09-30 10:59:04 +08:00
class HttpResponse;
KGE_DECLARE_SMART_PTR(HttpRequest);
2019-04-11 14:40:54 +08:00
class KGE_API HttpRequest
2019-08-18 10:23:54 +08:00
: public ObjectBase
{
public:
2019-09-30 10:59:04 +08:00
using ResponseCallback = Function<void(HttpRequest*, HttpResponse*)>;
enum class Type
{
Unknown,
Get,
Post,
Put,
Delete
};
2019-11-13 14:33:15 +08:00
HttpRequest();
2019-11-13 14:33:15 +08:00
HttpRequest(Type type);
2019-11-13 14:33:15 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
void SetUrl(String const& url);
String const& GetUrl() const;
2019-11-13 14:33:15 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void SetType(Type type);
Type GetType() const;
2019-11-13 14:33:15 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void SetData(String const& data);
void SetJsonData(Json const& json);
String const& GetData() const;
2019-11-13 14:33:15 +08:00
// <20><><EFBFBD><EFBFBD>ͷ
void SetHeaders(Map<String, String> const& headers);
void SetHeader(String const& field, String const& content);
Map<String, String>& GetHeaders();
String const& GetHeader(String const& header) const;
2019-11-13 14:33:15 +08:00
// <20><>Ӧ<EFBFBD>ص<EFBFBD>
void SetResponseCallback(ResponseCallback const& callback);
ResponseCallback const& GetResponseCallback() const;
2019-12-23 18:05:08 +08:00
private:
2019-11-13 14:33:15 +08:00
Type type_;
String url_;
String data_;
Map<String, String> headers_;
ResponseCallback response_cb_;
};
2019-11-13 14:33:15 +08:00
inline HttpRequest::HttpRequest() : type_(Type::Unknown) {}
2019-11-13 14:33:15 +08:00
inline HttpRequest::HttpRequest(Type type) : type_(type) {}
2019-11-13 14:33:15 +08:00
inline void HttpRequest::SetUrl(String const& url) { url_ = url; }
2019-11-13 14:33:15 +08:00
inline String const& HttpRequest::GetUrl() const { return url_; }
2019-11-13 14:33:15 +08:00
inline void HttpRequest::SetType(Type type) { type_ = type; }
2019-11-13 14:33:15 +08:00
inline HttpRequest::Type HttpRequest::GetType() const { return type_; }
2019-11-13 14:33:15 +08:00
inline void HttpRequest::SetData(String const& data) { data_ = data; }
2019-11-13 14:33:15 +08:00
inline String const& HttpRequest::GetData() const { return data_; }
inline void HttpRequest::SetHeaders(Map<String, String> const& headers) { headers_ = headers; }
inline void HttpRequest::SetHeader(String const& field, String const& content) { headers_[field] = content; }
inline Map<String, String>& HttpRequest::GetHeaders() { return headers_; }
inline String const& HttpRequest::GetHeader(String const& header) const { return headers_.at(header); }
inline void HttpRequest::SetResponseCallback(ResponseCallback const& callback) { response_cb_ = callback; }
inline HttpRequest::ResponseCallback const& HttpRequest::GetResponseCallback() const { return response_cb_; }
}
}