William Cheng edc05df774
Add C client generator (#516)
* add c generator (1st commit)

* udpate c model template

* more fixes

* Add string replace function for C generator (#908)

* Add string replace function for C generator

* Fixed replacement for variable only

* Fixed problem for different datatypes of paramName

* store return value of modified path

* set str_replace variable to be same as original variable.

* [C] Fixed coding style issues

* add uncrustify support

* update petstore sampmles

* add Locale.ROOT

* added test-api-client.c to include test cases for strReplace function

* added header and body mustache and made changes to ClibcurlClientCodegen.java accordingly

* [C] renamed functions in apiKey.c.mustache according to apiKey.h.mustache

* [C] changes in import statement

* renamed apiKey.h to keyValuePair.h and made necessary changes in the codes

* changed apiKey.c according to keyValuePair.h

* fixed import statement in model

* added code for generating struct in model-header.mustache

* added typedef struct for model-headers

* updated sample/client/petstore/C

* fix locale

* [C] Function addition and modification of major structs (#1020)

* added readme file

* fixed parameters in api headers functions

* made changes to add readme file and typemapping of array to list

* fixed header import statement in apiheader files

* modified struct of model type in model header

* updated sample

* modified README file

* updated sample

* parse from json function added

* modified struct and create function

* added include for model

* modified parsefromjson function

* modified struct and create function for more datatypes

* added mapping for date-time and modified model import return statement

* modified function parameters

* modified include statement

* fix function in api body

* updated sample

* clean up files

* regenerate c petstore

* fix error message when setting uncrustify

* add version to uncrustify usage

* added uncrustify rules in  mustache (#1021)

* added uncrustify rules in  mustache

* updated sample

* updated same with crustify

* updated sample with uncrusitfy 0.67

* modified readme about uncrustify requirements

* fixed mistakes in readme mustache and sample readme

* fix file import, unformat c petstore

* fix import in test files

* fix model with complexType

* fix free string, format the code using uncrustify

* modified sample

* Modified sample to check

* return type issue figured,more to do to fix it

* minor fixes to make complete code compile

* Compiling sample code. Store has issue with map.

* comment out test file generation which is not req.

* commented operation name

* fixed various issues responsible for code not compiling

* test mustache temporary for testing

* updated sample add,del,getbyid works, formparam yet to do

* few minor changes and added fuction to add different header and content type in apiClient

* added code to upload image

* added function to test upload image

* fixes for fileupload and various other small things

* fixed issue due to xml produces

* updated sample:working sample add,del,find,uploading:tocheck ,

* added free functions for variable where memory is allocated

* rename imagecontainer struct to filestruct

* fix issues with if functions for all list types

* fixed issue with primitive return type in header file

* updated sample w/ free functions

* update c samples

* remove corrupted file

* update samples

* test cases for APIs

* added function to generate test cases from new mustache

* mustache files for manual written test cases

* added default content type to application/json

* fixed issue with primitive return type

* fixed issue with bool type

* added file apiKey.c

* updated sample tested

* update c environment variable (#1090)

* add mapping for map (#1103)

* minor update

* revert list paramter check to NULL

* modified return type for primitive(map - list_t)in mustache

* removed apiClient_free as it was two times

* updated sample

* fixed issue of path parameter when string less than parameter len

* fixed issue for form paramters upload

* added checks to avoid seg faults

* updated sample

* added check for null value in form parameter

* modified size of mallocs to dynamic

* updated sample

* Add C Petstore to Travis CI (#1136)

* setup CI for C petstore

* update bash script permission

* unit petapi test

* fixed memory leak in strReplace and apiClient Functions

* modified return value for status generation

* added enum defination and functions to convert and back from string

* added function for enum and made changes to free memory at necessary places

* added datatype handling for enum

* fixes regarding memory allocation and free

* updated mustache of test files

* updated sample

* renamed manually written test files

* manual test file for pet

* cleaned common api test file for time being

* renamed test files

* added renamed test files to build test bash

* added file null pointer check

* modified uncrusitfy rules

* minor update to c templates (#1161)

* [C] Fixed enum function declaration  (#1178)

* fixed enum function declaration in model headers

* fixed enum declaration in header files for sample

* disable curl verbose mode and add response code variable

* added response code variable in apiClient struct

* modified apiClient header and source file

* added response and removed commented code api-body mustache

* removed commented code from model-body mustache

* removed unnecessary print statements from test mustache

* updated sample

* fixed spaces issue

* Better format in C templates (#1224)

* better format in the c template

* minor format fix

* [C] changed base url from static to dynamic (#1225)

* changed basePath from static to dynamic

* removed unnecessary header declaration

* updated sample

* [C] added curl version check in CMakeList.txt (#1248)

* added curl version check in CMakeList.txt

* Updated README for latest curl version

* [C] Major changes to keyValuePair function (#1282)

* removed static declaration

* changed static declaration

* added difference for string and non string

* added more code for different function for string and non string

* fix issue with param name

* change value in keyPairValue to void

* fixed issue of difference in function name cases

* added support for non char parameters in api

* fix issue of map return data

* modified manual-StoreAPI map return data handling

* fix minor mistake

* added support for map and changed code to support value of keyvaluepair as char and other

* updated sample

* fixed api header declarations

* change map declaration in header

* resolved issues realted to map data handling

* fix minor issues

* add N at start if enum variable starts with number

* override toParamName method

* changed paramters to paramName from baseName

* change variables in apibody from baseName to paramName

* Skip test file generation (#1459)

* skip test file generation

* skip overwriting CMakeLists.txt
2018-11-17 01:32:18 +08:00

540 lines
15 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "apiClient.h"
#include "cJSON.h"
#include "keyValuePair.h"
#include "list.h"
#include "user.h"
#define MAX_BUFFER_LENGTH 4096
#define intToStr(dst, src) \
do { \
char dst[256]; \
snprintf(dst, 256, "%ld", (long int) (src)); \
} while(0)
// Create user
//
// This can only be done by the logged in user.
//
void UserAPI_createUser(apiClient_t *apiClient, user_t *user) {
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("/user") + 1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/user");
// Body Param
cJSON *localVarSingleItemJSON_user;
if(user != NULL) {
// string
localVarSingleItemJSON_user = user_convertToJSON(user);
localVarBodyParameters =
cJSON_Print(localVarSingleItemJSON_user);
}
apiClient_invoke(apiClient,
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarHeaderType,
localVarContentType,
localVarBodyParameters,
"POST");
if(apiClient->response_code == 0) {
printf("%s\n", "successful operation");
}
// No return type
end: apiClient_free(apiClient);
free(localVarPath);
cJSON_Delete(localVarSingleItemJSON_user);
free(localVarBodyParameters);
}
// Creates list of users with given input array
//
void UserAPI_createUsersWithArrayInput(apiClient_t *apiClient, list_t *user) {
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("/user/createWithArray") + 1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/user/createWithArray");
// Body Param
// notstring
cJSON *localVar_user;
cJSON *localVarItemJSON_user;
cJSON *localVarSingleItemJSON_user;
if(user != NULL) {
localVarItemJSON_user = cJSON_CreateObject();
localVarSingleItemJSON_user = cJSON_AddArrayToObject(
localVarItemJSON_user, "user");
if(localVarSingleItemJSON_user == NULL) {
// nonprimitive container
const char *error_ptr = cJSON_GetErrorPtr();
if(error_ptr != NULL) {
fprintf(stderr, "Error Before: %s\n",
error_ptr);
goto end;
}
}
listEntry_t *userBodyListEntry;
list_ForEach(userBodyListEntry, user) {
localVar_user = user_convertToJSON(
userBodyListEntry->data);
if(localVar_user == NULL) {
const char *error_ptr = cJSON_GetErrorPtr();
if(error_ptr != NULL) {
fprintf(stderr, "Error Before: %s\n",
error_ptr);
goto end;
}
}
cJSON_AddItemToArray(localVarSingleItemJSON_user,
localVar_user);
}
localVarBodyParameters = cJSON_Print(localVarItemJSON_user);
}
apiClient_invoke(apiClient,
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarHeaderType,
localVarContentType,
localVarBodyParameters,
"POST");
if(apiClient->response_code == 0) {
printf("%s\n", "successful operation");
}
// No return type
end: apiClient_free(apiClient);
free(localVarPath);
cJSON_Delete(localVarItemJSON_user);
cJSON_Delete(localVarSingleItemJSON_user);
cJSON_Delete(localVar_user);
free(localVarBodyParameters);
}
// Creates list of users with given input array
//
void UserAPI_createUsersWithListInput(apiClient_t *apiClient, list_t *user) {
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("/user/createWithList") + 1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/user/createWithList");
// Body Param
// notstring
cJSON *localVar_user;
cJSON *localVarItemJSON_user;
cJSON *localVarSingleItemJSON_user;
if(user != NULL) {
localVarItemJSON_user = cJSON_CreateObject();
localVarSingleItemJSON_user = cJSON_AddArrayToObject(
localVarItemJSON_user, "user");
if(localVarSingleItemJSON_user == NULL) {
// nonprimitive container
const char *error_ptr = cJSON_GetErrorPtr();
if(error_ptr != NULL) {
fprintf(stderr, "Error Before: %s\n",
error_ptr);
goto end;
}
}
listEntry_t *userBodyListEntry;
list_ForEach(userBodyListEntry, user) {
localVar_user = user_convertToJSON(
userBodyListEntry->data);
if(localVar_user == NULL) {
const char *error_ptr = cJSON_GetErrorPtr();
if(error_ptr != NULL) {
fprintf(stderr, "Error Before: %s\n",
error_ptr);
goto end;
}
}
cJSON_AddItemToArray(localVarSingleItemJSON_user,
localVar_user);
}
localVarBodyParameters = cJSON_Print(localVarItemJSON_user);
}
apiClient_invoke(apiClient,
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarHeaderType,
localVarContentType,
localVarBodyParameters,
"POST");
if(apiClient->response_code == 0) {
printf("%s\n", "successful operation");
}
// No return type
end: apiClient_free(apiClient);
free(localVarPath);
cJSON_Delete(localVarItemJSON_user);
cJSON_Delete(localVarSingleItemJSON_user);
cJSON_Delete(localVar_user);
free(localVarBodyParameters);
}
// Delete user
//
// This can only be done by the logged in user.
//
void UserAPI_deleteUser(apiClient_t *apiClient, char *username) {
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("/user/{username}") + 1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/user/{username}");
// Path Params
long sizeOfPathParams_username = strlen(username) + 3 + strlen(
"{ username }");
if(username == NULL) {
goto end;
}
char *localVarToReplace_username = malloc(sizeOfPathParams_username);
sprintf(localVarToReplace_username, "%s%s%s", "{", "username", "}");
localVarPath = strReplace(localVarPath, localVarToReplace_username,
username);
apiClient_invoke(apiClient,
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarHeaderType,
localVarContentType,
localVarBodyParameters,
"DELETE");
if(apiClient->response_code == 400) {
printf("%s\n", "Invalid username supplied");
}
if(apiClient->response_code == 404) {
printf("%s\n", "User not found");
}
// No return type
end: apiClient_free(apiClient);
free(localVarPath);
free(localVarToReplace_username);
}
// Get user by user name
//
user_t *UserAPI_getUserByName(apiClient_t *apiClient, char *username) {
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("/user/{username}") + 1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/user/{username}");
// Path Params
long sizeOfPathParams_username = strlen(username) + 3 + strlen(
"{ username }");
if(username == NULL) {
goto end;
}
char *localVarToReplace_username = malloc(sizeOfPathParams_username);
sprintf(localVarToReplace_username, "%s%s%s", "{", "username", "}");
localVarPath = strReplace(localVarPath, localVarToReplace_username,
username);
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 username supplied");
}
if(apiClient->response_code == 404) {
printf("%s\n", "User not found");
}
// nonprimitive not container
user_t *elementToReturn = user_parseFromJSON(apiClient->dataReceived);
if(elementToReturn == NULL) {
// return 0;
}
// return type
apiClient_free(apiClient);
list_free(localVarHeaderType);
free(localVarPath);
free(localVarToReplace_username);
return elementToReturn;
end:
return NULL;
}
// Logs user into the system
//
char *UserAPI_loginUser(apiClient_t *apiClient, char *username,
char *password) {
list_t *localVarQueryParameters = list_create();
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("/user/login") + 1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/user/login");
// query parameters
char *keyQuery_username;
char *valueQuery_username;
keyValuePair_t *keyPairQuery_username = 0;
if(username) {
// string
keyQuery_username = strdup("username");
valueQuery_username = strdup(username);
keyPairQuery_username = keyValuePair_create(keyQuery_username,
valueQuery_username);
list_addElement(localVarQueryParameters, keyPairQuery_username);
}
// query parameters
char *keyQuery_password;
char *valueQuery_password;
keyValuePair_t *keyPairQuery_password = 0;
if(password) {
// string
keyQuery_password = strdup("password");
valueQuery_password = strdup(password);
keyPairQuery_password = keyValuePair_create(keyQuery_password,
valueQuery_password);
list_addElement(localVarQueryParameters, keyPairQuery_password);
}
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 username/password supplied");
}
// primitive reutrn type simple
char *elementToReturn = strdup((char *) apiClient->dataReceived);
apiClient_free(apiClient);
list_free(localVarQueryParameters);
list_free(localVarHeaderType);
free(localVarPath);
free(keyQuery_username);
free(valueQuery_username);
keyValuePair_free(keyPairQuery_username);
free(keyQuery_password);
free(valueQuery_password);
keyValuePair_free(keyPairQuery_password);
return elementToReturn;
end:
return NULL;
}
// Logs out current logged in user session
//
void UserAPI_logoutUser(apiClient_t *apiClient) {
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("/user/logout") + 1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/user/logout");
apiClient_invoke(apiClient,
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarHeaderType,
localVarContentType,
localVarBodyParameters,
"GET");
if(apiClient->response_code == 0) {
printf("%s\n", "successful operation");
}
// No return type
end: apiClient_free(apiClient);
free(localVarPath);
}
// Updated user
//
// This can only be done by the logged in user.
//
void UserAPI_updateUser(apiClient_t *apiClient, char *username, user_t *user) {
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("/user/{username}") + 1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/user/{username}");
// Path Params
long sizeOfPathParams_username = strlen(username) + 3 + strlen(
"{ username }");
if(username == NULL) {
goto end;
}
char *localVarToReplace_username = malloc(sizeOfPathParams_username);
sprintf(localVarToReplace_username, "%s%s%s", "{", "username", "}");
localVarPath = strReplace(localVarPath, localVarToReplace_username,
username);
// Body Param
cJSON *localVarSingleItemJSON_user;
if(user != NULL) {
// string
localVarSingleItemJSON_user = user_convertToJSON(user);
localVarBodyParameters =
cJSON_Print(localVarSingleItemJSON_user);
}
apiClient_invoke(apiClient,
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarHeaderType,
localVarContentType,
localVarBodyParameters,
"PUT");
if(apiClient->response_code == 400) {
printf("%s\n", "Invalid user supplied");
}
if(apiClient->response_code == 404) {
printf("%s\n", "User not found");
}
// No return type
end: apiClient_free(apiClient);
free(localVarPath);
free(localVarToReplace_username);
cJSON_Delete(localVarSingleItemJSON_user);
free(localVarBodyParameters);
}