mirror of
https://github.com/BlackMATov/curly.hpp.git
synced 2025-12-15 12:19:47 +07:00
update README and little style fixes
This commit is contained in:
@@ -30,9 +30,9 @@
|
|||||||
- Custom headers
|
- Custom headers
|
||||||
- Asynchronous requests
|
- Asynchronous requests
|
||||||
- Different types of timeouts
|
- Different types of timeouts
|
||||||
- Custom completion callbacks
|
- Completion and progress callbacks
|
||||||
- PUT, GET, HEAD, POST methods
|
|
||||||
- Custom uploading and downloading streams
|
- Custom uploading and downloading streams
|
||||||
|
- PUT, GET, HEAD, POST, PATCH, DELETE, OPTIONS methods
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -244,7 +244,7 @@ netex::promise<net::content_t> download(std::string url) {
|
|||||||
reject(net::exception("network error"));
|
reject(net::exception("network error"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
net::response response = request.get();
|
net::response response = request.take();
|
||||||
if ( response.is_http_error() ) {
|
if ( response.is_http_error() ) {
|
||||||
reject(net::exception("server error"));
|
reject(net::exception("server error"));
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -289,25 +289,33 @@ namespace curly_hpp
|
|||||||
|
|
||||||
template < typename Callback >
|
template < typename Callback >
|
||||||
request_builder& callback(Callback&& f) {
|
request_builder& callback(Callback&& f) {
|
||||||
static_assert(std::is_convertible_v<Callback, callback_t>);
|
static_assert(
|
||||||
|
std::is_convertible_v<Callback, callback_t>,
|
||||||
|
"custom callback type error");
|
||||||
return callback(callback_t(std::forward<Callback>(f)));
|
return callback(callback_t(std::forward<Callback>(f)));
|
||||||
}
|
}
|
||||||
|
|
||||||
template < typename Uploader, typename... Args >
|
template < typename Uploader, typename... Args >
|
||||||
request_builder& uploader(Args&&... args) {
|
request_builder& uploader(Args&&... args) {
|
||||||
static_assert(std::is_base_of_v<upload_handler, Uploader>);
|
static_assert(
|
||||||
|
std::is_base_of_v<upload_handler, Uploader>,
|
||||||
|
"custom uploader type error");
|
||||||
return uploader(std::make_unique<Uploader>(std::forward<Args>(args)...));
|
return uploader(std::make_unique<Uploader>(std::forward<Args>(args)...));
|
||||||
}
|
}
|
||||||
|
|
||||||
template < typename Downloader, typename... Args >
|
template < typename Downloader, typename... Args >
|
||||||
request_builder& downloader(Args&&... args) {
|
request_builder& downloader(Args&&... args) {
|
||||||
static_assert(std::is_base_of_v<download_handler, Downloader>);
|
static_assert(
|
||||||
|
std::is_base_of_v<download_handler, Downloader>,
|
||||||
|
"custom downloader type error");
|
||||||
return downloader(std::make_unique<Downloader>(std::forward<Args>(args)...));
|
return downloader(std::make_unique<Downloader>(std::forward<Args>(args)...));
|
||||||
}
|
}
|
||||||
|
|
||||||
template < typename Progressor, typename... Args >
|
template < typename Progressor, typename... Args >
|
||||||
request_builder& progressor(Args&&... args) {
|
request_builder& progressor(Args&&... args) {
|
||||||
static_assert(std::is_base_of_v<progress_handler, Progressor>);
|
static_assert(
|
||||||
|
std::is_base_of_v<progress_handler, Progressor>,
|
||||||
|
"custom progressor type error");
|
||||||
return progressor(std::make_unique<Progressor>(std::forward<Args>(args)...));
|
return progressor(std::make_unique<Progressor>(std::forward<Args>(args)...));
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -121,14 +121,12 @@ namespace
|
|||||||
cvar_.notify_all();
|
cvar_.notify_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool try_dequeue(T& v) noexcept(
|
bool try_dequeue(T& v) {
|
||||||
std::is_nothrow_move_assignable_v<T>)
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> guard(mutex_);
|
std::lock_guard<std::mutex> guard(mutex_);
|
||||||
if ( queue_.empty() ) {
|
if ( queue_.empty() ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
v = std::move(queue_.front());
|
v = queue_.front();
|
||||||
queue_.pop();
|
queue_.pop();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -154,7 +152,7 @@ namespace
|
|||||||
}
|
}
|
||||||
|
|
||||||
template < typename Clock, typename Duration >
|
template < typename Clock, typename Duration >
|
||||||
bool wait_until(const std::chrono::time_point<Clock, Duration>& time) const {
|
bool wait_until(const std::chrono::time_point<Clock, Duration>& time) const noexcept {
|
||||||
std::unique_lock<std::mutex> lock(mutex_);
|
std::unique_lock<std::mutex> lock(mutex_);
|
||||||
return cvar_.wait_until(lock, time, [this](){
|
return cvar_.wait_until(lock, time, [this](){
|
||||||
return !queue_.empty();
|
return !queue_.empty();
|
||||||
@@ -171,9 +169,6 @@ namespace
|
|||||||
std::string header_builder;
|
std::string header_builder;
|
||||||
curl_slist* result = nullptr;
|
curl_slist* result = nullptr;
|
||||||
for ( const auto& [key,value] : headers ) {
|
for ( const auto& [key,value] : headers ) {
|
||||||
if ( key.empty() ) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
header_builder.clear();
|
header_builder.clear();
|
||||||
header_builder.append(key);
|
header_builder.append(key);
|
||||||
@@ -214,14 +209,12 @@ namespace
|
|||||||
class curl_state final {
|
class curl_state final {
|
||||||
public:
|
public:
|
||||||
template < typename F >
|
template < typename F >
|
||||||
static std::invoke_result_t<F, CURLM*> with(F&& f)
|
static std::invoke_result_t<F, CURLM*> with(F&& f) {
|
||||||
noexcept(std::is_nothrow_invocable_v<F, CURLM*>)
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> guard(mutex_);
|
std::lock_guard<std::mutex> guard(mutex_);
|
||||||
if ( !self_ ) {
|
if ( !self_ ) {
|
||||||
self_ = std::make_unique<curl_state>();
|
self_ = std::make_unique<curl_state>();
|
||||||
}
|
}
|
||||||
return std::forward<F>(f)(self_->curlm_);
|
return std::invoke(std::forward<F>(f), self_->curlm_);
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
curl_state() {
|
curl_state() {
|
||||||
|
|||||||
Reference in New Issue
Block a user