From e823dae385b9d7300fa58e1b44a70d86bcc6fcd9 Mon Sep 17 00:00:00 2001 From: Hemant Zope Date: Tue, 20 Nov 2018 16:56:40 +0100 Subject: [PATCH 1/7] added auth support in apiClient --- .../resources/C-libcurl/apiClient.c.mustache | 61 ++++++++++++------- .../resources/C-libcurl/apiClient.h.mustache | 19 +++--- 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache index 1d591214c45f..c186ec387922 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache @@ -12,13 +12,18 @@ apiClient_t *apiClient_create() { apiClient->basePath = "{{{basePath}}}"; apiClient->dataReceived = NULL; apiClient->response_code = 0; - #ifdef BASIC_AUTH + {{#hasAuthMethods}} + {{#authMethods}} + {{#isBasic}} apiClient->username = NULL; apiClient->password = NULL; - #endif // BASIC_AUTH - #ifdef OAUTH2 + {{/isBasic}} + {{#isOAuth}} apiClient->accessToken = NULL; - #endif // OAUTH2 + {{/isOAuth}} + {{/authMethods}} + {{/hasAuthMethods}} + return apiClient; } @@ -287,8 +292,12 @@ void apiClient_invoke(apiClient_t *apiClient, free(headerValueToWrite); } } + {{#hasAuthMethods}} + {{#authMethods}} + {{#isApiKey}} // this would only be generated for apiKey authentication - #ifdef API_KEY + if (apiClient->apiKeys != NULL) + { list_ForEach(listEntry, apiClient->apiKeys) { keyValuePair_t *apiKey = listEntry->data; if((apiKey->key != NULL) && @@ -300,7 +309,10 @@ void apiClient_invoke(apiClient_t *apiClient, free(headerValueToWrite); } } - #endif // API_KEY + } + {{/isApiKey}} + {{/authMethods}} + {{/hasAuthMethods}} char *targetUrl = assembleTargetUrl(apiClient->basePath, @@ -316,19 +328,11 @@ void apiClient_invoke(apiClient_t *apiClient, &apiClient->dataReceived); curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(handle, CURLOPT_VERBOSE, 0); // to get curl debug msg 0: to disable, 1L:to enable - // this would only be generated for OAuth2 authentication - #ifdef OAUTH2 - if(apiClient->accessToken != NULL) { - // curl_easy_setopt(handle, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); - curl_easy_setopt(handle, - CURLOPT_XOAUTH2_BEARER, - apiClient->accessToken); - } - #endif - + {{#hasAuthMethods}} + {{#authMethods}} + {{#isBasic}} // this would only be generated for basic authentication: - #ifdef BASIC_AUTH char *authenticationToken; if((apiClient->username != NULL) && @@ -351,8 +355,18 @@ void apiClient_invoke(apiClient_t *apiClient, CURLOPT_USERPWD, authenticationToken); } - - #endif // BASIC_AUTH + {{/isBasic}} + {{#isOAuth}} + // this would only be generated for OAuth2 authentication + if(apiClient->accessToken != NULL) { + // curl_easy_setopt(handle, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); + curl_easy_setopt(handle, + CURLOPT_XOAUTH2_BEARER, + apiClient->accessToken); + } + {{/isOAuth}} + {{/authMethods}} + {{/hasAuthMethods}} if(bodyParameters != NULL) { postData(handle, bodyParameters); @@ -374,13 +388,18 @@ void apiClient_invoke(apiClient_t *apiClient, fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } - #ifdef BASIC_AUTH + {{#hasAuthMethods}} + {{#authMethods}} + {{#isBasic}} if((apiClient->username != NULL) && (apiClient->password != NULL) ) { free(authenticationToken); } - #endif // BASIC_AUTH + {{/isBasic}} + {{/authMethods}} + {{/hasAuthMethods}} + curl_easy_cleanup(handle); if(formParameters != NULL) { free(formString); diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache index 721c80dd072d..1e24ff29a1c8 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache @@ -12,19 +12,20 @@ typedef struct apiClient_t { char *basePath; void *dataReceived; long response_code; - // this would only be generated for basic authentication - #ifdef BASIC_AUTH + {{#hasAuthMethods}} + {{#authMethods}} + {{#isBasic}} char *username; char *password; - #endif // BASIC_AUTH - // this would only be generated for OAUTH2 authentication - #ifdef OAUTH2 + {{/isBasic}} + {{#isOAuth}} char *accessToken; - #endif // OAUTH2 - #ifdef API_KEY - //this would only be generated for apiKey authentication + {{/isOAuth}} + {{#isApiKey}} list_t *apiKeys; - #endif // API_KEY + {{/isApiKey}} + {{/authMethods}} + {{/hasAuthMethods}} } apiClient_t; typedef struct FileStruct From c22bdf7c7eeeb56a395bc4155a28161417f1062a Mon Sep 17 00:00:00 2001 From: Hemant Zope Date: Tue, 20 Nov 2018 17:14:50 +0100 Subject: [PATCH 2/7] added httperror response in detail --- .../src/main/resources/C-libcurl/apiClient.c.mustache | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache index c186ec387922..9c38adda6c69 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache @@ -385,7 +385,13 @@ void apiClient_invoke(apiClient_t *apiClient, if(res == CURLE_OK) { curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &apiClient->response_code); } else { - fprintf(stderr, "curl_easy_perform() failed: %s\n", + char *url,*ip,*scheme; + long port; + curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, &url); + curl_easy_getinfo(handle, CURLINFO_PRIMARY_IP, &ip); + curl_easy_getinfo(handle, CURLINFO_PRIMARY_PORT, &port); + curl_easy_getinfo(handle, CURLINFO_SCHEME, &scheme); + fprintf(stderr, "curl_easy_perform() failed\n\nURL: %s\nIP: %s\nPORT: %li\nSCHEME: %s\nStrERROR: %s\n",url,ip,port,scheme, curl_easy_strerror(res)); } {{#hasAuthMethods}} From 53375c3f9811141070bee2defbb440650cbb8b28 Mon Sep 17 00:00:00 2001 From: Hemant Zope Date: Tue, 20 Nov 2018 17:15:25 +0100 Subject: [PATCH 3/7] added apikey to apiClient create --- .../src/main/resources/C-libcurl/apiClient.c.mustache | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache index 9c38adda6c69..2969849b6f0b 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache @@ -21,6 +21,9 @@ apiClient_t *apiClient_create() { {{#isOAuth}} apiClient->accessToken = NULL; {{/isOAuth}} + {{#isApiKey}} + apiClient->apiKeys = NULL; + {{/isApiKey}} {{/authMethods}} {{/hasAuthMethods}} From ecddf68052e811383604a1f8ef4c7ef6f89c8a5d Mon Sep 17 00:00:00 2001 From: Hemant Zope Date: Wed, 21 Nov 2018 11:26:45 +0100 Subject: [PATCH 4/7] remove unnecessary print statements --- .../src/main/resources/C-libcurl/apiClient.c.mustache | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache index 2969849b6f0b..e53b0ce93c1e 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache @@ -262,11 +262,9 @@ void apiClient_invoke(apiClient_t *apiClient, if(strcmp(keyValuePair->key, "file") == 0) { - printf("Size of fileVar - %p\n",fileVar); memcpy(&fileVar, keyValuePair->value, sizeof(fileVar)); - printf("Size of fileVar1 - %p\n",fileVar); curl_mime_data(part, fileVar->fileData, fileVar->fileSize); From 45db13090bcb02325787b2382db95a5a9d31ccb9 Mon Sep 17 00:00:00 2001 From: Hemant Zope Date: Wed, 21 Nov 2018 11:42:28 +0100 Subject: [PATCH 5/7] remove freeing of apiclient object from apiclient_free function --- .../src/main/resources/C-libcurl/apiClient.c.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache index e53b0ce93c1e..c12979d18dfa 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache @@ -34,7 +34,7 @@ void apiClient_free(apiClient_t *apiClient) { if(apiClient->dataReceived) { free(apiClient->dataReceived); } - free(apiClient); + //free(apiClient); curl_global_cleanup(); } From 651cbc597ab26eba1b2159ebd09495b2a9469898 Mon Sep 17 00:00:00 2001 From: Hemant Zope Date: Mon, 10 Dec 2018 13:54:04 +0100 Subject: [PATCH 6/7] added auth params to apiClient_free function --- .../resources/C-libcurl/apiClient.c.mustache | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache index c12979d18dfa..e9714d0c0b4e 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache @@ -31,10 +31,30 @@ apiClient_t *apiClient_create() { } void apiClient_free(apiClient_t *apiClient) { - if(apiClient->dataReceived) { - free(apiClient->dataReceived); + if(apiClient->basePath) { + free(apiClient->basePath); } - //free(apiClient); + {{#hasAuthMethods}} + {{#authMethods}} + {{#isBasic}} + if(apiClient->username) { + free(apiClient->username); + } + if(apiClient->password) { + free(apiClient->password); + } + {{/isBasic}} + {{#isOAuth}} + if(apiClient->accessToken) { + free(apiClient->accessToken); + } + {{/isOAuth}} + {{#isApiKey}} + list_free(apiClient->apiKeys); + {{/isApiKey}} + {{/authMethods}} + {{/hasAuthMethods}} + free(apiClient); curl_global_cleanup(); } From eb6240de35c34961a7ed06cc873b9d2fddfe3841 Mon Sep 17 00:00:00 2001 From: Hemant Zope Date: Mon, 10 Dec 2018 13:54:59 +0100 Subject: [PATCH 7/7] free only received data and apiClient object to be freed by user --- .../src/main/resources/C-libcurl/api-body.mustache | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache index 77d26a0280bf..934a0f3f7f16 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache @@ -310,7 +310,9 @@ {{/returnContainer}} //return type {{/returnTypeIsPrimitive}} - apiClient_free(apiClient); + if (apiClient->dataReceived) { + free(apiClient->dataReceived); + } {{#hasQueryParams}}list_free(localVarQueryParameters);{{/hasQueryParams}} {{#hasHeaderParams}}list_free(localVarHeaderParameters);{{/hasHeaderParams}} {{#hasFormParams}}list_free(localVarFormParameters);{{/hasFormParams}} @@ -380,7 +382,10 @@ end: {{/returnType}} {{^returnType}} //No return type -end: apiClient_free(apiClient); +end: + if (apiClient->dataReceived) { + free(apiClient->dataReceived); + } {{#hasQueryParams}}list_free(localVarQueryParameters);{{/hasQueryParams}} {{#hasHeaderParams}}list_free(localVarHeaderParameters);{{/hasHeaderParams}} {{#hasFormParams}}list_free(localVarFormParameters);{{/hasFormParams}}