diff options
Diffstat (limited to 'interchange.c')
-rw-r--r-- | interchange.c | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/interchange.c b/interchange.c new file mode 100644 index 0000000..66497b8 --- /dev/null +++ b/interchange.c @@ -0,0 +1,107 @@ +#include <stdlib.h> +#include <string.h> +#include "request.h" +#include "interchange.h" + +char *image_dir; +#ifdef __EMSCRIPTEN__ +emscripten_fetch_attr_t attr; +#else +char *sampleurl; +char *cainfo = NULL; +#endif + +extern void handle_results(interchange_response *); + +void interchange_init(const char *url, const char *dir, const char *certificate) +{ + image_dir = malloc(strlen(dir) + 1); + strcpy(image_dir, dir); +#ifdef __EMSCRIPTEN__ + emscripten_fetch_attr_init(&attr); + attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; +#else + size_t length = strlen(url); + size_t append = url[length - 1] != '/'; + sampleurl = malloc(length + append + 1); + strcpy(sampleurl, url); + if (append) + strcat(sampleurl, "/"); + curl_global_init(CURL_GLOBAL_SSL); + if (certificate) { + cainfo = malloc(strlen(certificate) + 1); + strcpy(cainfo, certificate); + } +#endif +} + +void interchange_catalog(const char *prod_group, void (*handler)(interchange_response *), void (*callback)(struct interchange_catalog *)) +{ + char nonspaced[strlen(prod_group) + 1]; + strcpy(nonspaced, prod_group); + char *space = NULL; + while ((space = strchr(nonspaced, ' '))) + *space = '-'; + request(handler ? handler : handle_results, (void (*)(void *))callback, NULL, "%s", nonspaced); +} + +void interchange_product(const char *sku, void (*handler)(interchange_response *), void (*callback)(struct interchange_product *)) +{ + request(handler, (void (*)(void *))callback, NULL, "%s", sku); +} + +void interchange_page(const char *path, void (*handler)(interchange_response *)) +{ + request(handler, NULL, NULL, "%s", path); +} + +void interchange_free_product(struct interchange_product *product) +{ + if (product->crosssell) + for (size_t i = 0; i < product->crosssell->length; i++) + free(product->crosssell->skus[i]); + if (product->author) + free(product->author); + if (product->prod_group) + free(product->prod_group); + if (product->image) + free(product->image); + if (product->thumb) + free(product->thumb); + if (product->comment) + free(product->comment); + if (product->description) + free(product->description); + free(product->sku); + free(product); +} + +void interchange_free_catalog(struct interchange_catalog *catalog) +{ + for (size_t i = 0; i < catalog->length; i++) + interchange_free_product(catalog->products[i]); + free(catalog); +} + +void interchange_free_response(interchange_response *response) +{ + if (response->userData) + free(response->userData); +#ifdef __EMSCRIPTEN__ + emscripten_fetch_close(response); +#else + free(response->data); + curl_easy_cleanup(response->curl); + free(response); +#endif +} + +void interchange_cleanup() +{ + free(image_dir); +#ifndef __EMSCRIPTEN__ + free(cainfo); + free(sampleurl); + curl_global_cleanup(); +#endif +} |