Skip to content
Permalink
Browse files

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

Add examples and test for cpprestsdk
  • Loading branch information...
etherealjoy committed Jun 7, 2019
1 parent 3df525e commit 6c98046ee6f6312dac802f2f55441118b679d690
Showing with 295 additions and 126 deletions.
  1. +1 −1 bin/cpp-restsdk-petstore.sh
  2. +1 −1 bin/windows/cpp-restsdk-petstore.bat
  3. +37 −54 samples/client/petstore/cpp-restsdk/CMakeLists.txt
  4. +92 −0 samples/client/petstore/cpp-restsdk/PetApiTests.cpp
  5. +34 −0 samples/client/petstore/cpp-restsdk/PetApiTests.h
  6. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/ApiClient.cpp
  7. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/ApiClient.h
  8. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/ApiConfiguration.cpp
  9. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/ApiConfiguration.h
  10. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/ApiException.cpp
  11. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/ApiException.h
  12. +54 −0 samples/client/petstore/cpp-restsdk/client/CMakeLists.txt
  13. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/HttpContent.cpp
  14. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/HttpContent.h
  15. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/IHttpBody.h
  16. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/JsonBody.cpp
  17. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/JsonBody.h
  18. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/ModelBase.cpp
  19. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/ModelBase.h
  20. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/MultipartFormData.cpp
  21. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/MultipartFormData.h
  22. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/Object.cpp
  23. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/Object.h
  24. 0 samples/client/petstore/cpp-restsdk/{ → client}/README.md
  25. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/api/PetApi.cpp
  26. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/api/PetApi.h
  27. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/api/StoreApi.cpp
  28. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/api/StoreApi.h
  29. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/api/UserApi.cpp
  30. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/api/UserApi.h
  31. 0 samples/client/petstore/cpp-restsdk/{ → client}/git_push.sh
  32. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/model/ApiResponse.cpp
  33. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/model/ApiResponse.h
  34. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/model/Category.cpp
  35. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/model/Category.h
  36. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/model/Order.cpp
  37. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/model/Order.h
  38. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/model/Pet.cpp
  39. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/model/Pet.h
  40. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/model/Tag.cpp
  41. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/model/Tag.h
  42. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/model/User.cpp
  43. +2 −2 samples/client/petstore/cpp-restsdk/{ → client}/model/User.h
  44. 0 samples/client/petstore/cpp-restsdk/{ → client}/pom.xml
  45. +6 −0 samples/client/petstore/cpp-restsdk/main.cpp
@@ -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
@@ -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%
@@ -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)
@@ -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;
}
@@ -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
@@ -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.
*/
@@ -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.
*/
@@ -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.
*/
@@ -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.
*/
@@ -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.
*/
@@ -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.
*/
@@ -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})
@@ -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.
*/
@@ -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.
*/
@@ -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.
*/

0 comments on commit 6c98046

Please sign in to comment.
You can’t perform that action at this time.