Files
Netralib/include/NetRequest.hpp

224 lines
8.8 KiB
C++
Raw Normal View History

2025-09-08 12:41:08 +08:00
/*
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
*/
#pragma once
#include "httplib.h"
#include <string>
#include <functional>
#include <optional>
#include <future>
#include <chrono>
namespace ntq
{
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* @brief HTTP
*
*
* from_cache true
*/
struct HttpResponse
{
int status = 0; ///< HTTP 状态码(例如 200, 404 等)
std::string body; ///< 响应正文
httplib::Headers headers; ///< 响应头(大小写不敏感)
bool from_cache = false; ///< 是否来自缓存
};
/**
* @brief
*/
enum class ErrorCode
{
None = 0, ///< 无错误
Network, ///< 网络错误(连接失败/发送失败/接收失败等)
Timeout, ///< 超时
Canceled, ///< 被取消
InvalidURL, ///< URL 非法
IOError, ///< 本地 IO 错误(如写文件失败)
SSL, ///< SSL/HTTPS 相关错误
Unknown ///< 未分类错误
};
/**
* @brief
*
*
*/
struct RequestOptions
{
std::string scheme = "http"; ///< 协议http 或 https
std::string host; ///< 目标主机名或 IP必填
int port = 80; ///< 端口http 默认为 80https 通常为 443
std::string base_path; ///< 可选的统一前缀(例如 "/api/v1"
int connect_timeout_ms = 5000; ///< 连接超时(毫秒)
int read_timeout_ms = 10000; ///< 读取超时(毫秒)
int write_timeout_ms = 10000; ///< 写入超时(毫秒)
bool keep_alive = true; ///< 是否保持连接Keep-Alive
httplib::Headers default_headers; ///< 默认头部,随所有请求发送
};
/**
* @class NetRequest
* @brief HTTP cpp-httplib
*
* GET/POST
*
*/
class NetRequest
{
public:
using LogCallback = std::function<void(const std::string &)>; ///< 日志回调类型
/**
* @brief
*/
struct Stats
{
uint64_t total_requests = 0; ///< 累计请求次数
uint64_t total_errors = 0; ///< 累计失败次数
double last_latency_ms = 0.0;///< 最近一次请求耗时(毫秒)
double avg_latency_ms = 0.0; ///< 指数平滑后的平均耗时(毫秒)
};
/**
* @brief
* @param options
*/
explicit NetRequest(const RequestOptions &options);
/**
* @brief
*/
~NetRequest();
/**
* @brief
* @param logger
*/
void setLogger(LogCallback logger);
/**
* @brief
* @param n 1
*/
void setMaxConcurrentRequests(size_t n);
/**
* @brief
* @param ttl
*/
void enableCache(std::chrono::milliseconds ttl);
/**
* @brief
*/
void disableCache();
/**
* @brief GET
* @param path base_path
* @param query ?k=v&...
* @param headers
* @param err
* @return std::nullopt
*/
std::optional<HttpResponse> Get(const std::string &path,
const httplib::Params &query = {},
const httplib::Headers &headers = {},
ErrorCode *err = nullptr);
/**
* @brief POST JSON
* @param path
* @param json JSON Content-Type: application/json
* @param headers
* @param err
* @return std::nullopt
*/
std::optional<HttpResponse> PostJson(const std::string &path,
const std::string &json,
const httplib::Headers &headers = {},
ErrorCode *err = nullptr);
/**
* @brief POST application/x-www-form-urlencoded
* @param path
* @param form
* @param headers
* @param err
* @return std::nullopt
*/
std::optional<HttpResponse> PostForm(const std::string &path,
const httplib::Params &form,
const httplib::Headers &headers = {},
ErrorCode *err = nullptr);
/**
* @brief GET
* @return std::future
*/
std::future<std::optional<HttpResponse>> GetAsync(const std::string &path,
const httplib::Params &query = {},
const httplib::Headers &headers = {},
ErrorCode *err = nullptr);
/**
* @brief POST JSON
* @return std::future
*/
std::future<std::optional<HttpResponse>> PostJsonAsync(const std::string &path,
const std::string &json,
const httplib::Headers &headers = {},
ErrorCode *err = nullptr);
/**
* @brief POST
* @return std::future
*/
std::future<std::optional<HttpResponse>> PostFormAsync(const std::string &path,
const httplib::Params &form,
const httplib::Headers &headers = {},
ErrorCode *err = nullptr);
/**
* @brief
* @param path
* @param local_file
* @param headers
* @param resume Range
* @param chunk_size httplib
* @param err
* @return true 200 206false
*/
bool DownloadToFile(const std::string &path,
const std::string &local_file,
const httplib::Headers &headers = {},
bool resume = true,
size_t chunk_size = 1 << 15,
ErrorCode *err = nullptr);
/**
* @brief
*/
Stats getStats() const;
private:
struct Impl;
Impl *impl_;
};
}