summaryrefslogtreecommitdiff
path: root/anteraja.c
blob: 2fc265c8a5cf7b5ac4443bb13ad5e4d18a0f0f1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "shipping.h"
#include "handler.h"

#define SERVICES_PATH "serviceRates"
#define SERVICES_POST \
"{\
\"origin\":\"%s\",\
\"destination\":\"%s\",\
\"weight\":%d\
}"

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((const char *[]){ "access-key-id", "secret-access-key", NULL },
			&provisions[ACCESS_KEY], shipping);
	shipping->headers = curl_slist_append(shipping->headers, "Content-Type:application/json");
}

void anteraja_services(const char *origin, const char *destination, double weight,
		struct shipping *shipping, char **url, char **post)
{
	*url = malloc(strlen(shipping->base) + strlen(SERVICES_PATH) + 1);
	sprintf(*url, "%s%s", shipping->base, SERVICES_PATH);
	*post = malloc(strlen(SERVICES_POST) + strlen(origin) + strlen(destination) + strlen("50000")
			- 2 * strlen("%s") - strlen("%d") + 1);
	sprintf(*post, SERVICES_POST, origin, destination, weight < 1.0 ? 1000 : (int)weight * 1000);
	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post);
}

size_t anteraja_services_handle(const char *contents, size_t size, size_t nmemb,
		struct pikul_services **services)
{
	size_t realsize = size * nmemb;
	handle_services(contents, realsize, status_trail, (const char *[]){
			"content",
			"services",
			NULL
			}, (const char *[]){
			"product_code",
			"product_name",
			"etd",
			"rates"
			}, services);
	return realsize;
}