diff options
-rw-r--r-- | anteraja.c | 23 | ||||
-rw-r--r-- | handler.h | 45 | ||||
-rw-r--r-- | pikul.c | 5 |
3 files changed, 36 insertions, 37 deletions
@@ -1,15 +1,16 @@ #include "shipping.h" #include "handler.h" -enum { - BASE_PATH, - ACCESS_KEY -}; - extern CURL *curl; +static const char *status_trail[] = { + "status", + NULL +}; + void anteraja_init(char *provisions[], struct shipping *shipping) { + enum { BASE_PATH, ACCESS_KEY }; shipping->base = malloc(strlen(provisions[BASE_PATH]) + 1); strcpy(shipping->base, provisions[BASE_PATH]); headers(shipping, (const char *[]){ "access-key-id", "secret-access-key", NULL }, @@ -38,13 +39,15 @@ size_t anteraja_services_handle(const char *contents, size_t size, size_t nmemb, struct pikul_services **services) { size_t realsize = size * nmemb; - handle(contents, realsize, &(struct container){ services, (const char *[]){ + handle_services(contents, realsize, status_trail, (const char *[]){ + "content", + "services", + NULL + }, (const char *[]){ "product_code", "product_name", "etd", - "rates", - "content", - "services", - NULL}}); + "rates" + }, services); return realsize; } @@ -2,23 +2,11 @@ #include <json.h> #include "pikul.h" -enum { - CODE, - NAME, - ETD, - COST, - OBJECTS -}; - -struct container { - struct pikul_services **services; - const char **keys[7]; -}; - extern json_tokener *tokener; void recurse(struct json_object *, const char *[], struct json_object **); -inline void handle(const char *contents, size_t num_bytes, struct container *container) +inline void handle_services(const char *contents, size_t num_bytes, const char *status_trail[], + const char *services_trail[], const char *attributes[], struct pikul_services **services) { json_object *response = json_tokener_parse_ex(tokener, contents, num_bytes); enum json_tokener_error error = json_tokener_get_error(tokener); @@ -31,33 +19,38 @@ inline void handle(const char *contents, size_t num_bytes, struct container *con } } else if (!json_object_is_type(response, json_type_object) || error != json_tokener_success) return; - struct json_object *services = NULL; - recurse(response, &(*container->keys)[OBJECTS], &services); - size_t length = json_object_array_length(services); - *(container->services) = malloc(sizeof(struct pikul_services) + struct json_object *status = NULL; + recurse(response, status_trail, &status); + if (json_object_get_int(status) != 200) + return; + struct json_object *objects = NULL; + recurse(response, services_trail, &objects); + size_t length = json_object_array_length(objects); + *services = malloc(sizeof(struct pikul_services) + sizeof(struct pikul_service *[length])); - (*(container->services))->length = length; + (*services)->length = length; + enum { CODE, NAME, ETD, COST }; for (size_t i = 0; i < length; i++) { - (*(container->services))->list[i] = malloc(sizeof(struct pikul_service)); - struct pikul_service *service = (*(container->services))->list[i]; - json_object *object = json_object_array_get_idx(services, i); + (*services)->list[i] = malloc(sizeof(struct pikul_service)); + struct pikul_service *service = (*services)->list[i]; + json_object *object = json_object_array_get_idx(objects, 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, (*container->keys)[COST])) + if (!strcmp(key, attributes[COST])) service->cost = 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, (*container->keys)[CODE])) + if (!strcmp(key, attributes[CODE])) service->code = value; - else if (!strcmp(key, (*container->keys)[NAME])) + else if (!strcmp(key, attributes[NAME])) service->name = value; - else if (!strcmp(key, (*container->keys)[ETD])) + else if (!strcmp(key, attributes[ETD])) service->etd = value; } } @@ -6,7 +6,8 @@ json_tokener *tokener; static struct shipping shipping; extern inline void headers(struct shipping *shipping, const char *fields[], char *provisions[]); -extern inline void handle(const char *, size_t, struct container *); +extern inline void handle_services(const char *, size_t, const char *[], const char *[], const char *[], + struct pikul_services **); extern void anteraja_init(char *[], struct shipping *); extern void anteraja_services(const char *, const char *, double, @@ -92,6 +93,8 @@ static int servicecmp(const void *service1, const void *service2) double pikul_cost(const char *origin, const char *destination, double weight, const char *code) { struct pikul_services *services = pikul_services(origin, destination, weight); + if (!services) + return .0; qsort(services->list, services->length, sizeof(struct pikul_service *), servicecmp); struct pikul_service *key_service = malloc(sizeof(struct pikul_service)); memset(key_service, '\0', sizeof(struct pikul_service)); |