Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C++] [cpprestsdk] Add examples and test for cpprestsdk #3109

Merged
merged 2 commits into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/cpp-restsdk-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -t modules/openapi-generator/src/main/resources/cpp-rest-sdk-client -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g cpp-restsdk -o samples/client/petstore/cpp-restsdk $@"
ags="generate -t modules/openapi-generator/src/main/resources/cpp-rest-sdk-client -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g cpp-restsdk -o samples/client/petstore/cpp-restsdk/client $@"

java $JAVA_OPTS -jar $executable $ags
2 changes: 1 addition & 1 deletion bin/windows/cpp-restsdk-petstore.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ If Not Exist %executable% (
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g cpp-restsdk -o samples\client\petstore\cpp-restsdk
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g cpp-restsdk -o samples\client\petstore\cpp-restsdk\client

java %JAVA_OPTS% -jar %executable% %ags%
91 changes: 37 additions & 54 deletions samples/client/petstore/cpp-restsdk/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,54 +1,37 @@
#
# OpenAPI Petstore
# This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
#
# OpenAPI spec version: 1.0.0
#
# https://openapi-generator.tech
#
# NOTE: Auto generated by OpenAPI Generator (https://openapi-generator.tech).

cmake_minimum_required (VERSION 2.8)

#PROJECT's NAME
project(CppRestOpenAPIClient)


# THE LOCATION OF OUTPUT BINARIES
set(CMAKE_LIBRARY_DIR ${PROJECT_SOURCE_DIR}/lib)
set(LIBRARY_OUTPUT_PATH ${CMAKE_LIBRARY_DIR})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

# BUILD TYPE
message("A ${CMAKE_BUILD_TYPE} build configuration is detected")

# Update require components as necessary
#find_package(Boost 1.45.0 REQUIRED COMPONENTS ${Boost_THREAD_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_REGEX_LIBRARY} ${Boost_DATE_TIME_LIBRARY} ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_FILESYSTEM_LIBRARY})

# build and set path to cpp rest sdk
set(CPPREST_ROOT ${PROJECT_SOURCE_DIR}/3rdParty/cpprest)
set(CPPREST_INCLUDE_DIR ${CPPREST_ROOT}/include)
set(CPPREST_LIBRARY_DIR ${CPPREST_ROOT}/lib)

include_directories(${PROJECT_SOURCE_DIR} api model ${CPPREST_INCLUDE_DIR})


# If using vcpkg, set include directories. Also comment out CPPREST section above since vcpkg will handle it.
# To install required vcpkg packages execute:
# > vcpkg install cpprestsdk cpprestsdk:x64-windows boost-uuid boost-uuid:x64-windows
# set(VCPKG_ROOT "C:\\vcpkg\\installed\\x64-windows")
# set(VCPKG_INCLUDE_DIR ${VCPKG_ROOT}/include)
# set(VCPKG_LIBRARY_DIR ${VCPKG_ROOT}/lib)
# include_directories(${PROJECT_SOURCE_DIR} api model ${VCPKG_INCLUDE_DIR})

#SUPPORTING FILES
set(SUPPORTING_FILES "ApiClient" "ApiConfiguration" "ApiException" "HttpContent" "IHttpBody" "JsonBody" "ModelBase" "MultipartFormData" "Object")
#SOURCE FILES
file(GLOB SOURCE_FILES "api/*" "model/*")

add_library(${PROJECT_NAME} ${SUPPORTING_FILES} ${SOURCE_FILES})
cmake_minimum_required (VERSION 3.2)

project(cpprest-petstore)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)


set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -Wall -Wno-unused-variable")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -std=c++14 -Wall -Wno-unused-variable")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg -g3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg -g3")
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_PREFIX_PATH /usr/lib/x86_64-linux-gnu/cmake)
find_package(cpprestsdk REQUIRED)
find_package(Boost REQUIRED)
add_subdirectory(client)

file(GLOB SRCS
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/client
${CMAKE_CURRENT_SOURCE_DIR}/client/model
${CMAKE_CURRENT_SOURCE_DIR}/client/api
)

link_directories(
${Boost_LIBRARY_DIRS}
)
add_executable(${PROJECT_NAME} ${SRCS})
add_dependencies(${PROJECT_NAME} CppRestOpenAPIClient )
target_link_libraries(${PROJECT_NAME} CppRestOpenAPIClient cpprest pthread boost_system crypto)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)

install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)
92 changes: 92 additions & 0 deletions samples/client/petstore/cpp-restsdk/PetApiTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "PetApiTests.h"
#include <iostream>
#include <chrono>
#include <thread>
#include <functional>

OAIPetApiTests::OAIPetApiTests(std::string host, std::string basePath){
apiconfiguration = std::make_shared<ApiConfiguration>();
apiconfiguration->setBaseUrl(host + basePath);
apiconfiguration->setUserAgent(U("OpenAPI Client"));
apiclient = std::make_shared<ApiClient>(apiconfiguration);
api = std::make_shared<PetApi>(apiclient);
}

OAIPetApiTests::~OAIPetApiTests() {

}

void OAIPetApiTests::runTests(){
testAddPet();
testFindPetsByStatus();
testGetPetById();
}

void OAIPetApiTests::testAddPet(){
auto req = std::make_shared<Pet>();
req->setId(12345);
req->setName("cpprest-pet");
req->setStatus(U("123"));

std::function<void()> responseCallback = []()
{
std::cout << "added pet successfully" << std::endl;
};

auto reqTask = api->addPet(req).then(responseCallback);
try{
reqTask.wait();
}
catch(const ApiException& ex){
std::cout << ex.what() << std::endl << std::flush;
std::string err(ex.what());
}
catch(const std::exception &ex){
std::cout << ex.what() << std::endl << std::flush;
std::string err(ex.what());
}
}

