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