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{
24class Http
25{
26public:
27 class Request
28 {
29 public:
30 using FnCallback = std::function<void(void)>;
31 enum class Type
32 {
35 };
36 void SetUrl(std::string_view url) { _url = url; }
37 void SetType(Type type)
38 {
39 _type = type;
40 if (_type != Type::POST
41 && !_bufSend.empty())
42 {
43 log_err("附加数据必须是POST类型!");
44 return;
45 }
46 }
48 {
49 if (_type != Type::POST)
50 {
51 log_err("附加数据必须是POST类型!");
52 return;
53 }
54 _bufSend = CreateBuffer(buf);
55 }
56 void SetDataSend(std::string_view str)
57 {
59 }
60 void SetDataSend(const json& j)
61 {
63 }
64 void SetHeader(const std::vector<std::string>& vec_header)
65 {
66 _vecHeader = vec_header;
67 }
68 std::vector<std::string> GetHeader() { return _vecHeader; }
69 BufferView GetDataSend() { return _bufSend; }
70
71 FnCallback GetCb() { return _fnCb; }
72 std::string GetUrl() { return _url; }
73
74 BufferView GetDataRecv() { return _bufRecv; }
75 Buffer& GetDataRecvRef() { return _bufRecv; }
76
77 Type GetType() { return _type; }
78
80 _type{ Type::GET }
81 {
82
83 }
84 private:
85 Type _type;
86 FnCallback _fnCb;
87 std::string _url;
88 std::vector<std::string> _vecHeader;
89 Buffer _bufRecv;
90 Buffer _bufSend;
91 };
94 bool SendRequest(Request& request);
95
97 Buffer ExDownload(std::string_view url)
98 {
99 Request req;
101 req.SetUrl(url);
102
103 if (!SendRequest(req))
104 return {};
105 return CreateBuffer(req.GetDataRecv());
106 }
107
108 void SetCACertPath(std::string_view str)
109 {
110 _pathCACert = str;
111 }
112 void SetShowResultLog(bool show) { _bShowResultLog = show; }
113 // 辅助解析类
114 struct RecvData
115 {
116 int _code;
117 std::string _msg;
119
121 {
122 json j = dl::Json::Parse(buf);
123 dl::Json::Get(j, "Code", _code);
124 dl::Json::Get(j, "Msg", _msg);
125 dl::Json::Get(j, "Data", _data);
126 }
127
128 bool Check(int code = 0)
129 {
130 if (_code != code)
131 {
132 log_warn("{}", DumpError());
133 return false;
134 }
135 return true;
136 }
137
138 std::string DumpError()
139 {
140 std::string d;
141 if (_data.is_string())
142 d = _data;
143 else
144 d = _data.dump();
145 return fmt::format("{},{}({})", _msg, d, _code);
146 }
147 };
148private:
149 std::string _pathCACert; // 证书路径
150 bool _bShowResultLog;
151};
152extern Http* g_http;
153}
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(std::string_view path_name)
从 文件 构建json对象
bool Get(const json &j, const std::string &name, T &t)
从json返回值,不存在则返回false,打印日志
std::span< std::byte > BufferView
Http * g_http
std::vector< std::byte > Buffer
Buffer CreateBuffer(BufferView buf)
BufferView CreateBufferView(std::string_view str)
创建buffer视图
std::string DumpError()
bool Check(int code=0)
RecvData(BufferView buf)