summaryrefslogtreecommitdiff
path: root/anteraja.c
blob: cf9d68510f797c6c42f53f5ce0917b627ecc69f5 (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
#include "shipping.h"
#include "handler.h"

extern CURL *curl;

void anteraja_init(char *provisions[], struct shipping *shipping)
{
	shipping->base = malloc(strlen(*provisions) + 1);
	strcpy(shipping->base, *provisions);
	headers(shipping, (const char *[]){ "access-key-id", "secret-access-key", NULL }, ++provisions);
	shipping->headers = curl_slist_append(shipping->headers, "Content-Type:application/json");
}

void anteraja_services_request(const char *origin, const char *destination, double weight,
		struct shipping *shipping, char **url, char **post)
{
	static const char *path = "serviceRates";
	*url = malloc(strlen(shipping->base) + strlen(path) + 1);
	sprintf(*url, "%s%s", shipping->base, path);
	static const char *format = "{"
		"\"origin\": \"xx.xx.xx\","
		"\"destination\": \"xx.xx.xx\","
		"\"weight\": xxxxx"
		"}";
	*post = malloc(strlen(format) + 1);
	sprintf(*post, "{\"origin\": \"%s\",\"destination\": \"%s\",\"weight\": %d}",
			origin, destination, (int)weight);
	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(contents, realsize, &(struct container){ services, (const char *[]){
		"product_code",
		"product_name",
		"etd",
		"rates",
		"content",
		"services",
		NULL
	}});
	return realsize;
}