Skip to content

Commit 9bd70bd

Browse files
committed
Added async/await interface for all HTTP methods
1 parent 7e9194a commit 9bd70bd

File tree

7 files changed

+1067
-72
lines changed

7 files changed

+1067
-72
lines changed

CHANGELOG.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
# [1.2.0] - 2025-01-18
2+
3+
## New Features
4+
- **C++20 Coroutine Awaitable HTTP API**: Added modern async/await interface for all HTTP methods
5+
- `asio::awaitable<Response> async_get(url, headers)` - Awaitable GET request
6+
- `asio::awaitable<Response> async_post(url, data, headers)` - Awaitable POST request
7+
- `asio::awaitable<Response> async_put(url, data, headers)` - Awaitable PUT request
8+
- `asio::awaitable<Response> async_patch(url, data, headers)` - Awaitable PATCH request
9+
- `asio::awaitable<Response> async_del(url, data, headers)` - Awaitable DELETE request
10+
- Clean async/await syntax using `co_await` for sequential or parallel HTTP operations
11+
- Uses caller's executor context (no service thread management required)
12+
- Supports both HTTP and HTTPS protocols
13+
114
# [1.1.2] - 2025-11-13
2-
- Remove unnecessary slick_logger from slick_net link dependencies
15+
- Remove unnecessary slick_logger from slick_net link dependencies
316
- Update CMakeLists to link slick_logger with example executables
417

518
# [1.1.1] - 2025-10-22

CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ if (MSVC)
77
endif()
88
endif()
99

10-
set(BUILD_VERSION 1.1.2)
11-
project(slick_net VERSION ${BUILD_VERSION} LANGUAGES CXX)
10+
project(slick_net
11+
VERSION 1.2.0
12+
LANGUAGES CXX
13+
)
1214

1315
set(CMAKE_CXX_STANDARD 20)
1416
set(CMAKE_CXX_STANDARD_REQUIRED ON)
@@ -139,7 +141,7 @@ if(CMAKE_BUILD_TYPE STREQUAL "Release")
139141

140142
if (PROJECT_IS_TOP_LEVEL)
141143
add_custom_target(package_slick_net ALL
142-
COMMAND ${CMAKE_COMMAND} -E tar "cfv" "${CMAKE_BINARY_DIR}/dist/slick_net_${BUILD_VERSION}.zip" --format=zip "include"
144+
COMMAND ${CMAKE_COMMAND} -E tar "cfv" "${CMAKE_BINARY_DIR}/dist/slick_net_${PROJECT_VERSION}.zip" --format=zip "include"
143145
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/dist"
144146
COMMENT "Creating zip archive"
145147
)

README.md

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ A high-performance C++ HTTP/WebSocket client library built on Boost.Beast with f
1313
- **HTTP Streaming**: Support for Server-Sent Events (SSE) and chunked response streaming
1414
- **Asynchronous WebSocket Client**: Built on Boost.Asio coroutines for high-performance async operations
1515
- **SSL/TLS Support**: Native support for secure `https://` and `wss://` connections
16-
- **Synchronous & Asynchronous APIs**: Both blocking and non-blocking HTTP operations
16+
- **Multiple Async APIs**: Synchronous, callback-based, and C++20 coroutine awaitable interfaces
1717
- **Cross-Platform**: Works on Windows, Linux, and macOS
1818
- **Header-Only**: Easy integration with minimal dependencies
1919
- **Callback-Based API**: Clean event-driven interface for connection lifecycle management
@@ -156,6 +156,7 @@ Run examples:
156156
./examples/websocket_client_example
157157
./examples/http_client_example
158158
./examples/http_stream_client_example
159+
./examples/http_awaitable_client_example
159160
```
160161

161162
## API Reference
@@ -171,7 +172,7 @@ Http::Response patch(std::string_view url, std::string_view data, std::vector<st
171172
Http::Response del(std::string_view url, std::string_view data, std::vector<std::pair<std::string, std::string>>&& headers = {});
172173
```
173174
174-
**Asynchronous Methods:**
175+
**Asynchronous Callback-Based Methods:**
175176
```cpp
176177
void async_get(std::function<void(Response&&)> on_response, std::string_view url, std::vector<std::pair<std::string, std::string>>&& headers = {});
177178
void async_post(std::function<void(Response&&)> on_response, std::string_view url, std::string_view data, std::vector<std::pair<std::string, std::string>>&& headers = {});
@@ -180,6 +181,15 @@ void async_patch(std::function<void(Response&&)> on_response, std::string_view u
180181
void async_del(std::function<void(Response&&)> on_response, std::string_view url, std::string_view data, std::vector<std::pair<std::string, std::string>>&& headers = {});
181182
```
182183

