-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example using the library as a cmake external project.
This closes #29
- Loading branch information
Showing
4 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
cmake_minimum_required(VERSION 3.0) | ||
project (example) | ||
|
||
find_package(ZLIB REQUIRED) | ||
find_package(OpenSSL REQUIRED) | ||
find_package(Boost REQUIRED COMPONENTS | ||
system | ||
program_options | ||
filesystem | ||
date_time | ||
context | ||
coroutine | ||
chrono | ||
log | ||
) | ||
|
||
include(cmake/external-projects.cmake) | ||
|
||
add_executable(${PROJECT_NAME} main.cpp) | ||
add_dependencies(${PROJECT_NAME} externalRestcCpp) | ||
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14) | ||
target_link_libraries(${PROJECT_NAME} | ||
restc-cpp | ||
${Boost_LIBRARIES} | ||
${ZLIB_LIBRARIES} | ||
${OPENSSL_LIBRARIES} | ||
) | ||
|
||
add_definitions( | ||
-DBOOST_COROUTINE_NO_DEPRECATION_WARNING=1 | ||
-DBOOST_ALL_DYN_LINK=1 | ||
) |
23 changes: 23 additions & 0 deletions
23
examples/cmake_external_project/cmake/external-projects.cmake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Here are registered all external projects | ||
# | ||
# Usage: | ||
# add_dependencies(TARGET externalProjectName) | ||
# target_link_libraries(TARGET PRIVATE ExternalLibraryName) | ||
|
||
set(EXTERNAL_PROJECTS_PREFIX ${CMAKE_BINARY_DIR}/external-projects) | ||
set(EXTERNAL_PROJECTS_INSTALL_PREFIX ${EXTERNAL_PROJECTS_PREFIX}/installed) | ||
|
||
include(GNUInstallDirs) | ||
|
||
# MUST be called before any add_executable() # https://stackoverflow.com/a/40554704/8766845 | ||
link_directories(${EXTERNAL_PROJECTS_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}) | ||
include_directories($<BUILD_INTERFACE:${EXTERNAL_PROJECTS_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}>) | ||
|
||
include(ExternalProject) | ||
|
||
ExternalProject_Add(externalRestcCpp | ||
PREFIX "${EXTERNAL_PROJECTS_PREFIX}" | ||
GIT_REPOSITORY "https://github.com/jgaa/restc-cpp.git" | ||
GIT_TAG "master" | ||
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_PROJECTS_INSTALL_PREFIX} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// We use the example from the main readme file | ||
|
||
#include <iostream> | ||
|
||
#include <boost/lexical_cast.hpp> | ||
#include <boost/fusion/adapted.hpp> | ||
|
||
#define BOOST_LOG_DYN_LINK 1 | ||
#include <boost/log/trivial.hpp> | ||
#include <boost/log/utility/setup.hpp> | ||
|
||
#include <restc-cpp/restc-cpp.h> | ||
#include <restc-cpp/RequestBuilder.h> | ||
|
||
using namespace std; | ||
using namespace restc_cpp; | ||
namespace logging = boost::log; | ||
|
||
// C++ structure that match the JSON entries received | ||
// from http://jsonplaceholder.typicode.com/posts/{id} | ||
struct Post { | ||
int userId = 0; | ||
int id = 0; | ||
string title; | ||
string body; | ||
}; | ||
|
||
// Since C++ does not (yet) offer reflection, we need to tell the library how | ||
// to map json members to a type. We are doing this by declaring the | ||
// structs/classes with BOOST_FUSION_ADAPT_STRUCT from the boost libraries. | ||
// This allows us to convert the C++ classes to and from JSON. | ||
|
||
BOOST_FUSION_ADAPT_STRUCT( | ||
Post, | ||
(int, userId) | ||
(int, id) | ||
(string, title) | ||
(string, body) | ||
) | ||
|
||
// The C++ main function - the place where any adventure starts | ||
int main() { | ||
// Set the log-level to a reasonable value | ||
logging::core::get()->set_filter | ||
( | ||
logging::trivial::severity >= logging::trivial::info | ||
); | ||
|
||
// Create an instance of the rest client | ||
auto rest_client = RestClient::Create(); | ||
|
||
// Create and instantiate a Post from data received from the server. | ||
Post my_post = rest_client->ProcessWithPromiseT<Post>([&](Context& ctx) { | ||
// This is a co-routine, running in a worker-thread | ||
|
||
// Instantiate a Post structure. | ||
Post post; | ||
|
||
// Serialize it asynchronously. The asynchronously part does not really matter | ||
// here, but it may if you receive huge data structures. | ||
SerializeFromJson(post, | ||
|
||
// Construct a request to the server | ||
RequestBuilder(ctx) | ||
.Get("http://jsonplaceholder.typicode.com/posts/1") | ||
|
||
// Add some headers for good taste | ||
.Header("X-Client", "RESTC_CPP") | ||
.Header("X-Client-Purpose", "Testing") | ||
|
||
// Send the request | ||
.Execute()); | ||
|
||
// Return the post instance trough a C++ future<> | ||
return post; | ||
}) | ||
|
||
// Get the Post instance from the future<>, or any C++ exception thrown | ||
// within the lambda. | ||
.get(); | ||
|
||
// Print the result for everyone to see. | ||
cout << "Received post# " << my_post.id << ", title: " << my_post.title | ||
<< endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# example with cmake and restc-cpp as an external project | ||
|
||
This example shows how to compile a program using restc-cpp from cmake, delegating the compilation of restc-cpp to cmake. | ||
|
||
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. | ||
|
||
Currently this example is only tested under Linux. | ||
|
||
```sh | ||
~$ rm -rf build/ | ||
~$ mkdir build | ||
~$ cd build/ | ||
~$ cmake .. | ||
~$ make | ||
``` |