Skip to content

Commit d6e5aca

Browse files
committedDec 12, 2017
Added example on how to link with the library from the command-line.
1 parent 908f011 commit d6e5aca

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
 

‎examples/cmdline/build.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
g++ -DBOOST_COROUTINE_NO_DEPRECATION_WARNING main.cpp \
2+
-lrestc-cpp -lz -lssl -lcrypto -lpthread \
3+
-lboost_system -lboost_program_options \
4+
-lboost_filesystem -lboost_date_time \
5+
-lboost_context -lboost_coroutine \
6+
-lboost_chrono -lboost_log -lboost_thread \
7+
-lboost_log_setup -lboost_regex \
8+
-lboost_atomic -lpthread

‎examples/cmdline/main.cpp

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}

‎examples/cmdline/readme.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# example cmdline
2+
3+
This example shows how to compile a program using restc-cpp from the command-line.
4+
5+
We assume that the library has been built and installed.
6+
7+
Currently this example is only tested under Linux.
8+
9+
```sh
10+
~/src/restc-cpp/examples/cmdline$ ./build.sh
11+
~/src/restc-cpp/examples/cmdline$ ./a.out
12+
Received post# 1, title: sunt aut facere repellat provident occaecati excepturi optio reprehenderit
13+
~/src/restc-cpp/examples/cmdline$
14+
```
15+
16+
Please examine [build.sh](build.sh) to see the full list of libraries to link with.
17+

0 commit comments

Comments
 (0)
Please sign in to comment.