modularized api stuff (a step forward to support multiple protocols)

This commit is contained in:
Steffen Vogel 2012-02-03 15:26:07 +01:00
parent a15aa43ca3
commit 17865a660c
3 changed files with 42 additions and 28 deletions

View file

@ -39,7 +39,13 @@ typedef struct {
size_t size;
} CURLresponse;
CURL * api_curl_init(channel_t *ch);
typedef struct {
CURL *curl;
struct curl_slist *headers;
} api_handle_t;
int api_init(channel_t *ch, api_handle_t *api);
void api_free(api_handle_t *api);
/**
* Reformat CURLs debugging output

View file

@ -107,32 +107,35 @@ json_object * api_json_tuples(buffer_t *buf, reading_t *first, reading_t *last)
return json_tuples;
}
CURL * api_curl_init(channel_t *ch) {
CURL *curl;
struct curl_slist *header = NULL;
int api_init(channel_t *ch, api_handle_t *api) {
char url[255], agent[255];
/* prepare header, uuid & url */
sprintf(agent, "User-Agent: %s/%s (%s)", PACKAGE, VERSION, curl_version()); /* build user agent */
sprintf(url, "%s/data/%s.json", ch->middleware, ch->uuid); /* build url */
header = curl_slist_append(header, "Content-type: application/json");
header = curl_slist_append(header, "Accept: application/json");
header = curl_slist_append(header, agent);
api->headers = NULL;
api->headers = curl_slist_append(api->headers, "Content-type: application/json");
api->headers = curl_slist_append(api->headers, "Accept: application/json");
api->headers = curl_slist_append(api->headers, agent);
curl = curl_easy_init();
if (!curl) {
print(log_error, "CURL: cannot create handle", ch);
exit(EXIT_FAILURE);
api->curl = curl_easy_init();
if (!api->curl) {
return EXIT_FAILURE;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
curl_easy_setopt(curl, CURLOPT_VERBOSE, options.verbosity);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, curl_custom_debug_callback);
curl_easy_setopt(curl, CURLOPT_DEBUGDATA, (void *) ch);
curl_easy_setopt(api->curl, CURLOPT_URL, url);
curl_easy_setopt(api->curl, CURLOPT_HTTPHEADER, api->headers);
curl_easy_setopt(api->curl, CURLOPT_VERBOSE, options.verbosity);
curl_easy_setopt(api->curl, CURLOPT_DEBUGFUNCTION, curl_custom_debug_callback);
curl_easy_setopt(api->curl, CURLOPT_DEBUGDATA, (void *) ch);
return curl;
return EXIT_SUCCESS;
}
void api_free(api_handle_t *api) {
curl_easy_cleanup(api->curl);
curl_slist_free_all(api->headers);
}
void api_parse_exception(CURLresponse response, char *err, size_t n) {

View file

@ -129,8 +129,6 @@ void * reading_thread(void *arg) {
while (dump == NULL || buffer_dump(buf, dump, dump_len) == NULL) {
dump_len *= 1.5;
print(log_debug, "New dump_len: %i", ch ,dump_len);
free(dump);
dump = malloc(dump_len);
}
@ -153,14 +151,21 @@ void * reading_thread(void *arg) {
}
void logging_thread_cleanup(void *arg) {
curl_easy_cleanup((CURL *) arg); /* always cleanup */
api_handle_t *api = (api_handle_t *) arg;
api_free(api);
}
void * logging_thread(void *arg) {
channel_t *ch = (channel_t *) arg; /* casting argument */
CURL *curl = api_curl_init(ch);
pthread_cleanup_push(&logging_thread_cleanup, curl);
api_handle_t api;
if (api_init(ch, &api) != SUCCESS) {
print(log_error, "CURL: cannot create handle", ch);
exit(EXIT_FAILURE);
}
pthread_cleanup_push(&logging_thread_cleanup, &api);
do { /* start thread mainloop */
CURLresponse response;
@ -186,12 +191,12 @@ void * logging_thread(void *arg) {
print(log_debug, "JSON request body: %s", ch, json_str);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_str);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_custom_write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &response);
curl_easy_setopt(api.curl, CURLOPT_POSTFIELDS, json_str);
curl_easy_setopt(api.curl, CURLOPT_WRITEFUNCTION, curl_custom_write_callback);
curl_easy_setopt(api.curl, CURLOPT_WRITEDATA, (void *) &response);
curl_code = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
curl_code = curl_easy_perform(api.curl);
curl_easy_getinfo(api.curl, CURLINFO_RESPONSE_CODE, &http_code);
/* check response */
if (curl_code == CURLE_OK && http_code == 200) { /* everything is ok */