Ernesto Fernández b7c7ed087f
Fix a few issues with the C generator (part 4) (#20289)
* [C] Deal with binary api parameters

With this change, the bodyParameters array can also be binary, so pass
its length around instead of relying on strlen().

* [C] Fix a few remaining enum issues

* [C] Install headers and include any_type.h header

* [C] Don't require C++ to compile C code

* [C] Test binary bodies and path enums in schemas

* Update samples

---------

Co-authored-by: Sam Bingner <sam@corellium.com>
2024-12-13 02:01:04 +08:00

232 lines
6.6 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "DefaultAPI.h"
#define MAX_NUMBER_LENGTH 16
#define MAX_BUFFER_LENGTH 4096
#define intToStr(dst, src) \
do {\
char dst[256];\
snprintf(dst, 256, "%ld", (long int)(src));\
}while(0)
// Returns private information.
//
// This endpoint requires global security settings.
//
object_t*
DefaultAPI_privateGet(apiClient_t *apiClient)
{
list_t *localVarQueryParameters = NULL;
list_t *localVarHeaderParameters = NULL;
list_t *localVarFormParameters = NULL;
list_t *localVarHeaderType = list_createList();
list_t *localVarContentType = NULL;
char *localVarBodyParameters = NULL;
size_t localVarBodyLength = 0;
// clear the error code from the previous api call
apiClient->response_code = 0;
// create the path
long sizeOfPath = strlen("/private")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/private");
list_addElement(localVarHeaderType,"application/json"); //produces
apiClient_invoke(apiClient,
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarHeaderType,
localVarContentType,
localVarBodyParameters,
localVarBodyLength,
"GET");
// uncomment below to debug the error response
//if (apiClient->response_code == 200) {
// printf("%s\n","A JSON object with private information");
//}
//nonprimitive not container
object_t *elementToReturn = NULL;
if(apiClient->response_code >= 200 && apiClient->response_code < 300) {
cJSON *DefaultAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
elementToReturn = object_parseFromJSON(DefaultAPIlocalVarJSON);
cJSON_Delete(DefaultAPIlocalVarJSON);
if(elementToReturn == NULL) {
// return 0;
}
}
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}
list_freeList(localVarHeaderType);
free(localVarPath);
return elementToReturn;
end:
free(localVarPath);
return NULL;
}
// Returns public information.
//
// This endpoint does not require authentication.
//
object_t*
DefaultAPI_publicGet(apiClient_t *apiClient)
{
list_t *localVarQueryParameters = NULL;
list_t *localVarHeaderParameters = NULL;
list_t *localVarFormParameters = NULL;
list_t *localVarHeaderType = list_createList();
list_t *localVarContentType = NULL;
char *localVarBodyParameters = NULL;
size_t localVarBodyLength = 0;
// clear the error code from the previous api call
apiClient->response_code = 0;
// create the path
long sizeOfPath = strlen("/public")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/public");
list_addElement(localVarHeaderType,"application/json"); //produces
apiClient_invoke(apiClient,
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarHeaderType,
localVarContentType,
localVarBodyParameters,
localVarBodyLength,
"GET");
// uncomment below to debug the error response
//if (apiClient->response_code == 200) {
// printf("%s\n","A JSON object with public information");
//}
//nonprimitive not container
object_t *elementToReturn = NULL;
if(apiClient->response_code >= 200 && apiClient->response_code < 300) {
cJSON *DefaultAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
elementToReturn = object_parseFromJSON(DefaultAPIlocalVarJSON);
cJSON_Delete(DefaultAPIlocalVarJSON);
if(elementToReturn == NULL) {
// return 0;
}
}
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}
list_freeList(localVarHeaderType);
free(localVarPath);
return elementToReturn;
end:
free(localVarPath);
return NULL;
}
// Returns a list of users.
//
// Optional extended description in CommonMark or HTML.
//
list_t*
DefaultAPI_usersGet(apiClient_t *apiClient)
{
list_t *localVarQueryParameters = NULL;
list_t *localVarHeaderParameters = NULL;
list_t *localVarFormParameters = NULL;
list_t *localVarHeaderType = list_createList();
list_t *localVarContentType = NULL;
char *localVarBodyParameters = NULL;
size_t localVarBodyLength = 0;
// clear the error code from the previous api call
apiClient->response_code = 0;
// create the path
long sizeOfPath = strlen("/users")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/users");
list_addElement(localVarHeaderType,"application/json"); //produces
apiClient_invoke(apiClient,
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarHeaderType,
localVarContentType,
localVarBodyParameters,
localVarBodyLength,
"GET");
// uncomment below to debug the error response
//if (apiClient->response_code == 200) {
// printf("%s\n","A JSON array of user names");
//}
//primitive return type not simple
list_t *elementToReturn = NULL;
if(apiClient->response_code >= 200 && apiClient->response_code < 300) {
cJSON *localVarJSON = cJSON_Parse(apiClient->dataReceived);
cJSON *VarJSON;
elementToReturn = list_createList();
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);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}
list_freeList(localVarHeaderType);
free(localVarPath);
return elementToReturn;
end:
free(localVarPath);
return NULL;
}