void OAIPetApiTests::testFindPetsByStatus(){
auto req = std::vector<utility::string_t>();
req.push_back(U("123"));
auto reqTask = api->findPetsByStatus(req)
.then([=](std::vector<std::shared_ptr<Pet>> pets)
{
std::cout << "found pet successfully" << std::endl;
});
try{
reqTask.wait();
}
catch(const ApiException& ex){
std::cout << ex.what() << std::endl << std::flush;
std::string err(ex.what());
}
catch(const std::exception &ex){
std::cout << ex.what() << std::endl << std::flush;
std::string err(ex.what());
}
}

void OAIPetApiTests::testGetPetById(){
int req = 12345;
auto responseCallback = std::bind(&OAIPetApiTests::getPetByIdCallback, this, std::placeholders::_1);

auto reqTask = api->getPetById(req).then(responseCallback);

try{
reqTask.wait();
}
catch(const ApiException& ex){
std::cout << ex.what() << std::endl << std::flush;
std::string err(ex.what());
}
catch(const std::exception &ex){
std::cout << ex.what() << std::endl << std::flush;
std::string err(ex.what());
}
}

void OAIPetApiTests::getPetByIdCallback(std::shared_ptr<Pet> pet){
std::cout << "found pet by id successfully" << std::endl;
}
34 changes: 34 additions & 0 deletions samples/client/petstore/cpp-restsdk/PetApiTests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef OAI_PETAPI_TESTS_H
#define OAI_PETAPI_TESTS_H

#include <memory>
#include "client/ApiClient.h"
#include "client/ApiConfiguration.h"
#include "client/api/PetApi.h"
#include "client/model/Pet.h"


using namespace std;
using namespace org::openapitools::client::api;

class OAIPetApiTests
{
public:
explicit OAIPetApiTests(std::string host = U("http://petstore.swagger.io"), std::string basePath = U("/v2"));

virtual ~OAIPetApiTests();
public:
void runTests();
private:
void testAddPet();
void testFindPetsByStatus();
void testGetPetById();

void getPetByIdCallback(std::shared_ptr<Pet> pet);

std::shared_ptr<ApiConfiguration> apiconfiguration;
std::shared_ptr<ApiClient> apiclient;
std::shared_ptr<PetApi> api;
};

#endif // OAI_PETAPI_TESTS_H
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
* The version of the OpenAPI document: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.2-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
* The version of the OpenAPI document: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.2-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
* The version of the OpenAPI document: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.2-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
* The version of the OpenAPI document: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.2-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
* The version of the OpenAPI document: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.2-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
* The version of the OpenAPI document: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.2-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
Expand Down
54 changes: 54 additions & 0 deletions samples/client/petstore/cpp-restsdk/client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#
# OpenAPI Petstore
# This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
#
# The version of the OpenAPI document: 1.0.0
#
# https://openapi-generator.tech
#
# NOTE: Auto generated by OpenAPI Generator (https://openapi-generator.tech).

cmake_minimum_required (VERSION 2.8)

#PROJECT's NAME
project(CppRestOpenAPIClient)


# THE LOCATION OF OUTPUT BINARIES
set(CMAKE_LIBRARY_DIR ${PROJECT_SOURCE_DIR}/lib)
set(LIBRARY_OUTPUT_PATH ${CMAKE_LIBRARY_DIR})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

# BUILD TYPE
message("A ${CMAKE_BUILD_TYPE} build configuration is detected")

# Update require components as necessary
#find_package(Boost 1.45.0 REQUIRED COMPONENTS ${Boost_THREAD_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_REGEX_LIBRARY} ${Boost_DATE_TIME_LIBRARY} ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_FILESYSTEM_LIBRARY})

# build and set path to cpp rest sdk
set(CPPREST_ROOT ${PROJECT_SOURCE_DIR}/3rdParty/cpprest)
set(CPPREST_INCLUDE_DIR ${CPPREST_ROOT}/include)
set(CPPREST_LIBRARY_DIR ${CPPREST_ROOT}/lib)

include_directories(${PROJECT_SOURCE_DIR} api model ${CPPREST_INCLUDE_DIR})


# If using vcpkg, set include directories. Also comment out CPPREST section above since vcpkg will handle it.
# To install required vcpkg packages execute:
# > vcpkg install cpprestsdk cpprestsdk:x64-windows boost-uuid boost-uuid:x64-windows
# set(VCPKG_ROOT "C:\\vcpkg\\installed\\x64-windows")
# set(VCPKG_INCLUDE_DIR ${VCPKG_ROOT}/include)
# set(VCPKG_LIBRARY_DIR ${VCPKG_ROOT}/lib)
# include_directories(${PROJECT_SOURCE_DIR} api model ${VCPKG_INCLUDE_DIR})

#SUPPORTING FILES
set(SUPPORTING_FILES "ApiClient" "ApiConfiguration" "ApiException" "HttpContent" "IHttpBody" "JsonBody" "ModelBase" "MultipartFormData" "Object")
#SOURCE FILES
file(GLOB SOURCE_FILES "api/*" "model/*")

add_library(${PROJECT_NAME} ${SUPPORTING_FILES} ${SOURCE_FILES})
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
* The version of the OpenAPI document: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.2-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
* The version of the OpenAPI document: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.2-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
* The version of the OpenAPI document: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.2-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
Expand Down
Loading