Skip to content

Commit 2616792

Browse files
committed
Added example using the library as a cmake external project.
This closes jgaa#29
1 parent 8c8fe0c commit 2616792

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
cmake_minimum_required(VERSION 3.0)
2+
project (example)
3+
4+
find_package(ZLIB REQUIRED)
5+
find_package(OpenSSL REQUIRED)
6+
find_package(Boost REQUIRED COMPONENTS
7+
system
8+
program_options
9+
filesystem
10+
date_time
11+
context
12+
coroutine
13+
chrono
14+
log
15+
)
16+
17+
include(cmake/external-projects.cmake)
18+
19+
add_executable(${PROJECT_NAME} main.cpp)
20+
add_dependencies(${PROJECT_NAME} externalRestcCpp)
21+
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14)
22+
target_link_libraries(${PROJECT_NAME}
23+
restc-cpp
24+
${Boost_LIBRARIES}
25+
${ZLIB_LIBRARIES}
26+
${OPENSSL_LIBRARIES}
27+
)
28+
29+
add_definitions(
30+
-DBOOST_COROUTINE_NO_DEPRECATION_WARNING=1
31+
-DBOOST_ALL_DYN_LINK=1
32+
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Here are registered all external projects
2+
#
3+
# Usage:
4+
# add_dependencies(TARGET externalProjectName)
5+
# target_link_libraries(TARGET PRIVATE ExternalLibraryName)
6+
7+
set(EXTERNAL_PROJECTS_PREFIX ${CMAKE_BINARY_DIR}/external-projects)
8+
set(EXTERNAL_PROJECTS_INSTALL_PREFIX ${EXTERNAL_PROJECTS_PREFIX}/installed)
9+
10+
include(GNUInstallDirs)
11+
12+
# MUST be called before any add_executable() # https://stackoverflow.com/a/40554704/8766845
13+
link_directories(${EXTERNAL_PROJECTS_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
14+
include_directories($<BUILD_INTERFACE:${EXTERNAL_PROJECTS_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}>)
15+
16+
include(ExternalProject)
17+
18+
ExternalProject_Add(externalRestcCpp
19+
PREFIX "${EXTERNAL_PROJECTS_PREFIX}"
20+
GIT_REPOSITORY "https://github.com/jgaa/restc-cpp.git"
21+
GIT_TAG "master"
22+
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_PROJECTS_INSTALL_PREFIX}
23+
)
Lines changed: 85 additions & 0 deletions
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# example with cmake and restc-cpp as an external project
2+
3+
This example shows how to compile a program using restc-cpp from cmake, delegating the compilation of restc-cpp to cmake.
4+
5+
This is probably the simplest way to use the library, but the external project dependencies does add some extra time each time you run make.
6+
7+
Currently this example is only tested under Linux.
8+
9+
```sh
10+
~$ rm -rf build/
11+
~$ mkdir build
12+
~$ cd build/
13+
~$ cmake ..
14+
~$ make
15+
```

0 commit comments

Comments
 (0)