dl
dl_http.h
浏览该文件的文档.
1
13#pragma once
14
15#include <functional>
16#include <string>
17
18#include "io/dl_log.h"
19#include "memory/dl_buffer.h"
20#include "io/dl_json.h"
21
22namespace dl
23{
25class Http
26{
27public:
28 class Request
29 {
30 public:
31 using FnCallback = std::function<void(void)>;
32 enum class Type
33 {
36 };
37 void SetUrl(std::string_view url) { _url = url; }
38 void SetType(Type type)
39 {
40 _type = type;
41 if (_type != Type::POST
42 && !_bufSend.empty())
43 {
44 log_err("附加数据必须是POST类型!");
45 return;
46 }
47 }
49 {
50 if (_type != Type::POST)
51 {
52 log_err("附加数据必须是POST类型!");
53 return;
54 }
55 _bufSend = CreateBuffer(buf);
56 }
57 void SetDataSend(std::string_view str)
58 {
60 }
61 void SetDataSend(const json& j)
62 {
64 }
65 void SetHeader(const std::vector<std::string>& vec_header)
66 {
67 _vecHeader = vec_header;
68 }
69 std::vector<std::string> GetHeader() { return _vecHeader; }
70 BufferView GetDataSend() { return _bufSend; }
71
72 FnCallback GetCb() { return _fnCb; }
73 std::string GetUrl() { return _url; }
74
75 BufferView GetDataRecv() { return _bufRecv; }
76 Buffer& GetDataRecvRef() { return _bufRecv; }
77
78 Type GetType() { return _type; }
79
81 _type{ Type::GET }
82 {
83
84 }
85 private:
86 Type _type;
87 FnCallback _fnCb;
88 std::string _url;
89 std::vector<std::string> _vecHeader;
90 Buffer _bufRecv;
91 Buffer _bufSend;
92 };
95 bool SendRequest(Request& request);
96
98 Buffer ExDownload(std::string_view url)
99 {
100 Request req;
102 req.SetUrl(url);
103
104 if (!SendRequest(req))
105 return {};
106 return CreateBuffer(req.GetDataRecv());
107 }
108
109 void SetCACertPath(std::string_view str)
110 {
111 _pathCACert = str;
112 }
113 void SetShowResultLog(bool show) { _bShowResultLog = show; }
114 // 辅助解析类
115 struct RecvData
116 {
117 int _code;
118 std::string _msg;
120
122 {
123 json j = dl::Json::Parse(buf);
124 dl::Json::Get(j, "Code", _code);
125 dl::Json::Get(j, "Msg", _msg);
126 dl::Json::Get(j, "Data", _data);
127 }
128
129 bool Check(int code = 0)
130 {
131 if (_code != code)
132 {
133 log_warn("{}", DumpError());
134 return false;
135 }
136 return true;
137 }
138
139 std::string DumpError()
140 {
141 std::string d;
142 if (_data.is_string())
143 d = _data;
144 else
145 d = _data.dump();
146 return fmt::format("{},{}({})", _msg, d, _code);
147 }
148 };
149private:
150 std::string _pathCACert; // 证书路径
151 bool _bShowResultLog;
152};
153extern Http* g_http;
154}
void SetDataSend(const json &j)
std::vector< std::string > GetHeader()
void SetUrl(std::string_view url)
Buffer & GetDataRecvRef()
BufferView GetDataSend()
void SetDataSend(std::string_view str)
void SetType(Type type)
BufferView GetDataRecv()
std::function< void(void)> FnCallback
FnCallback GetCb()
void SetDataSend(BufferView buf)
std::string GetUrl()
void SetHeader(const std::vector< std::string > &vec_header)
bool SendRequest(Request &request)
void SetShowResultLog(bool show)
Buffer ExDownload(std::string_view url)
下载文件
void SetCACertPath(std::string_view str)
表示一段内存
json格式相关
nlohmann::json json
日志系统
#define log_warn(...)
#define log_err(...)
json Parse(BufferView buf, bool binary=false)
从 内存 构建json对象
bool Get(const json &j, const std::string &name, T &t)
从json返回子元素值,不存在则返回false,打印日志
Http * g_http
Buffer CreateBuffer(BufferView buf)
创建副本
BufferView CreateBufferView(const Buffer &buf)
创建buffer视图(右值不能默认转换)
std::string DumpError()
bool Check(int code=0)
RecvData(BufferView buf)