Code snippet
#include "stackapi/StackAPI.hpp"
#include "stackapi/data/structs/comments/Comment.hpp"
#include <iostream>
int main() {
stackapi::StackAPI api (
{
.apiKey{API_KEY},
.site{"stackoverflow"}, // default site
.pageSize = 2,
.userAgent{"StackAPIDemo/git (+https://github.com/LunarWatcher/stackapi.cpp)"}
}
);
auto res = api.get<stackapi::Comment>("comments", {
// Additional URL parameters can go here. Mostly exists for edge-cases with extra GET parameters. This is just a regular map, so arguments are in the form of
// {"key", "value"}
}, {
// The second argument is a struct, as revealed by the init syntax. It contains a few common fields, as well as some temporary config override options.
// For instance, you can write:
// .site{"metastackoverflow"},
// to, for this call only, set the site to meta.SO.
.filter{"!nOedRLmEXx"},
.pageSize{3},
});
std::cout << "Received " << res.items.size() << " comments." << std::endl;
std::cout << "Samples: " << std::endl;
for (auto& comment : res.items) {
std::cout << comment.body_markdown << " - " << comment.owner.display_name << std::endl;
}
}
Example run:
Received 3 comments.
Samples:
I tried this and it didn't work - user4951
@IanAbbott new IRQ stuff goes to _/sys/kernel/irq/$IRQNUMBER_ (and to be precise this has to be _$vIRQNUMBER_, note **v** there for virtual, a.k.a. Linux IRQ). - 0andriy
I've updated my question, explaining where my idea comes from. - Dominique
Additional demos can be found on GitHub. See also boson-light.cpp (not currently listed on stackapps) for a real-world application of the library.
About
I needed a (non-boost) C++ API library for Stuff™. So far, I've stuck to a single highly specialised and minimal implementation (that this library is based on), but because I have plans to write another bot, that strategy is now off the table.
The library uses cpr for web requests, nlohmann/json for JSON parsing and deserialisation, and spdlog for error logging (libfmt is explicitly downloaded as well), all of which are automagically handled thanks to FetchContent.
It runs on v2.3 of the API at the time of writing, but that can be changed. Note that the data objects are set up to follow the latest version, so drastically different versions will not work with the default data objects. Also, data objects can be provided by the user, so if there are new endpoints that I haven't gotten around to, or you have custom data objects partly (but not fully) based on API data, those can be plugged in. Data objects only need a from_json
method to be compatible with nlohmann/json deserialisation.
Note that the library is currently a work in progress. It's lacking in documentation and data objects for return values, but is otherwise fully functional. I'll be updating it and another upcoming library for chat (different post coming soon:tm:) over the next few weeks to get it up and running. This notice will be removed and replaced with documentation links when most/all the API objects have been implemented (which is trivial, but tedious), and documentation has been written.
License
MIT. See LICENSE
Source
Platform
Theoretically Windows, MacOS, and Linux, but I've only verified that it works on Linux. Should also work on any other platforms able to compile C++20 and the dependencies - open an issue if it doesn't.
Contact
Use GitHub to report any issues. General questions can be posted on GitHub Discussions.
Integration
The recommended integration approach is with FetchContent:
include (FetchContent)
FetchContent_Declare(stackapi
GIT_REPOSITORY https://github.com/LunarWatcher/stackapi.cpp)
FetchContent_MakeAvailable(stackapi)
# ...
target_link_libraries(your_program stackapi)
Using submodules, as well as general use, is covered in the documentation