184+
**Asynchronous Awaitable Methods (C++20 Coroutines):**
185+
```cpp
186+
asio::awaitable<Response> async_get(std::string_view url, std::vector<std::pair<std::string, std::string>>&& headers = {});
187+
asio::awaitable<Response> async_post(std::string_view url, std::string_view data, std::vector<std::pair<std::string, std::string>>&& headers = {});
188+
asio::awaitable<Response> async_put(std::string_view url, std::string_view data, std::vector<std::pair<std::string, std::string>>&& headers = {});
189+
asio::awaitable<Response> async_patch(std::string_view url, std::string_view data, std::vector<std::pair<std::string, std::string>>&& headers = {});
190+
asio::awaitable<Response> async_del(std::string_view url, std::string_view data = "", std::vector<std::pair<std::string, std::string>>&& headers = {});
191+
```
192+
183193
**Response Structure:**
184194
```cpp
185195
struct Response {
@@ -189,7 +199,7 @@ struct Response {
189199
};
190200
```
191201

192-
**Example Usage:**
202+
**Example Usage - Synchronous:**
193203
```cpp
194204
#include <slick/net/http.h>
195205

@@ -198,6 +208,11 @@ auto response = Http::get("https://api.example.com/data");
198208
if (response.is_ok()) {
199209
std::cout << response.result_text << std::endl;
200210
}
211+
```
212+
213+
**Example Usage - Asynchronous Callback-Based:**
214+
```cpp
215+
#include <slick/net/http.h>
201216

202217
// Asynchronous POST with JSON
203218
nlohmann::json data = {{"key", "value"}};
@@ -208,6 +223,41 @@ Http::async_post([](Http::Response&& rsp) {
208223
}, "https://api.example.com/resource", data.dump(), {{"Content-Type", "application/json"}});
209224
```
210225
226+
**Example Usage - Asynchronous Awaitable (C++20 Coroutines):**
227+
```cpp
228+
#include <slick/net/http.h>
229+
#include <boost/asio.hpp>
230+
231+
asio::awaitable<void> fetch_data() {
232+
// Awaitable GET - clean async/await syntax
233+
auto response = co_await Http::async_get("https://api.example.com/data");
234+
if (response.is_ok()) {
235+
std::cout << "Response: " << response.result_text << std::endl;
236+
}
237+
238+
// Sequential requests
239+
nlohmann::json post_data = {{"key", "value"}};
240+
auto post_response = co_await Http::async_post(
241+
"https://api.example.com/resource",
242+
post_data.dump(),
243+
{{"Content-Type", "application/json"}}
244+
);
245+
246+
if (post_response.is_ok()) {
247+
std::cout << "Created: " << post_response.result_text << std::endl;
248+
}
249+
}
250+
251+
int main() {
252+
asio::io_context ioc;
253+
254+
asio::co_spawn(ioc, fetch_data(), asio::detached);
255+
256+
ioc.run();
257+
return 0;
258+
}
259+
```
260+
211261
### Websocket Class
212262

213263
**Constructor:**

examples/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ target_link_libraries(http_client_example PRIVATE slick_net slick_logger nlohman
2525

2626
add_executable(http_stream_client_example http_stream_client.cpp)
2727
target_include_directories(http_stream_client_example PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include)
28-
target_link_libraries(http_stream_client_example PRIVATE slick_net slick_logger nlohmann_json::nlohmann_json)
28+
target_link_libraries(http_stream_client_example PRIVATE slick_net slick_logger nlohmann_json::nlohmann_json)
29+
30+
add_executable(http_awaitable_client_example http_awaitable_client.cpp)
31+
target_include_directories(http_awaitable_client_example PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include)
32+
target_link_libraries(http_awaitable_client_example PRIVATE slick_net slick_logger nlohmann_json::nlohmann_json)

0 commit comments

Comments
 (0)