Hemant Zope 3be4902444 [C] C generator refactored (#2463)
* New modified model header and body mustache for c client generator

* remove uncrustify from cmake as it is used during code generation, also remove valgrind as it is not used

* add function to encode and decode binary data

* update model mustache

* update api body and header mustache for handling all types of parameters

* update model mustache with variable names and address few more issues to generate working codes

* updated api body and header mustaches with support for various new parameters and fix some issues as per new changes in code flow structure

* update apiClient header and body mustache as per new modifications for handling binary data and few more stuff

* updated samples generated by new modified mustache

* update handling of file and binary data type to binary_t

* update samples with recent commit on master regarding c-generator

* update cmakelist which was ignored by .openapi-generator-ignore, cleanup external folder

* update CMakeList mustache to show how to use compiled libary to compile source files

* update samples with new cmake

* Add comments explaining what each command is doing inshort

* remove freeing of base path as it is not memory allocated

* update samples to free apiclient object when the requirement is over

* add missing cJSON delete to fix memory not freed bugs

* use uncrustify to beautify manual written test code
2019-04-15 11:27:34 +08:00

283 lines
7.5 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "StoreAPI.h"
#define MAX_BUFFER_LENGTH 4096
#define intToStr(dst, src) \
do { \
char dst[256]; \
snprintf(dst, 256, "%ld", (long int) (src)); \
} while(0)
// Delete purchase order by ID
//
// For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
//
void StoreAPI_deleteOrder(apiClient_t *apiClient, char *orderId) {
list_t *localVarQueryParameters = NULL;
list_t *localVarHeaderParameters = NULL;
list_t *localVarFormParameters = NULL;
list_t *localVarHeaderType = NULL;
list_t *localVarContentType = NULL;
char *localVarBodyParameters = NULL;
// create the path
long sizeOfPath = strlen("/store/order/{orderId}") + 1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/store/order/{orderId}");
// Path Params
long sizeOfPathParams_orderId = strlen(orderId) + 3 + strlen(
"{ orderId }");
if(orderId == NULL) {
goto end;
}
char *localVarToReplace_orderId = malloc(sizeOfPathParams_orderId);
sprintf(localVarToReplace_orderId, "{%s}", "orderId");
localVarPath = strReplace(localVarPath, localVarToReplace_orderId,
orderId);
apiClient_invoke(apiClient,
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarHeaderType,
localVarContentType,
localVarBodyParameters,
"DELETE");
if(apiClient->response_code == 400) {
printf("%s\n", "Invalid ID supplied");
}
if(apiClient->response_code == 404) {
printf("%s\n", "Order not found");
}
// No return type
end:
if(apiClient->dataReceived) {
free(apiClient->dataReceived);
}
free(localVarPath);
free(localVarToReplace_orderId);
}
// Returns pet inventories by status
//
// Returns a map of status codes to quantities
//
list_t *StoreAPI_getInventory(apiClient_t *apiClient) {
list_t *localVarQueryParameters = NULL;
list_t *localVarHeaderParameters = NULL;
list_t *localVarFormParameters = NULL;
list_t *localVarHeaderType = list_create();
list_t *localVarContentType = NULL;
char *localVarBodyParameters = NULL;
// create the path
long sizeOfPath = strlen("/store/inventory") + 1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/store/inventory");
list_addElement(localVarHeaderType, "application/json"); // produces
apiClient_invoke(apiClient,
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarHeaderType,
localVarContentType,
localVarBodyParameters,
"GET");
if(apiClient->response_code == 200) {
printf("%s\n", "successful operation");
}
// primitive reutrn type not simple
cJSON *localVarJSON = cJSON_Parse(apiClient->dataReceived);
cJSON *VarJSON;
list_t *elementToReturn = list_create();
cJSON_ArrayForEach(VarJSON, localVarJSON) {
keyValuePair_t *keyPair =
keyValuePair_create(strdup(VarJSON->string), cJSON_Print(
VarJSON));
list_addElement(elementToReturn, keyPair);
}
cJSON_Delete(localVarJSON);
if(apiClient->dataReceived) {
free(apiClient->dataReceived);
}
list_free(localVarHeaderType);
free(localVarPath);
return elementToReturn;
end:
return NULL;
}
// Find purchase order by ID
//
// For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
//
order_t *StoreAPI_getOrderById(apiClient_t *apiClient, long orderId) {
list_t *localVarQueryParameters = NULL;
list_t *localVarHeaderParameters = NULL;
list_t *localVarFormParameters = NULL;
list_t *localVarHeaderType = list_create();
list_t *localVarContentType = NULL;
char *localVarBodyParameters = NULL;
// create the path
long sizeOfPath = strlen("/store/order/{orderId}") + 1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/store/order/{orderId}");
// Path Params
long sizeOfPathParams_orderId = sizeof(orderId) + 3 + strlen(
"{ orderId }");
if(orderId == 0) {
goto end;
}
char *localVarToReplace_orderId = malloc(sizeOfPathParams_orderId);
snprintf(localVarToReplace_orderId, sizeOfPathParams_orderId, "{%s}",
"orderId");
char localVarBuff_orderId[256];
intToStr(localVarBuff_orderId, orderId);
localVarPath = strReplace(localVarPath, localVarToReplace_orderId,
localVarBuff_orderId);
list_addElement(localVarHeaderType, "application/xml"); // produces
list_addElement(localVarHeaderType, "application/json"); // produces
apiClient_invoke(apiClient,
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarHeaderType,
localVarContentType,
localVarBodyParameters,
"GET");
if(apiClient->response_code == 200) {
printf("%s\n", "successful operation");
}
if(apiClient->response_code == 400) {
printf("%s\n", "Invalid ID supplied");
}
if(apiClient->response_code == 404) {
printf("%s\n", "Order not found");
}
// nonprimitive not container
cJSON *StoreAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
order_t *elementToReturn = order_parseFromJSON(StoreAPIlocalVarJSON);
cJSON_Delete(StoreAPIlocalVarJSON);
if(elementToReturn == NULL) {
// return 0;
}
// return type
if(apiClient->dataReceived) {
free(apiClient->dataReceived);
}
list_free(localVarHeaderType);
free(localVarPath);
free(localVarToReplace_orderId);
return elementToReturn;
end:
return NULL;
}
// Place an order for a pet
//
order_t *StoreAPI_placeOrder(apiClient_t *apiClient, order_t *body) {
list_t *localVarQueryParameters = NULL;
list_t *localVarHeaderParameters = NULL;
list_t *localVarFormParameters = NULL;
list_t *localVarHeaderType = list_create();
list_t *localVarContentType = NULL;
char *localVarBodyParameters = NULL;
// create the path
long sizeOfPath = strlen("/store/order") + 1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/store/order");
// Body Param
cJSON *localVarSingleItemJSON_body;
if(body != NULL) {
// string
localVarSingleItemJSON_body = order_convertToJSON(body);
localVarBodyParameters =
cJSON_Print(localVarSingleItemJSON_body);
}
list_addElement(localVarHeaderType, "application/xml"); // produces
list_addElement(localVarHeaderType, "application/json"); // produces
apiClient_invoke(apiClient,
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarHeaderType,
localVarContentType,
localVarBodyParameters,
"POST");
if(apiClient->response_code == 200) {
printf("%s\n", "successful operation");
}
if(apiClient->response_code == 400) {
printf("%s\n", "Invalid Order");
}
// nonprimitive not container
cJSON *StoreAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
order_t *elementToReturn = order_parseFromJSON(StoreAPIlocalVarJSON);
cJSON_Delete(StoreAPIlocalVarJSON);
if(elementToReturn == NULL) {
// return 0;
}
// return type
if(apiClient->dataReceived) {
free(apiClient->dataReceived);
}
list_free(localVarHeaderType);
free(localVarPath);
cJSON_Delete(localVarSingleItemJSON_body);
free(localVarBodyParameters);
return elementToReturn;
end:
return NULL;
}