diff options
author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-06-11 18:11:43 +0800 |
---|---|---|
committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-06-11 18:11:43 +0800 |
commit | fbaf3ef798fd4cf101c86b29bf12e3da2868c16c (patch) | |
tree | 7db602f01cddbb3377de9701c8da20f5aa6fb3b2 | |
parent | ed0824654a1d1049711646ab355b8c0a1dd20af7 (diff) |
Test custom handler parameter
-rw-r--r-- | Makefile.am | 8 | ||||
-rw-r--r-- | main.c | 75 |
2 files changed, 72 insertions, 11 deletions
diff --git a/Makefile.am b/Makefile.am index 2814636..b43a6f7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -35,8 +35,8 @@ pkginclude_HEADERS += \ endif #if !WASM -#bin_PROGRAMS = testicclient -#testicclient_SOURCES = main.c client.c catalog.c product.c request.c -#testicclient_CPPFLAGS = -I${prefix}/include $(DEPS_CFLAGS) -#testicclient_LDFLAGS = $(DEPS_LIBS) -lcurl +#bin_PROGRAMS = icclient +#icclient_SOURCES = main.c client.c catalog.c product.c request.c +#icclient_CPPFLAGS = -I${prefix}/include $(DEPS_CFLAGS) +#icclient_LDFLAGS = $(DEPS_LIBS) -lcurl #endif @@ -2,11 +2,14 @@ #include <stdlib.h> #include <string.h> #include <stdbool.h> +#include <json.h> #include "icclient/typedefs.h" #include "icclient/product.h" #include "icclient/catalog.h" #include "icclient/client.h" +static json_tokener *tokener; + static void callback(struct icclient_catalog *catalog) { for (size_t i = 0; i < catalog->length; i++) { @@ -27,8 +30,69 @@ static void callback(struct icclient_catalog *catalog) } } -int main(void) +static size_t handler_results(void *contents, size_t size, size_t nmemb, void *userData) +{ + size_t realsize = size * nmemb; + char data[realsize + 1]; + memcpy(data, contents, realsize); + data[realsize] = '\0'; + json_object *products = json_tokener_parse_ex(tokener, data, realsize); + enum json_tokener_error error = json_tokener_get_error(tokener); + if (!products) { + if (error == json_tokener_continue) + return realsize; + else { + json_tokener_reset(tokener); + return realsize; + } + } else if (!json_object_is_type(products, json_type_array) || error != json_tokener_success) + return realsize; + size_t length = json_object_array_length(products); + struct icclient_catalog_callback *catalog_callback = (struct icclient_catalog_callback *)userData; + struct icclient_catalog **catalogptr = catalog_callback->catalog; + *catalogptr = malloc(sizeof(struct icclient_catalog) + sizeof(struct icclient_product *[length])); + struct icclient_catalog *catalog = *catalogptr; + catalog->length = length; + for (size_t i = 0; i < length; i++) { + catalog->products[i] = malloc(sizeof(struct icclient_product)); + struct icclient_product *product = catalog->products[i]; + icclient_product_init(product); + json_object *object = json_object_array_get_idx(products, i); + struct json_object_iterator iterator = json_object_iter_begin(object); + struct json_object_iterator iterator_end = json_object_iter_end(object); + while (!json_object_iter_equal(&iterator, &iterator_end)) { + const char *key = json_object_iter_peek_name(&iterator); + json_object *val = json_object_iter_peek_value(&iterator); + if (!strcmp(key, "price")) + product->price = json_object_get_double(val); + else { + int len = json_object_get_string_len(val); + if (len) { + char *value = malloc(len + 1); + strcpy(value, json_object_get_string(val)); + if (!strcmp(key, "sku")) + product->sku = value; + else if (!strcmp(key, "thumb")) + product->thumb = value; + else if (!strcmp(key, "image")) + product->image = value; + else if (!strcmp(key, "description")) + product->description = value; + else if (!strcmp(key, "prod_group")) + product->prod_group = value; + } + } + json_object_iter_next(&iterator); + } + } + catalog_callback->callback(catalog); + free(catalog_callback); + return realsize; +} + +int main(int argc, char *argv[]) { + tokener = json_tokener_new(); /* char *url_line = NULL, *name_line = NULL, *pass_line = NULL; printf("URL: "); @@ -37,7 +101,6 @@ int main(void) ssize_t name_nread = getline(&name_line, &(size_t){0}, stdin); printf("Pass: "); ssize_t pass_nread = getline(&pass_line, &(size_t){0}, stdin); - char *url = malloc(--url_nread + 1), *name = malloc(--name_nread + 1), *pass = malloc(--pass_nread + 1); strncpy(url, url_line, url_nread); free(url_line); @@ -46,8 +109,7 @@ int main(void) strncpy(pass, pass_line, pass_nread); free(pass_line); */ - - icclient_init("http://localhost/namatoko", NULL); + icclient_init(argv[1], NULL); /* free(url); @@ -57,10 +119,9 @@ int main(void) icclient_logout(); */ - struct icclient_catalog *catalog; - icclient_allproducts(callback, &catalog, NULL); + icclient_allproducts(callback, &catalog, handler_results); icclient_catalog_free(catalog); - icclient_cleanup(); + json_tokener_free(tokener); } |