| 1 | coeurl |
| 2 | ====== |
| 3 | |
| 4 | [](https://nheko.im/nheko-reborn/coeurl/-/pipelines) |
| 5 | [](https://nheko.im/nheko-reborn/coeurl/-/pipelines/latest) |
| 6 | [](https://nheko-reborn.pages.nheko.im/coeurl/index.html) |
| 7 | [](https://matrix.to/#/#nheko:nheko.im) |
| 8 | |
| 9 | Simple library to do http requests asynchronously via CURL in C++. (Eventually |
| 10 | as coroutines, once all the compilers I need to support support them.) |
| 11 | |
| 12 | This is based on the [CURL-libevent |
| 13 | example](https://curl.se/libcurl/c/hiperfifo.html). |
| 14 | |
| 15 | You can do a get request with 3 simple steps: |
| 16 | |
| 17 | 1. Initialize the library: |
| 18 | ```cpp |
| 19 | coeurl::Client g{}; |
| 20 | ``` |
| 21 | 2. Do a request |
| 22 | ```cpp |
| 23 | g.get("http://localhost:5000/", [](const coeurl::Request &res) { |
| 24 | std::cout << res.response() << std::endl; |
| 25 | }); |
| 26 | ``` |
| 27 | |
| 28 | If you need more flexibility, you can initialize a `coeurl::Request` manually |
| 29 | and set all the fields required. Currently only a few methods are exposed, but |
| 30 | we may decide to add more in the future or expose the easy handle for direct |
| 31 | manipulation. |
| 32 | |
| 33 | Dependencies |
| 34 | ------------ |
| 35 | |
| 36 | - CURL (duh!) |
| 37 | - libevent |
| 38 | - spdlog |
| 39 | - for tests: doctest |
| 40 | |
| 41 | Building |
| 42 | -------- |
| 43 | |
| 44 | Usually meson should do all you need. For example you can build it like this in |
| 45 | a subdirectory called `buildir/`: |
| 46 | |
| 47 | ```sh |
| 48 | meson setup builddir |
| 49 | meson compile -C builddir |
| 50 | ``` |
| 51 | |
| 52 | There is also a cmake file, but this does not properly support installation. It |
| 53 | is only useful for using this project via ExternalProject. |
| 54 | |
| 55 | Limitations |
| 56 | ----------- |
| 57 | |
| 58 | The event loop can only run on one thread at a time! If you need to parallelize |
| 59 | your request, use multiple clients or dispatch the responses into a threadpool |
| 60 | manually. In most cases the one thread should be enough for the simpler |
| 61 | workloads though and this way you can benefit from connection pooling and the |
| 62 | HTTP/2 multiplexing. The tread is created internally and you currently have no |
| 63 | control over it. You can wait for it to exit using `close()` or the `~Client` |
| 64 | destructor. If you block this thread, no other requests will get processed. |
| 65 | |
| 66 | This library also only exposes the simple way to do things currently. This is |
| 67 | all I need at the moment. |
| 68 | |
| 69 | Interesting bits |
| 70 | ---------------- |
| 71 | |
| 72 | - You can enable logging or customize the logging by setting a logger with |
| 73 | `coeurl::Client::set_logger`. Do this before you initialize any client! |
| 74 | - The Request can be constructed builder style, then submitted and then you can |
| 75 | query the Request once it has completed for the headers, status code, etc. |
| 76 | - Don't modify the Request while it is in flight or call any members on it until |
| 77 | it is completed, after you submitted it. |
| 78 |