summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-07-25 11:48:32 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-07-25 11:48:32 +0800
commit017f4c4312444c1ba386d6e0f6192fdc40f03c01 (patch)
tree3cf9ff0db673e3990363a3f154bf3df02d02fbc3
parent6e7d2c8ef7394e31f18b7193fb1957d3242c72aa (diff)
Origin & destination info retrieval implementations
-rw-r--r--pikul.c78
-rw-r--r--pikul.h9
-rw-r--r--private.h1
-rw-r--r--sicepat.c42
4 files changed, 130 insertions, 0 deletions
diff --git a/pikul.c b/pikul.c
index ddb02a7..b40ee17 100644
--- a/pikul.c
+++ b/pikul.c
@@ -24,6 +24,8 @@ void anteraja_order(const char *, const char *, const char *, const char *, cons
void anteraja_cleanup();
const char **sicepat_init(char *[]);
+const char **sicepat_origins();
+const char **sicepat_destinations();
const char **sicepat_services(const char *, const char *, double);
static void recurse(struct json_object *outer, const char *trail[], struct json_object **last)
@@ -62,6 +64,46 @@ static size_t handle(char *contents, size_t size, size_t nmemb, struct shipping
return realsize;
}
switch (shipping->mode) {
+ case PLACES:
+ ;
+ struct json_object *place_array = NULL;
+ recurse(response, shipping->trail, &place_array);
+ size_t places_length = json_object_array_length(place_array);
+ struct pikul_place **places = malloc(sizeof(struct pikul_place *)
+ * (places_length + 1));
+ const char **place_attrs = (const char **)shipping->data;
+ enum { PLACE_CODE, DISTRICT, CITY, PROVINCE };
+ for (size_t i = 0; i < places_length; i++) {
+ places[i] = malloc(sizeof(struct pikul_place));
+ struct pikul_place *place = places[i];
+ json_object *object = json_object_array_get_idx(place_array, 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 *name = json_object_iter_peek_name(&iterator);
+ json_object *value = json_object_iter_peek_value(&iterator);
+ int len = json_object_get_string_len(value);
+ if (len) {
+ char *string = malloc(len + 1);
+ strcpy(string, json_object_get_string(value));
+ if (!strcmp(name, place_attrs[PLACE_CODE]))
+ place->code = string;
+ else if (place_attrs[DISTRICT]
+ && !strcmp(name, place_attrs[DISTRICT]))
+ place->district = string;
+ else if (place_attrs[CITY]
+ && !strcmp(name, place_attrs[CITY]))
+ place->city = string;
+ else if (place_attrs[PROVINCE]
+ && !strcmp(name, place_attrs[PROVINCE]))
+ place->province = string;
+ }
+ json_object_iter_next(&iterator);
+ }
+ }
+ places[places_length] = NULL;
+ shipping->data = places;
+ break;
case SERVICES:
;
struct json_object *array = NULL;
@@ -147,6 +189,42 @@ void pikul_init(enum pikul_company company, char *provisions[])
shipping->tokener = json_tokener_new();
}
+struct pikul_place **pikul_origins(enum pikul_company company)
+{
+ struct shipping *shipping = shipping_list[company];
+ switch (company) {
+ case PIKUL_SICEPAT:
+ shipping->data = sicepat_origins();
+ break;
+ default:
+ break;
+ }
+ curl_easy_setopt(shipping->handle, CURLOPT_URL, shipping->url);
+ curl_easy_setopt(shipping->handle, CURLOPT_HTTPGET, 1L);
+ shipping->mode = PLACES;
+ curl_easy_perform(shipping->handle);
+ free(shipping->url);
+ return (struct pikul_place **)shipping->data;
+}
+
+struct pikul_place **pikul_destinations(enum pikul_company company)
+{
+ struct shipping *shipping = shipping_list[company];
+ switch (company) {
+ case PIKUL_SICEPAT:
+ shipping->data = sicepat_destinations();
+ break;
+ default:
+ break;
+ }
+ curl_easy_setopt(shipping->handle, CURLOPT_URL, shipping->url);
+ curl_easy_setopt(shipping->handle, CURLOPT_HTTPGET, 1L);
+ shipping->mode = PLACES;
+ curl_easy_perform(shipping->handle);
+ free(shipping->url);
+ return (struct pikul_place **)shipping->data;
+}
+
struct pikul_service **pikul_services(enum pikul_company company,
const char *origin, const char *destination, double weight)
{
diff --git a/pikul.h b/pikul.h
index 26cfc4c..9b60181 100644
--- a/pikul.h
+++ b/pikul.h
@@ -8,6 +8,13 @@ enum pikul_company {
PIKUL_END
};
+struct pikul_place {
+ char *code;
+ char *district;
+ char *city;
+ char *province;
+};
+
struct pikul_service {
char *code;
char *name;
@@ -20,6 +27,8 @@ extern "C" {
#endif
void pikul_init(enum pikul_company company, char *provisions[]);
+struct pikul_place **pikul_origins(enum pikul_company company);
+struct pikul_place **pikul_destinations(enum pikul_company company);
struct pikul_service **pikul_services(enum pikul_company company,
const char *origin, const char *destination, double weight);
void pikul_free_services(struct pikul_service **services);
diff --git a/private.h b/private.h
index 954d29e..1810c69 100644
--- a/private.h
+++ b/private.h
@@ -11,6 +11,7 @@ extern struct shipping {
char *post;
json_tokener *tokener;
enum {
+ PLACES,
SERVICES,
ORDER
} mode;
diff --git a/sicepat.c b/sicepat.c
index 9dd3d19..549bdfc 100644
--- a/sicepat.c
+++ b/sicepat.c
@@ -3,6 +3,8 @@
#include "private.h"
#define BASE "http://api.sicepat.com/customer/"
+#define ORIGINS "origin"
+#define DESTINATIONS "destination"
const char **sicepat_init(char *provisions[])
{
@@ -20,6 +22,46 @@ const char **sicepat_init(char *provisions[])
return fields;
}
+const char **sicepat_origins()
+{
+ struct shipping *shipping = shipping_list[PIKUL_SICEPAT];
+ shipping->url = malloc(strlen(BASE) + strlen(ORIGINS) + 1);
+ sprintf(shipping->url, "%s%s", BASE, ORIGINS);
+ static const char *trail[] = {
+ "sicepat",
+ "results",
+ NULL
+ };
+ shipping->trail = trail;
+ static const char *attributes[] = {
+ "origin_code",
+ NULL,
+ "origin_name",
+ NULL
+ };
+ return attributes;
+}
+
+const char **sicepat_destinations()
+{
+ struct shipping *shipping = shipping_list[PIKUL_SICEPAT];
+ shipping->url = malloc(strlen(BASE) + strlen(DESTINATIONS) + 1);
+ sprintf(shipping->url, "%s%s", BASE, DESTINATIONS);
+ static const char *trail[] = {
+ "sicepat",
+ "results",
+ NULL
+ };
+ shipping->trail = trail;
+ static const char *attributes[] = {
+ "destination_code",
+ "subdistrict",
+ "city",
+ "province"
+ };
+ return attributes;
+}
+
const char **sicepat_services(const char *origin, const char *destination, double weight)
{
struct shipping *shipping = shipping_list[PIKUL_SICEPAT];