Skip to content

Commit 0f60098

Browse files
PowerOfCreationwing328
authored andcommitted
[C] Adding petstore sample written in C (OpenAPITools#306)
* Added a .gitignore to ignore the build folder * Added a CMakeLists and a basic implementation of a double linked list * Added the pet model * changed the behaviour when a list gets freed - the data of each element doesn't get freed anymore * Added the tool uncrustify in order to make code look better * Uncrustified code * added an implementation(constructor and deconstructor) for the category model * Added a third party JSON library * The pet struct now uses pointers for its members; the pet struct now has a proper constructor and a basic toJSON method * The pet model now gets fully serialized into JSON * Fixed the example url... * Added third party library libcurl * Modified category struct and added an unit test * Added a foreach macro and added two functions * Added a tag model and an unit test * the pet struct now uses no double pointer for it's name anymore and no pointer for the enum status anymore; the pet struct can now be fully converted to json and parsed from json * Added the struct APIClient and an unit test * Uncrustified the unit test for category * Added ifdef in pet.h to prevent errors * Added one API endpoint to get a pet by id * Added a "== 0" comparison that I forgot * Added some kind of debug functionality to test-petApi.c * Removed the DEBUG define * Moved the c petstore example from samples/client/c to samples/client/petstore/c * Renamed function getPetById to petApi_getPetById to avoid name collisions * Removed unecessary method in list.c * Added POST functionality; added petApi_addPet method and improved unit-test for petApi; cleaned up some code in apiClient * removed two methods in list.c(string/tag to JSON) and moved their code directly in the pet_convertToJSON method * Removed old, already commented out, puts artifact in apiClient.c * Added a convertToJSON method to the category model * Added a convertToJSON method to the tag model * changed how the convertToJSON method works in the pet model * Adjusted the unit-tests on how the convertToJSON method now works(now returns a cJSON* instead of a char*) * apiClient_t now needs to be given to API methods as a parameter. This means apiClient_t can now be reused in multiple methods. * Added an untested concept for how authentication could be handled * Tested basicAuth using wireshark and added untested OAuth2 feature * Added support for api key authentication using the http-header and tested functionality using wireshark
1 parent c99b10c commit 0f60098

28 files changed

+4734
-0
lines changed

samples/client/petstore/c/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
cmake_minimum_required (VERSION 2.6)
2+
project (CGenerator)
3+
4+
file(GLOB SRC_C src/*.c)
5+
file(GLOB UNIT_TESTS_C unit-tests/*.c)
6+
file(GLOB MODEL_C model/*.c)
7+
file(GLOB API_C api/*.c)
8+
file(GLOB EXTERNAL_SRC_C external/src/*.c)
9+
set(ALL_SRC_LIST ${SRC_C} ${UNIT_TESTS_C} ${MODEL_C} ${API_C})
10+
11+
include(CTest)
12+
include_directories(include)
13+
include_directories(external/include)
14+
15+
find_program(VALGRIND valgrind)
16+
if(VALGRIND)
17+
set(CMAKE_MEMORYCHECK_COMMAND valgrind)
18+
set(CMAKE_MEMORYCHECK_COMMAND_OPTIONS "--leak-check=full --track-origins=yes --read-var-info=yes --show-leak-kinds=all --error-exitcode=1")
19+
set(VALGRIND_LIST "")
20+
endif()
21+
22+
find_package(CURL REQUIRED)
23+
if(CURL_FOUND)
24+
include_directories(${CURL_INCLUDE_DIR})
25+
set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} )
26+
else(CURL_FOUND)
27+
message(FATAL_ERROR "Could not find the CURL library and development files.")
28+
endif()
29+
30+
foreach(ELEMENT ${UNIT_TESTS_C})
31+
get_filename_component(ELEMENT_NAME ${ELEMENT} NAME_WE)
32+
string(REGEX REPLACE "\\.c$" "" ELEMENT_REPLACED ${ELEMENT_NAME})
33+
set(EXE_NAME unit-${ELEMENT_REPLACED})
34+
add_executable(${EXE_NAME} ${ELEMENT} ${SRC_C} ${MODEL_C} ${API_C} ${EXTERNAL_SRC_C})
35+
target_link_libraries(${EXE_NAME} ${CURL_LIBRARIES})
36+
add_test(NAME ${EXE_NAME} COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME})
37+
38+
if(VALGRIND)
39+
set(memcheck_command "${CMAKE_MEMORYCHECK_COMMAND} ${CMAKE_MEMORYCHECK_COMMAND_OPTIONS}")
40+
separate_arguments(memcheck_command)
41+
42+
add_test(
43+
NAME valgrind-test-${ELEMENT_REPLACED}
44+
COMMAND ${memcheck_command} ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME}
45+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
46+
)
47+
endif()
48+
endforeach()
49+
50+
#For common coding standard (code beautifier/pretty printing)
51+
find_program(UNCRUSTIFY uncrustify)
52+
if(UNCRUSTIFY)
53+
add_custom_target(
54+
uncrustify
55+
)
56+
57+
foreach(ELEMENT ${ALL_SRC_LIST})
58+
string(REGEX REPLACE "/" "_" ELEMENT_NAME ${ELEMENT})
59+
set(DEP_NAME "uncrustify-${ELEMENT_NAME}")
60+
add_custom_target(
61+
${DEP_NAME}
62+
uncrustify -c uncrustify-rules.cfg --no-backup ${ELEMENT}
63+
DEPENDS ${ELEMENT}
64+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
65+
VERBATIM
66+
)
67+
add_dependencies(uncrustify ${DEP_NAME})
68+
endforeach()
69+
endif()
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include "apiClient.h"
4+
#include "cJSON.h"
5+
#include "pet.h"
6+
7+
#define MAX_BUFFER_LENGTH 9
8+
9+
pet_t *petApi_getPetById(apiClient_t *apiClient, long petId) {
10+
pet_t *pet;
11+
char *petIdString = malloc(MAX_BUFFER_LENGTH);
12+
13+
snprintf(petIdString, MAX_BUFFER_LENGTH, "%li", petId);
14+
15+
apiClient_invoke(apiClient,
16+
"pet",
17+
petIdString,
18+
NULL);
19+
pet = pet_parseFromJSON(apiClient->dataReceived);
20+
free(apiClient->dataReceived);
21+
if(pet == NULL) {
22+
return 0;
23+
} else {
24+
cJSON *jsonObject = pet_convertToJSON(pet);
25+
cJSON_Delete(jsonObject);
26+
}
27+
free(petIdString);
28+
29+
return pet;
30+
}
31+
32+
void *petApi_addPet(apiClient_t *apiClient, pet_t *pet) {
33+
cJSON *petJSONObject;
34+
char *petJSONString;
35+
36+
petJSONObject = pet_convertToJSON(pet);
37+
petJSONString = cJSON_Print(petJSONObject);
38+
apiClient_invoke(apiClient,
39+
"pet",
40+
NULL,
41+
petJSONString);
42+
free(apiClient->dataReceived);
43+
free(petJSONString);
44+
cJSON_Delete(petJSONObject);
45+
46+
return pet;
47+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

0 commit comments

Comments
 (0)