From f9365e7226ac8f636245da75a5a9095a88f1f0e0 Mon Sep 17 00:00:00 2001 From: Hui Yu Date: Wed, 18 Nov 2020 14:04:45 +0800 Subject: [PATCH] [C][Client] Support progress function of libcurl --- .../main/resources/C-libcurl/apiClient.c.mustache | 14 ++++++++++++++ .../main/resources/C-libcurl/apiClient.h.mustache | 3 +++ samples/client/petstore/c/include/apiClient.h | 3 +++ samples/client/petstore/c/src/apiClient.c | 14 ++++++++++++++ 4 files changed, 34 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 76438a96c9fc..d4d387f8f059 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 @@ -13,6 +13,8 @@ apiClient_t *apiClient_create() { apiClient->dataReceived = NULL; apiClient->dataReceivedLen = 0; apiClient->data_callback_func = NULL; + apiClient->progress_func = NULL; + apiClient->progress_data = NULL; apiClient->response_code = 0; {{#hasAuthMethods}} {{#authMethods}} @@ -58,6 +60,8 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath apiClient->dataReceived = NULL; apiClient->dataReceivedLen = 0; apiClient->data_callback_func = NULL; + apiClient->progress_func = NULL; + apiClient->progress_data = NULL; apiClient->response_code = 0; {{#hasAuthMethods}} {{#authMethods}} @@ -92,6 +96,8 @@ void apiClient_free(apiClient_t *apiClient) { free(apiClient->basePath); } apiClient->data_callback_func = NULL; + apiClient->progress_func = NULL; + apiClient->progress_data = NULL; {{#hasAuthMethods}} {{#authMethods}} {{#isBasic}} @@ -434,6 +440,14 @@ void apiClient_invoke(apiClient_t *apiClient, } } + if (apiClient->progress_func != NULL) { + curl_easy_setopt(handle, CURLOPT_XFERINFOFUNCTION, apiClient->progress_func); + if (apiClient->progress_data != NULL) { + curl_easy_setopt(handle, CURLOPT_XFERINFODATA, apiClient->progress_data); + } + curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L); + } + {{#hasAuthMethods}} {{#authMethods}} {{#isApiKey}} 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 ac17a3253b97..f8487e40e85e 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 @@ -6,6 +6,7 @@ #include #include #include +#include #include "../include/list.h" #include "../include/keyValuePair.h" #include "../include/binary.h" @@ -24,6 +25,8 @@ typedef struct apiClient_t { void *dataReceived; long dataReceivedLen; void (*data_callback_func)(void **, long *); + int (*progress_func)(void *, curl_off_t, curl_off_t, curl_off_t, curl_off_t); + void *progress_data; long response_code; {{#hasAuthMethods}} {{#authMethods}} diff --git a/samples/client/petstore/c/include/apiClient.h b/samples/client/petstore/c/include/apiClient.h index 595e912e092d..9f99d5f55ebd 100644 --- a/samples/client/petstore/c/include/apiClient.h +++ b/samples/client/petstore/c/include/apiClient.h @@ -6,6 +6,7 @@ #include #include #include +#include #include "../include/list.h" #include "../include/keyValuePair.h" #include "../include/binary.h" @@ -24,6 +25,8 @@ typedef struct apiClient_t { void *dataReceived; long dataReceivedLen; void (*data_callback_func)(void **, long *); + int (*progress_func)(void *, curl_off_t, curl_off_t, curl_off_t, curl_off_t); + void *progress_data; long response_code; list_t *apiKeys_api_key; char *accessToken; diff --git a/samples/client/petstore/c/src/apiClient.c b/samples/client/petstore/c/src/apiClient.c index f757bcf4d7db..2ab296b2e637 100644 --- a/samples/client/petstore/c/src/apiClient.c +++ b/samples/client/petstore/c/src/apiClient.c @@ -13,6 +13,8 @@ apiClient_t *apiClient_create() { apiClient->dataReceived = NULL; apiClient->dataReceivedLen = 0; apiClient->data_callback_func = NULL; + apiClient->progress_func = NULL; + apiClient->progress_data = NULL; apiClient->response_code = 0; apiClient->apiKeys_api_key = NULL; apiClient->accessToken = NULL; @@ -40,6 +42,8 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath apiClient->dataReceived = NULL; apiClient->dataReceivedLen = 0; apiClient->data_callback_func = NULL; + apiClient->progress_func = NULL; + apiClient->progress_data = NULL; apiClient->response_code = 0; if(apiKeys_api_key!= NULL) { apiClient->apiKeys_api_key = list_create(); @@ -62,6 +66,8 @@ void apiClient_free(apiClient_t *apiClient) { free(apiClient->basePath); } apiClient->data_callback_func = NULL; + apiClient->progress_func = NULL; + apiClient->progress_data = NULL; if(apiClient->apiKeys_api_key) { listEntry_t *listEntry = NULL; list_ForEach(listEntry, apiClient->apiKeys_api_key) { @@ -388,6 +394,14 @@ void apiClient_invoke(apiClient_t *apiClient, } } + if (apiClient->progress_func != NULL) { + curl_easy_setopt(handle, CURLOPT_XFERINFOFUNCTION, apiClient->progress_func); + if (apiClient->progress_data != NULL) { + curl_easy_setopt(handle, CURLOPT_XFERINFODATA, apiClient->progress_data); + } + curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L); + } + // this would only be generated for apiKey authentication if (apiClient->apiKeys_api_key != NULL) {