Skip to content

Commit 6c98046

Browse files
authored
[C++] [cpprestsdk] Add examples and test for cpprestsdk (#3109)
Add examples and test for cpprestsdk
1 parent 3df525e commit 6c98046

45 files changed

Lines changed: 295 additions & 126 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bin/cpp-restsdk-petstore.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ fi
2727

2828
# if you've executed sbt assembly previously it will use that instead.
2929
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
30-
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 $@"
30+
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 $@"
3131

3232
java $JAVA_OPTS -jar $executable $ags

bin/windows/cpp-restsdk-petstore.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ If Not Exist %executable% (
55
)
66

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

1010
java %JAVA_OPTS% -jar %executable% %ags%
Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,37 @@
1-
#
2-
# OpenAPI Petstore
3-
# This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
4-
#
5-
# OpenAPI spec version: 1.0.0
6-
#
7-
# https://openapi-generator.tech
8-
#
9-
# NOTE: Auto generated by OpenAPI Generator (https://openapi-generator.tech).
10-
11-
cmake_minimum_required (VERSION 2.8)
12-
13-
#PROJECT's NAME
14-
project(CppRestOpenAPIClient)
15-
16-
17-
# THE LOCATION OF OUTPUT BINARIES
18-
set(CMAKE_LIBRARY_DIR ${PROJECT_SOURCE_DIR}/lib)
19-
set(LIBRARY_OUTPUT_PATH ${CMAKE_LIBRARY_DIR})
20-
21-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
22-
23-
if(NOT CMAKE_BUILD_TYPE)
24-
set(CMAKE_BUILD_TYPE Release)
25-
endif()
26-
27-
# BUILD TYPE
28-
message("A ${CMAKE_BUILD_TYPE} build configuration is detected")
29-
30-
# Update require components as necessary
31-
#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})
32-
33-
# build and set path to cpp rest sdk
34-
set(CPPREST_ROOT ${PROJECT_SOURCE_DIR}/3rdParty/cpprest)
35-
set(CPPREST_INCLUDE_DIR ${CPPREST_ROOT}/include)
36-
set(CPPREST_LIBRARY_DIR ${CPPREST_ROOT}/lib)
37-
38-
include_directories(${PROJECT_SOURCE_DIR} api model ${CPPREST_INCLUDE_DIR})
39-
40-
41-
# If using vcpkg, set include directories. Also comment out CPPREST section above since vcpkg will handle it.
42-
# To install required vcpkg packages execute:
43-
# > vcpkg install cpprestsdk cpprestsdk:x64-windows boost-uuid boost-uuid:x64-windows
44-
# set(VCPKG_ROOT "C:\\vcpkg\\installed\\x64-windows")
45-
# set(VCPKG_INCLUDE_DIR ${VCPKG_ROOT}/include)
46-
# set(VCPKG_LIBRARY_DIR ${VCPKG_ROOT}/lib)
47-
# include_directories(${PROJECT_SOURCE_DIR} api model ${VCPKG_INCLUDE_DIR})
48-
49-
#SUPPORTING FILES
50-
set(SUPPORTING_FILES "ApiClient" "ApiConfiguration" "ApiException" "HttpContent" "IHttpBody" "JsonBody" "ModelBase" "MultipartFormData" "Object")
51-
#SOURCE FILES
52-
file(GLOB SOURCE_FILES "api/*" "model/*")
53-
54-
add_library(${PROJECT_NAME} ${SUPPORTING_FILES} ${SOURCE_FILES})
1+
cmake_minimum_required (VERSION 3.2)
2+
3+
project(cpprest-petstore)
4+
set(CMAKE_VERBOSE_MAKEFILE ON)
5+
set(CMAKE_INCLUDE_CURRENT_DIR ON)
6+
7+
8+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -Wall -Wno-unused-variable")
9+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -std=c++14 -Wall -Wno-unused-variable")
10+
11+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg -g3")
12+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg -g3")
13+
set(CMAKE_BUILD_TYPE Debug)
14+
set(CMAKE_PREFIX_PATH /usr/lib/x86_64-linux-gnu/cmake)
15+
find_package(cpprestsdk REQUIRED)
16+
find_package(Boost REQUIRED)
17+
add_subdirectory(client)
18+
19+
file(GLOB SRCS
20+
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
21+
)
22+
include_directories(
23+
${CMAKE_CURRENT_SOURCE_DIR}/client
24+
${CMAKE_CURRENT_SOURCE_DIR}/client/model
25+
${CMAKE_CURRENT_SOURCE_DIR}/client/api
26+
)
27+
28+
link_directories(
29+
${Boost_LIBRARY_DIRS}
30+
)
31+
add_executable(${PROJECT_NAME} ${SRCS})
32+
add_dependencies(${PROJECT_NAME} CppRestOpenAPIClient )
33+
target_link_libraries(${PROJECT_NAME} CppRestOpenAPIClient cpprest pthread boost_system crypto)
34+
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14)
35+
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
36+
37+
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include "PetApiTests.h"
2+
#include <iostream>
3+
#include <chrono>
4+
#include <thread>
5+
#include <functional>
6+
7+
OAIPetApiTests::OAIPetApiTests(std::string host, std::string basePath){
8+
apiconfiguration = std::make_shared<ApiConfiguration>();
9+
apiconfiguration->setBaseUrl(host + basePath);
10+
apiconfiguration->setUserAgent(U("OpenAPI Client"));
11+
apiclient = std::make_shared<ApiClient>(apiconfiguration);
12+
api = std::make_shared<PetApi>(apiclient);
13+
}
14+
15+
OAIPetApiTests::~OAIPetApiTests() {
16+
17+
}
18+
19+
void OAIPetApiTests::runTests(){
20+
testAddPet();
21+
testFindPetsByStatus();
22+
testGetPetById();
23+
}
24+
25+
void OAIPetApiTests::testAddPet(){
26+
auto req = std::make_shared<Pet>();
27+
req->setId(12345);
28+
req->setName("cpprest-pet");
29+
req->setStatus(U("123"));
30+
31+
std::function<void()> responseCallback = []()
32+
{
33+
std::cout << "added pet successfully" << std::endl;
34+
};
35+
36+
auto reqTask = api->addPet(req).then(responseCallback);
37+
try{
38+
reqTask.wait();
39+
}
40+
catch(const ApiException& ex){
41+
std::cout << ex.what() << std::endl << std::flush;
42+
std::string err(ex.what());
43+
}
44+
catch(const std::exception &ex){
45+
std::cout << ex.what() << std::endl << std::flush;
46+
std::string err(ex.what());
47+
}
48+
}
49+
50+
void OAIPetApiTests::testFindPetsByStatus(){
51+
auto req = std::vector<utility::string_t>();
52+
req.push_back(U("123"));
53+
auto reqTask = api->findPetsByStatus(req)
54+
.then([=](std::vector<std::shared_ptr<Pet>> pets)
55+
{
56+
std::cout << "found pet successfully" << std::endl;
57+
});
58+
try{
59+
reqTask.wait();
60+
}
61+
catch(const ApiException& ex){
62+
std::cout << ex.what() << std::endl << std::flush;
63+
std::string err(ex.what());
64+
}
65+
catch(const std::exception &ex){
66+
std::cout << ex.what() << std::endl << std::flush;
67+
std::string err(ex.what());
68+
}
69+
}
70+
71+
void OAIPetApiTests::testGetPetById(){
72+
int req = 12345;
73+
auto responseCallback = std::bind(&OAIPetApiTests::getPetByIdCallback, this, std::placeholders::_1);
74+
75+
auto reqTask = api->getPetById(req).then(responseCallback);
76+
77+
try{
78+
reqTask.wait();
79+
}
80+
catch(const ApiException& ex){
81+
std::cout << ex.what() << std::endl << std::flush;
82+
std::string err(ex.what());
83+
}
84+
catch(const std::exception &ex){
85+
std::cout << ex.what() << std::endl << std::flush;
86+
std::string err(ex.what());
87+
}
88+
}
89+
90+
void OAIPetApiTests::getPetByIdCallback(std::shared_ptr<Pet> pet){
91+
std::cout << "found pet by id successfully" << std::endl;
92+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef OAI_PETAPI_TESTS_H
2+
#define OAI_PETAPI_TESTS_H
3+
4+
#include <memory>
5+
#include "client/ApiClient.h"
6+
#include "client/ApiConfiguration.h"
7+
#include "client/api/PetApi.h"
8+
#include "client/model/Pet.h"
9+
10+
11+
using namespace std;
12+
using namespace org::openapitools::client::api;
13+
14+
class OAIPetApiTests
15+
{
16+
public:
17+
explicit OAIPetApiTests(std::string host = U("http://petstore.swagger.io"), std::string basePath = U("/v2"));
18+
19+
virtual ~OAIPetApiTests();
20+
public:
21+
void runTests();
22+
private:
23+
void testAddPet();
24+
void testFindPetsByStatus();
25+
void testGetPetById();
26+
27+
void getPetByIdCallback(std::shared_ptr<Pet> pet);
28+
29+
std::shared_ptr<ApiConfiguration> apiconfiguration;
30+
std::shared_ptr<ApiClient> apiclient;
31+
std::shared_ptr<PetApi> api;
32+
};
33+
34+
#endif // OAI_PETAPI_TESTS_H

samples/client/petstore/cpp-restsdk/ApiClient.cpp renamed to samples/client/petstore/cpp-restsdk/client/ApiClient.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* OpenAPI Petstore
33
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
44
*
5-
* OpenAPI spec version: 1.0.0
5+
* The version of the OpenAPI document: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.0-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.2-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiClient.h renamed to samples/client/petstore/cpp-restsdk/client/ApiClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* OpenAPI Petstore
33
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
44
*
5-
* OpenAPI spec version: 1.0.0
5+
* The version of the OpenAPI document: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.0-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.2-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiConfiguration.cpp renamed to samples/client/petstore/cpp-restsdk/client/ApiConfiguration.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* OpenAPI Petstore
33
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
44
*
5-
* OpenAPI spec version: 1.0.0
5+
* The version of the OpenAPI document: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.0-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.2-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiConfiguration.h renamed to samples/client/petstore/cpp-restsdk/client/ApiConfiguration.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* OpenAPI Petstore
33
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
44
*
5-
* OpenAPI spec version: 1.0.0
5+
* The version of the OpenAPI document: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.0-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.2-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiException.cpp renamed to samples/client/petstore/cpp-restsdk/client/ApiException.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* OpenAPI Petstore
33
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
44
*
5-
* OpenAPI spec version: 1.0.0
5+
* The version of the OpenAPI document: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.0-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 4.0.2-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

0 commit comments

Comments
 (0)