mirror of
https://github.com/BlackMATov/curly.hpp.git
synced 2025-12-14 12:10:53 +07:00
add promises example
This commit is contained in:
48
README.md
48
README.md
@@ -150,7 +150,7 @@ if ( request.is_done() ) {
|
||||
// Error message: Couldn't resolve host name
|
||||
```
|
||||
|
||||
### Request Callback
|
||||
### Request Callbacks
|
||||
|
||||
```cpp
|
||||
auto request = net::request_builder("http://www.httpbin.org/get")
|
||||
@@ -223,6 +223,52 @@ net::request_builder()
|
||||
.send().wait();
|
||||
```
|
||||
|
||||
## Promised Requests
|
||||
|
||||
Also, you can easily integrate promises like a [promise.hpp](https://github.com/BlackMATov/promise.hpp).
|
||||
|
||||
```cpp
|
||||
#include <curly.hpp/curly.hpp>
|
||||
namespace net = curly_hpp;
|
||||
|
||||
#include <promise.hpp/promise.hpp>
|
||||
namespace netex = promise_hpp;
|
||||
|
||||
netex::promise<net::content_t> download(std::string url) {
|
||||
return netex::make_promise<net::content_t>([
|
||||
url = std::move(url)
|
||||
](auto resolve, auto reject){
|
||||
net::request_builder(std::move(url))
|
||||
.callback([resolve,reject](net::request request) mutable {
|
||||
if ( !request.is_done() ) {
|
||||
reject(net::exception("network error"));
|
||||
return;
|
||||
}
|
||||
net::response response = request.get();
|
||||
if ( response.is_http_error() ) {
|
||||
reject(net::exception("server error"));
|
||||
return;
|
||||
}
|
||||
resolve(std::move(response.content));
|
||||
}).send();
|
||||
});
|
||||
}
|
||||
|
||||
auto promise = download("https://httpbin.org/image/png")
|
||||
.then([](const net::content_t& content){
|
||||
std::cout << content.size() << " bytes downloaded" << std::endl;
|
||||
}).except([](std::exception_ptr e){
|
||||
try {
|
||||
std::rethrow_exception(e);
|
||||
} catch (const std::exception& ee) {
|
||||
std::cerr << "Failed to download: " << ee.what() << std::endl;
|
||||
}
|
||||
});
|
||||
|
||||
promise.wait();
|
||||
// 8090 bytes downloaded
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
> coming soon!
|
||||
|
||||
@@ -57,6 +57,26 @@ namespace
|
||||
throw std::exception();
|
||||
}
|
||||
};
|
||||
|
||||
netex::promise<net::content_t> download(std::string url) {
|
||||
return netex::make_promise<net::content_t>([
|
||||
url = std::move(url)
|
||||
](auto resolve, auto reject){
|
||||
net::request_builder(std::move(url))
|
||||
.callback([resolve,reject](net::request request) mutable {
|
||||
if ( !request.is_done() ) {
|
||||
reject(net::exception("network error"));
|
||||
return;
|
||||
}
|
||||
net::response response = request.get();
|
||||
if ( response.is_http_error() ) {
|
||||
reject(net::exception("server error"));
|
||||
return;
|
||||
}
|
||||
resolve(std::move(response.content));
|
||||
}).send();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("curly") {
|
||||
@@ -706,7 +726,7 @@ TEST_CASE("curly_examples") {
|
||||
// Error message: Couldn't resolve host name
|
||||
}
|
||||
|
||||
SECTION("Request Callback") {
|
||||
SECTION("Request Callbacks") {
|
||||
auto request = net::request_builder("http://www.httpbin.org/get")
|
||||
.callback([](net::request request){
|
||||
if ( request.is_done() ) {
|
||||
@@ -771,4 +791,20 @@ TEST_CASE("curly_examples") {
|
||||
.send().get();
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Promised Requests") {
|
||||
auto promise = download("https://httpbin.org/image/png")
|
||||
.then([](const net::content_t& content){
|
||||
std::cout << content.size() << " bytes downloaded" << std::endl;
|
||||
}).except([](std::exception_ptr e){
|
||||
try {
|
||||
std::rethrow_exception(e);
|
||||
} catch (const std::exception& ee) {
|
||||
std::cerr << "Failed to download: " << ee.what() << std::endl;
|
||||
}
|
||||
});
|
||||
|
||||
promise.wait();
|
||||
// 8090 bytes downloaded
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user