|
| 1 | +// We use the example from the main readme file |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | + |
| 5 | +#include <boost/lexical_cast.hpp> |
| 6 | +#include <boost/fusion/adapted.hpp> |
| 7 | + |
| 8 | +#define BOOST_LOG_DYN_LINK 1 |
| 9 | +#include <boost/log/trivial.hpp> |
| 10 | +#include <boost/log/utility/setup.hpp> |
| 11 | + |
| 12 | +#include <restc-cpp/restc-cpp.h> |
| 13 | +#include <restc-cpp/RequestBuilder.h> |
| 14 | + |
| 15 | +using namespace std; |
| 16 | +using namespace restc_cpp; |
| 17 | +namespace logging = boost::log; |
| 18 | + |
| 19 | +// C++ structure that match the JSON entries received |
| 20 | +// from http://jsonplaceholder.typicode.com/posts/{id} |
| 21 | +struct Post { |
| 22 | + int userId = 0; |
| 23 | + int id = 0; |
| 24 | + string title; |
| 25 | + string body; |
| 26 | +}; |
| 27 | + |
| 28 | +// Since C++ does not (yet) offer reflection, we need to tell the library how |
| 29 | +// to map json members to a type. We are doing this by declaring the |
| 30 | +// structs/classes with BOOST_FUSION_ADAPT_STRUCT from the boost libraries. |
| 31 | +// This allows us to convert the C++ classes to and from JSON. |
| 32 | + |
| 33 | +BOOST_FUSION_ADAPT_STRUCT( |
| 34 | + Post, |
| 35 | + (int, userId) |
| 36 | + (int, id) |
| 37 | + (string, title) |
| 38 | + (string, body) |
| 39 | +) |
| 40 | + |
| 41 | +// The C++ main function - the place where any adventure starts |
| 42 | +int main() { |
| 43 | + // Set the log-level to a reasonable value |
| 44 | + logging::core::get()->set_filter |
| 45 | + ( |
| 46 | + logging::trivial::severity >= logging::trivial::info |
| 47 | + ); |
| 48 | + |
| 49 | + // Create an instance of the rest client |
| 50 | + auto rest_client = RestClient::Create(); |
| 51 | + |
| 52 | + // Create and instantiate a Post from data received from the server. |
| 53 | + Post my_post = rest_client->ProcessWithPromiseT<Post>([&](Context& ctx) { |
| 54 | + // This is a co-routine, running in a worker-thread |
| 55 | + |
| 56 | + // Instantiate a Post structure. |
| 57 | + Post post; |
| 58 | + |
| 59 | + // Serialize it asynchronously. The asynchronously part does not really matter |
| 60 | + // here, but it may if you receive huge data structures. |
| 61 | + SerializeFromJson(post, |
| 62 | + |
| 63 | + // Construct a request to the server |
| 64 | + RequestBuilder(ctx) |
| 65 | + .Get("http://jsonplaceholder.typicode.com/posts/1") |
| 66 | + |
| 67 | + // Add some headers for good taste |
| 68 | + .Header("X-Client", "RESTC_CPP") |
| 69 | + .Header("X-Client-Purpose", "Testing") |
| 70 | + |
| 71 | + // Send the request |
| 72 | + .Execute()); |
| 73 | + |
| 74 | + // Return the post instance trough a C++ future<> |
| 75 | + return post; |
| 76 | + }) |
| 77 | + |
| 78 | + // Get the Post instance from the future<>, or any C++ exception thrown |
| 79 | + // within the lambda. |
| 80 | + .get(); |
| 81 | + |
| 82 | + // Print the result for everyone to see. |
| 83 | + cout << "Received post# " << my_post.id << ", title: " << my_post.title |
| 84 | + << endl; |
| 85 | +} |
0 commit comments