Niklas Werner 96e0814510 [C] Adding petstore sample written in C (#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
2018-07-05 12:28:43 +08:00

55 lines
1.2 KiB
C

#include <stdlib.h>
#include "apiClient.h"
#include "cJSON.h"
#include "pet.h"
#ifdef API_KEY
#include "list.h"
#include "apiKey.h"
#endif // API_KEY
#ifdef DEBUG
#include <stdio.h>
#endif // DEBUG
#define EXAMPLE_OPERATION_NAME "pet"
#define EXAMPLE_OPERATION_PARAMETER "3"
int main() {
apiClient_t *apiClient = apiClient_create();
#ifdef OAUTH2
apiClient->accessToken = "thisIsMyExampleAccessToken";
#endif // OAUTH2
#ifdef API_KEY
apiClient->apiKeys = list_create();
apiKey_t *apiKey = apiKey_create("X-API-Key", "abcdef12345");
list_addElement(apiClient->apiKeys, apiKey);
#endif // API_KEY
apiClient_invoke(apiClient,
EXAMPLE_OPERATION_NAME,
EXAMPLE_OPERATION_PARAMETER,
NULL);
pet_t *pet = pet_parseFromJSON(apiClient->dataReceived);
if(pet == NULL) {
free(apiClient);
return 0;
} else {
cJSON *petJSONObject = pet_convertToJSON(pet);
#ifdef DEBUG
char *jsonString = cJSON_Print(petJSONObject);
puts(jsonString);
free(jsonString);
#endif
cJSON_Delete(petJSONObject);
}
free(apiClient->dataReceived);
#ifdef API_KEY
free(apiKey);
list_free(apiClient->apiKeys);
#endif // API_KEY
apiClient_free(apiClient);
pet_free(pet);
}