dl
dl_http.h
浏览该文件的文档.
1
13#pragma once
14
15#ifndef DL_DISABLE_CURL
16#include <functional>
17#include <string>
18
19#include "io/dl_log.h"
20#include "memory/dl_buffer.h"
21#include "io/dl_json.h"
22
23namespace dl
24{
26class Http
27{
28public:
29 class Request
30 {
31 public:
32 using FnCallback = std::function<void(void)>;
33 enum class Type
34 {
37 };
38 void SetUrl(std::string_view url) { _url = url; }
39 void SetType(Type type)
40 {
41 _type = type;
42 if (_type != Type::POST
43 && !_bufSend.empty())
44 {
45 log_err("附加数据必须是POST类型!");
46 return;
47 }
48 }
50 {
51 if (_type != Type::POST)
52 {
53 log_err("附加数据必须是POST类型!");
54 return;
55 }
56 _bufSend = CreateBuffer(buf);
57 }
58 void SetDataSend(std::string_view str)
59 {
61 }
62 void SetDataSend(const json& j)
63 {
65 }
66 void SetHeader(const std::vector<std::string>& vec_header)
67 {
68 _vecHeader = vec_header;
69 }
70 std::vector<std::string> GetHeader() { return _vecHeader; }
71 BufferView GetDataSend() { return _bufSend; }
72
73 FnCallback GetCb() { return _fnCb; }
74 std::string GetUrl() { return _url; }
75
76 BufferView GetDataRecv() { return _bufRecv; }
77 Buffer& GetDataRecvRef() { return _bufRecv; }
78
79 Type GetType() { return _type; }
80
82 _type{ Type::GET }
83 {
84
85 }
86 private:
87 Type _type;
88 FnCallback _fnCb;
89 std::string _url;
90 std::vector<std::string> _vecHeader;
91 Buffer _bufRecv;
92 Buffer _bufSend;
93 };
96 bool SendRequest(Request& request);
97
99 Buffer ExDownload(std::string_view url)
100 {
101 Request req;
103 req.SetUrl(url);
104
105 if (!SendRequest(req))
106 return {};
107 return CreateBuffer(req.GetDataRecv());
108 }
109
110 void SetCACertPath(std::string_view str)
111 {
112 _pathCACert = str;
113 }
114 void SetShowResultLog(bool show) { _bShowResultLog = show; }
115 // 辅助解析类
116 struct RecvData
117 {
118 int _code;
119 std::string _msg;
121
123 {
124 json j = dl::Json::Parse(buf);
125 dl::Json::Get(j, "Code", _code);
126 dl::Json::Get(j, "Msg", _msg);
127 dl::Json::Get(j, "Data", _data);
128 }
129
130 bool Check(int code = 0)
131 {
132 if (_code != code)
133 {
134 log_warn("{}", DumpError());
135 return false;
136 }
137 return true;
138 }
139
140 std::string DumpError()
141 {
142 std::string d;
143 if (_data.is_string())
144 d = _data;
145 else
146 d = _data.dump();
147 return fmt::format("{},{}({})", _msg, d, _code);
148 }
149 };
150private:
151 std::string _pathCACert; // 证书路径
152 bool _bShowResultLog;
153};
154extern Http* g_http;
155}
156#endif
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)
从 内存 构建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)