Skip to content

Commit 4dc8d2a

Browse files
authored
[C][Client] Support progress function of libcurl (#7974)
1 parent 2b6b774 commit 4dc8d2a

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ apiClient_t *apiClient_create() {
1313
apiClient->dataReceived = NULL;
1414
apiClient->dataReceivedLen = 0;
1515
apiClient->data_callback_func = NULL;
16+
apiClient->progress_func = NULL;
17+
apiClient->progress_data = NULL;
1618
apiClient->response_code = 0;
1719
{{#hasAuthMethods}}
1820
{{#authMethods}}
@@ -58,6 +60,8 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
5860
apiClient->dataReceived = NULL;
5961
apiClient->dataReceivedLen = 0;
6062
apiClient->data_callback_func = NULL;
63+
apiClient->progress_func = NULL;
64+
apiClient->progress_data = NULL;
6165
apiClient->response_code = 0;
6266
{{#hasAuthMethods}}
6367
{{#authMethods}}
@@ -92,6 +96,8 @@ void apiClient_free(apiClient_t *apiClient) {
9296
free(apiClient->basePath);
9397
}
9498
apiClient->data_callback_func = NULL;
99+
apiClient->progress_func = NULL;
100+
apiClient->progress_data = NULL;
95101
{{#hasAuthMethods}}
96102
{{#authMethods}}
97103
{{#isBasic}}
@@ -434,6 +440,14 @@ void apiClient_invoke(apiClient_t *apiClient,
434440
}
435441
}
436442

443+
if (apiClient->progress_func != NULL) {
444+
curl_easy_setopt(handle, CURLOPT_XFERINFOFUNCTION, apiClient->progress_func);
445+
if (apiClient->progress_data != NULL) {
446+
curl_easy_setopt(handle, CURLOPT_XFERINFODATA, apiClient->progress_data);
447+
}
448+
curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L);
449+
}
450+
437451
{{#hasAuthMethods}}
438452
{{#authMethods}}
439453
{{#isApiKey}}

modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <stdio.h>
77
#include <stdbool.h>
88
#include <stdint.h>
9+
#include <curl/curl.h>
910
#include "../include/list.h"
1011
#include "../include/keyValuePair.h"
1112
#include "../include/binary.h"
@@ -24,6 +25,8 @@ typedef struct apiClient_t {
2425
void *dataReceived;
2526
long dataReceivedLen;
2627
void (*data_callback_func)(void **, long *);
28+
int (*progress_func)(void *, curl_off_t, curl_off_t, curl_off_t, curl_off_t);
29+
void *progress_data;
2730
long response_code;
2831
{{#hasAuthMethods}}
2932
{{#authMethods}}

samples/client/petstore/c/include/apiClient.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <stdio.h>
77
#include <stdbool.h>
88
#include <stdint.h>
9+
#include <curl/curl.h>
910
#include "../include/list.h"
1011
#include "../include/keyValuePair.h"
1112
#include "../include/binary.h"
@@ -24,6 +25,8 @@ typedef struct apiClient_t {
2425
void *dataReceived;
2526
long dataReceivedLen;
2627
void (*data_callback_func)(void **, long *);
28+
int (*progress_func)(void *, curl_off_t, curl_off_t, curl_off_t, curl_off_t);
29+
void *progress_data;
2730
long response_code;
2831
list_t *apiKeys_api_key;
2932
char *accessToken;

samples/client/petstore/c/src/apiClient.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ apiClient_t *apiClient_create() {
1313
apiClient->dataReceived = NULL;
1414
apiClient->dataReceivedLen = 0;
1515
apiClient->data_callback_func = NULL;
16+
apiClient->progress_func = NULL;
17+
apiClient->progress_data = NULL;
1618
apiClient->response_code = 0;
1719
apiClient->apiKeys_api_key = NULL;
1820
apiClient->accessToken = NULL;
@@ -40,6 +42,8 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
4042
apiClient->dataReceived = NULL;
4143
apiClient->dataReceivedLen = 0;
4244
apiClient->data_callback_func = NULL;
45+
apiClient->progress_func = NULL;
46+
apiClient->progress_data = NULL;
4347
apiClient->response_code = 0;
4448
if(apiKeys_api_key!= NULL) {
4549
apiClient->apiKeys_api_key = list_create();
@@ -62,6 +66,8 @@ void apiClient_free(apiClient_t *apiClient) {
6266
free(apiClient->basePath);
6367
}
6468
apiClient->data_callback_func = NULL;
69+
apiClient->progress_func = NULL;
70+
apiClient->progress_data = NULL;
6571
if(apiClient->apiKeys_api_key) {
6672
listEntry_t *listEntry = NULL;
6773
list_ForEach(listEntry, apiClient->apiKeys_api_key) {
@@ -388,6 +394,14 @@ void apiClient_invoke(apiClient_t *apiClient,
388394
}
389395
}
390396

397+
if (apiClient->progress_func != NULL) {
398+
curl_easy_setopt(handle, CURLOPT_XFERINFOFUNCTION, apiClient->progress_func);
399+
if (apiClient->progress_data != NULL) {
400+
curl_easy_setopt(handle, CURLOPT_XFERINFODATA, apiClient->progress_data);
401+
}
402+
curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L);
403+
}
404+
391405
// this would only be generated for apiKey authentication
392406
if (apiClient->apiKeys_api_key != NULL)
393407
{

0 commit comments

Comments
 (0)