forked from loafle/openapi-generator-original
* 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
111 lines
3.3 KiB
C
111 lines
3.3 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
#ifdef DEBUG
|
|
#include <stdio.h>
|
|
#endif // DEBUG
|
|
#include "apiClient.h"
|
|
#include "cJSON.h"
|
|
#include "pet.h"
|
|
#include "petApi.h"
|
|
#include "category.h"
|
|
#include "tag.h"
|
|
|
|
#ifdef DEBUG
|
|
#include <stdio.h>
|
|
#endif // DEBUG
|
|
|
|
#define EXAMPLE_CATEGORY_NAME "Example Category"
|
|
#define EXAMPLE_CATEGORY_ID 5
|
|
#define EXAMPLE_PET_NAME "Example Pet"
|
|
#define EXAMPLE_URL_1 "http://www.github.com"
|
|
#define EXAMPLE_URL_2 "http://www.gitter.im"
|
|
#define EXAMPLE_TAG_1_NAME "beautiful code"
|
|
#define EXAMPLE_TAG_2_NAME "at least I tried"
|
|
#define EXAMPLE_TAG_1_ID 1
|
|
#define EXAMPLE_TAG_2_ID 542353
|
|
#define EXAMPLE_PET_ID 1 // Set to 0 to generate a new pet
|
|
|
|
#define EXAMPLE_OPERATION_PARAMETER 4
|
|
|
|
/*
|
|
Creates one pet and adds it. Then gets the pet with the just added ID and compare if the values are equal.
|
|
Could fail if someone else makes changes to the added pet, before it can be fetched again.
|
|
*/
|
|
int main() {
|
|
apiClient_t *apiClient = apiClient_create();
|
|
|
|
char *categoryName = malloc(strlen(EXAMPLE_CATEGORY_NAME) + 1);
|
|
strcpy(categoryName, EXAMPLE_CATEGORY_NAME);
|
|
|
|
category_t *category =
|
|
category_create(EXAMPLE_CATEGORY_ID, categoryName);
|
|
|
|
char *petName = malloc(strlen(EXAMPLE_PET_NAME) + 1);
|
|
strcpy(petName, EXAMPLE_PET_NAME);
|
|
|
|
char *exampleUrl1 = malloc(strlen(EXAMPLE_URL_1) + 1);
|
|
strcpy(exampleUrl1, EXAMPLE_URL_1);
|
|
|
|
char *exampleUrl2 = malloc(strlen(EXAMPLE_URL_2) + 1);
|
|
strcpy(exampleUrl2, EXAMPLE_URL_2);
|
|
|
|
list_t *photoUrls = list_create();
|
|
|
|
list_addElement(photoUrls, exampleUrl1);
|
|
list_addElement(photoUrls, exampleUrl2);
|
|
|
|
char *exampleTag1Name = malloc(strlen(EXAMPLE_TAG_1_NAME) + 1);
|
|
strcpy(exampleTag1Name, EXAMPLE_TAG_1_NAME);
|
|
tag_t *exampleTag1 = tag_create(EXAMPLE_TAG_1_ID, exampleTag1Name);
|
|
|
|
char *exampleTag2Name = malloc(strlen(EXAMPLE_TAG_2_NAME) + 1);
|
|
strcpy(exampleTag2Name, EXAMPLE_TAG_2_NAME);
|
|
tag_t *exampleTag2 = tag_create(EXAMPLE_TAG_2_ID, exampleTag2Name);
|
|
|
|
list_t *tags = list_create();
|
|
|
|
list_addElement(tags, exampleTag1);
|
|
list_addElement(tags, exampleTag2);
|
|
|
|
status_t status = sold;
|
|
|
|
pet_t *pet =
|
|
pet_create(EXAMPLE_PET_ID,
|
|
category,
|
|
petName,
|
|
photoUrls,
|
|
tags,
|
|
status);
|
|
|
|
petApi_addPet(apiClient, pet);
|
|
|
|
pet_free(pet);
|
|
|
|
pet = petApi_getPetById(apiClient, 1);
|
|
|
|
assert(strcmp(pet->name, EXAMPLE_PET_NAME) == 0);
|
|
assert(pet->id == EXAMPLE_PET_ID);
|
|
assert(strcmp(pet->category->name, EXAMPLE_CATEGORY_NAME) == 0);
|
|
assert(pet->category->id == EXAMPLE_CATEGORY_ID);
|
|
assert(strcmp(list_getElementAt(pet->photoUrls,
|
|
0)->data, EXAMPLE_URL_1) == 0);
|
|
assert(strcmp(list_getElementAt(pet->photoUrls,
|
|
1)->data, EXAMPLE_URL_2) == 0);
|
|
assert(((tag_t *) list_getElementAt(pet->tags,
|
|
0)->data)->id == EXAMPLE_TAG_1_ID);
|
|
assert(((tag_t *) list_getElementAt(pet->tags,
|
|
1)->data)->id == EXAMPLE_TAG_2_ID);
|
|
assert(strcmp(((tag_t *) list_getElementAt(pet->tags, 0)->data)->name,
|
|
EXAMPLE_TAG_1_NAME) == 0);
|
|
assert(strcmp(((tag_t *) list_getElementAt(pet->tags, 1)->data)->name,
|
|
EXAMPLE_TAG_2_NAME) == 0);
|
|
|
|
#ifdef DEBUG
|
|
char *petJSON = pet_convertToJSON(pet);
|
|
puts(petJSON);
|
|
free(petJSON);
|
|
#endif // DEBUG
|
|
pet_free(pet);
|
|
apiClient_free(apiClient);
|
|
} |