Skip to content

Commit f608803

Browse files
authored
[C][Client]Support data callback function (#7467)
1 parent 9ace82a commit f608803

File tree

8 files changed

+48
-0
lines changed

8 files changed

+48
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ apiClient_t *apiClient_create() {
1313
apiClient->sslConfig = NULL;
1414
apiClient->dataReceived = NULL;
1515
apiClient->dataReceivedLen = 0;
16+
apiClient->data_callback_func = NULL;
1617
apiClient->response_code = 0;
1718
{{#hasAuthMethods}}
1819
{{#authMethods}}
@@ -58,6 +59,7 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
5859

5960
apiClient->dataReceived = NULL;
6061
apiClient->dataReceivedLen = 0;
62+
apiClient->data_callback_func = NULL;
6163
apiClient->response_code = 0;
6264
{{#hasAuthMethods}}
6365
{{#authMethods}}
@@ -91,6 +93,7 @@ void apiClient_free(apiClient_t *apiClient) {
9193
if(apiClient->basePath) {
9294
free(apiClient->basePath);
9395
}
96+
apiClient->data_callback_func = NULL;
9497
{{#hasAuthMethods}}
9598
{{#authMethods}}
9699
{{#isBasic}}
@@ -558,6 +561,10 @@ size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) {
558561
apiClient->dataReceived = (char *)realloc( apiClient->dataReceived, apiClient->dataReceivedLen + size_this_time + 1);
559562
memcpy(apiClient->dataReceived + apiClient->dataReceivedLen, buffer, size_this_time);
560563
apiClient->dataReceivedLen += size_this_time;
564+
((char*)apiClient->dataReceived)[apiClient->dataReceivedLen] = '\0'; // the space size of (apiClient->dataReceived) = dataReceivedLen + 1
565+
if (apiClient->data_callback_func) {
566+
apiClient->data_callback_func(&apiClient->dataReceived, &apiClient->dataReceivedLen);
567+
}
561568
return size_this_time;
562569
}
563570

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ typedef struct apiClient_t {
2323
sslConfig_t *sslConfig;
2424
void *dataReceived;
2525
long dataReceivedLen;
26+
void (*data_callback_func)(void **, long *);
2627
long response_code;
2728
{{#hasAuthMethods}}
2829
{{#authMethods}}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,18 @@ char* findStrInStrList(list_t *strList, const char *str)
183183

184184
return NULL;
185185
}
186+
187+
void clear_and_free_string_list(list_t *list)
188+
{
189+
if (!list) {
190+
return;
191+
}
192+
193+
listEntry_t *listEntry = NULL;
194+
list_ForEach(listEntry, list) {
195+
char *list_item = listEntry->data;
196+
free(list_item);
197+
list_item = NULL;
198+
}
199+
list_free(list);
200+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ void listEntry_printAsInt(listEntry_t* listEntry, void *additionalData);
3838
void listEntry_free(listEntry_t *listEntry, void *additionalData);
3939

4040
char* findStrInStrList(list_t* strList, const char* str);
41+
void clear_and_free_string_list(list_t * list);
4142
#endif // INCLUDE_LIST_H

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ typedef struct apiClient_t {
2323
sslConfig_t *sslConfig;
2424
void *dataReceived;
2525
long dataReceivedLen;
26+
void (*data_callback_func)(void **, long *);
2627
long response_code;
2728
list_t *apiKeys_api_key;
2829
char *accessToken;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ void listEntry_printAsInt(listEntry_t* listEntry, void *additionalData);
3838
void listEntry_free(listEntry_t *listEntry, void *additionalData);
3939

4040
char* findStrInStrList(list_t* strList, const char* str);
41+
void clear_and_free_string_list(list_t * list);
4142
#endif // INCLUDE_LIST_H

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ apiClient_t *apiClient_create() {
1313
apiClient->sslConfig = NULL;
1414
apiClient->dataReceived = NULL;
1515
apiClient->dataReceivedLen = 0;
16+
apiClient->data_callback_func = NULL;
1617
apiClient->response_code = 0;
1718
apiClient->apiKeys_api_key = NULL;
1819
apiClient->accessToken = NULL;
@@ -40,6 +41,7 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
4041

4142
apiClient->dataReceived = NULL;
4243
apiClient->dataReceivedLen = 0;
44+
apiClient->data_callback_func = NULL;
4345
apiClient->response_code = 0;
4446
if(apiKeys_api_key!= NULL) {
4547
apiClient->apiKeys_api_key = list_create();
@@ -61,6 +63,7 @@ void apiClient_free(apiClient_t *apiClient) {
6163
if(apiClient->basePath) {
6264
free(apiClient->basePath);
6365
}
66+
apiClient->data_callback_func = NULL;
6467
if(apiClient->apiKeys_api_key) {
6568
listEntry_t *listEntry = NULL;
6669
list_ForEach(listEntry, apiClient->apiKeys_api_key) {
@@ -464,6 +467,10 @@ size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) {
464467
apiClient->dataReceived = (char *)realloc( apiClient->dataReceived, apiClient->dataReceivedLen + size_this_time + 1);
465468
memcpy(apiClient->dataReceived + apiClient->dataReceivedLen, buffer, size_this_time);
466469
apiClient->dataReceivedLen += size_this_time;
470+
((char*)apiClient->dataReceived)[apiClient->dataReceivedLen] = '\0'; // the space size of (apiClient->dataReceived) = dataReceivedLen + 1
471+
if (apiClient->data_callback_func) {
472+
apiClient->data_callback_func(&apiClient->dataReceived, &apiClient->dataReceivedLen);
473+
}
467474
return size_this_time;
468475
}
469476

samples/client/petstore/c/src/list.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,18 @@ char* findStrInStrList(list_t *strList, const char *str)
183183

184184
return NULL;
185185
}
186+
187+
void clear_and_free_string_list(list_t *list)
188+
{
189+
if (!list) {
190+
return;
191+
}
192+
193+
listEntry_t *listEntry = NULL;
194+
list_ForEach(listEntry, list) {
195+
char *list_item = listEntry->data;
196+
free(list_item);
197+
list_item = NULL;
198+
}
199+
list_free(list);
200+
}

0 commit comments

Comments
 (0)