Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C][Client]Support data callback function #7467

Merged
merged 1 commit into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ apiClient_t *apiClient_create() {
apiClient->sslConfig = NULL;
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
apiClient->data_callback_func = NULL;
apiClient->response_code = 0;
{{#hasAuthMethods}}
{{#authMethods}}
Expand Down Expand Up @@ -58,6 +59,7 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath

apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
apiClient->data_callback_func = NULL;
apiClient->response_code = 0;
{{#hasAuthMethods}}
{{#authMethods}}
Expand Down Expand Up @@ -91,6 +93,7 @@ void apiClient_free(apiClient_t *apiClient) {
if(apiClient->basePath) {
free(apiClient->basePath);
}
apiClient->data_callback_func = NULL;
{{#hasAuthMethods}}
{{#authMethods}}
{{#isBasic}}
Expand Down Expand Up @@ -558,6 +561,10 @@ size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) {
apiClient->dataReceived = (char *)realloc( apiClient->dataReceived, apiClient->dataReceivedLen + size_this_time + 1);
memcpy(apiClient->dataReceived + apiClient->dataReceivedLen, buffer, size_this_time);
apiClient->dataReceivedLen += size_this_time;
((char*)apiClient->dataReceived)[apiClient->dataReceivedLen] = '\0'; // the space size of (apiClient->dataReceived) = dataReceivedLen + 1
if (apiClient->data_callback_func) {
apiClient->data_callback_func(&apiClient->dataReceived, &apiClient->dataReceivedLen);
}
return size_this_time;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ typedef struct apiClient_t {
sslConfig_t *sslConfig;
void *dataReceived;
long dataReceivedLen;
void (*data_callback_func)(void **, long *);
long response_code;
{{#hasAuthMethods}}
{{#authMethods}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,18 @@ char* findStrInStrList(list_t *strList, const char *str)

return NULL;
}

void clear_and_free_string_list(list_t *list)
{
if (!list) {
return;
}

listEntry_t *listEntry = NULL;
list_ForEach(listEntry, list) {
char *list_item = listEntry->data;
free(list_item);
list_item = NULL;
}
list_free(list);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ void listEntry_printAsInt(listEntry_t* listEntry, void *additionalData);
void listEntry_free(listEntry_t *listEntry, void *additionalData);

char* findStrInStrList(list_t* strList, const char* str);
void clear_and_free_string_list(list_t * list);
#endif // INCLUDE_LIST_H
1 change: 1 addition & 0 deletions samples/client/petstore/c/include/apiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ typedef struct apiClient_t {
sslConfig_t *sslConfig;
void *dataReceived;
long dataReceivedLen;
void (*data_callback_func)(void **, long *);
long response_code;
list_t *apiKeys_api_key;
char *accessToken;
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/c/include/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ void listEntry_printAsInt(listEntry_t* listEntry, void *additionalData);
void listEntry_free(listEntry_t *listEntry, void *additionalData);

char* findStrInStrList(list_t* strList, const char* str);
void clear_and_free_string_list(list_t * list);
#endif // INCLUDE_LIST_H
7 changes: 7 additions & 0 deletions samples/client/petstore/c/src/apiClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ apiClient_t *apiClient_create() {
apiClient->sslConfig = NULL;
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
apiClient->data_callback_func = NULL;
apiClient->response_code = 0;
apiClient->apiKeys_api_key = NULL;
apiClient->accessToken = NULL;
Expand Down Expand Up @@ -40,6 +41,7 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath

apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
apiClient->data_callback_func = NULL;
apiClient->response_code = 0;
if(apiKeys_api_key!= NULL) {
apiClient->apiKeys_api_key = list_create();
Expand All @@ -61,6 +63,7 @@ void apiClient_free(apiClient_t *apiClient) {
if(apiClient->basePath) {
free(apiClient->basePath);
}
apiClient->data_callback_func = NULL;
if(apiClient->apiKeys_api_key) {
listEntry_t *listEntry = NULL;
list_ForEach(listEntry, apiClient->apiKeys_api_key) {
Expand Down Expand Up @@ -464,6 +467,10 @@ size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) {
apiClient->dataReceived = (char *)realloc( apiClient->dataReceived, apiClient->dataReceivedLen + size_this_time + 1);
memcpy(apiClient->dataReceived + apiClient->dataReceivedLen, buffer, size_this_time);
apiClient->dataReceivedLen += size_this_time;
((char*)apiClient->dataReceived)[apiClient->dataReceivedLen] = '\0'; // the space size of (apiClient->dataReceived) = dataReceivedLen + 1
if (apiClient->data_callback_func) {
apiClient->data_callback_func(&apiClient->dataReceived, &apiClient->dataReceivedLen);
}
return size_this_time;
}

Expand Down
15 changes: 15 additions & 0 deletions samples/client/petstore/c/src/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,18 @@ char* findStrInStrList(list_t *strList, const char *str)

return NULL;
}

void clear_and_free_string_list(list_t *list)
{
if (!list) {
return;
}

listEntry_t *listEntry = NULL;
list_ForEach(listEntry, list) {
char *list_item = listEntry->data;
free(list_item);
list_item = NULL;
}
list_free(list);
}