6

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&#39;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&#39;ve updated my question, explaining where my idea comes from. - Dominique

Additional demos can be found on GitHub

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

GitHub

Platform

Theoretically Windows, MacOS, and Linux, but I've only verified that it works on Linux. May also work on any other platforms able to compile C++20 and the dependencies.

Contact

Use GitHub to report any issues. General questions can be posted on GitHub Discussions.

Integration

The recommended integration approach is with submodules (git submodule add https://github.com/LunarWatcher/stackapi.cpp). Alternatively, FetchContent can be used, but this is left as an exercise to the reader for no reason in particular :)

With submodules, integration is done with two lines of code:

add_subdirectory(stackapi.cpp)

# ...

target_link_libraries(api-stience stackapi)

0

You must log in to answer this question.

Browse other questions tagged .