Skip to content
This repository has been archived by the owner on Nov 18, 2023. It is now read-only.

Commit

Permalink
Status outlet for [rest-json].
Browse files Browse the repository at this point in the history
  • Loading branch information
residuum committed Apr 2, 2012
1 parent 2a60bf9 commit e4c4ee1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -13,7 +13,7 @@ SOURCES = rest-json.c json-decode.c json-encode.c
PDOBJECTS =

# example patches and related files, in the 'examples' subfolder
EXAMPLES = purest-json-test.pd the-sound-of-money.pd twitter-visualization.pd
EXAMPLES = purest-json-test.pd the-sound-of-money.pd twitter-visualization.pd statistics.pd

# manuals and related files, in the 'manual' subfolder
MANUAL = index.html couchdb-example.png webservice-example.png
Expand Down
1 change: 1 addition & 0 deletions purest_json.h
Expand Up @@ -27,6 +27,7 @@ typedef struct key_value_pair {
typedef struct rest {
t_object x_ob;
t_outlet *done_outlet;
t_outlet *status_info_outlet;
int out_count;
char base_url[MAXPDSTRING];
/* cookie authentication */
Expand Down
46 changes: 37 additions & 9 deletions rest-json.c
Expand Up @@ -42,6 +42,8 @@ static void *execute_rest_thread(void *thread_args) {
CURLcode result;
t_memory_struct in_memory;
t_memory_struct out_memory;
long http_code;
t_atom http_status_data[3];

curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
Expand Down Expand Up @@ -73,17 +75,28 @@ static void *execute_rest_thread(void *thread_args) {
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&out_memory);
result = curl_easy_perform(curl_handle);

if (result == CURLE_OK) {
x->is_data_locked = 0;
output_json_string(out_memory.memory, x->x_ob.ob_outlet, x->done_outlet);
/* Free memory */
if (out_memory.memory) {
freebytes(out_memory.memory, (out_memory.size + 1) * sizeof(char));
/* output status */
curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_code);
SETSYMBOL(&http_status_data[0], gensym(x->request_type));
if (http_code == 200 && result == CURLE_OK) {
SETSYMBOL(&http_status_data[1], gensym("bang"));
outlet_list(x->status_info_outlet, &s_list, 2, &http_status_data[0]);
if (result == CURLE_OK) {
x->is_data_locked = 0;
output_json_string(out_memory.memory, x->x_ob.ob_outlet, x->done_outlet);
/* Free memory */
if (out_memory.memory) {
freebytes(out_memory.memory, (out_memory.size + 1) * sizeof(char));
}
free((void *)result);
} else {
x->is_data_locked = 0;
error("Error while performing request: %s", curl_easy_strerror(result));
}
free((void *)result);
} else {
x->is_data_locked = 0;
error("Error while performing request: %s", curl_easy_strerror(result));
SETFLOAT(&http_status_data[1], (float)http_code);
SETFLOAT(&http_status_data[2], (float)result);
outlet_list(x->status_info_outlet, &s_list, 3, &http_status_data[0]);
}
curl_easy_cleanup(curl_handle);
} else {
Expand Down Expand Up @@ -118,6 +131,8 @@ static void *get_auth_token_thread(void *thread_args) {
size_t post_data_length;
char *header_line;
char *cookie_params;
long http_code;
t_atom auth_status_data[3];

curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
Expand Down Expand Up @@ -149,6 +164,18 @@ static void *get_auth_token_thread(void *thread_args) {
curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER, (void *)&out_header);
result = curl_easy_perform(curl_handle);

/* output the status code at third outlet */
curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_code);
SETSYMBOL(&auth_status_data[0], gensym("cookie"));
if (http_code == 200 && result == CURLE_OK) {
SETSYMBOL(&auth_status_data[1], gensym("bang"));
outlet_list(x->status_info_outlet, &s_list, 2, &auth_status_data[0]);
} else {
SETFLOAT(&auth_status_data[1], (float)http_code);
SETFLOAT(&auth_status_data[2], (float)result);
outlet_list(x->status_info_outlet, &s_list, 3, &auth_status_data[0]);
}

if (result == CURLE_OK) {
if (out_header.memory) {
header_line = strtok(out_header.memory, "\n");
Expand Down Expand Up @@ -325,6 +352,7 @@ void *rest_new(t_symbol *selector, int argcount, t_atom *argvec) {

outlet_new(&x->x_ob, NULL);
x->done_outlet = outlet_new(&x->x_ob, &s_bang);
x->status_info_outlet = outlet_new(&x->x_ob, NULL);
x->is_data_locked = 0;

return (void *)x;
Expand Down

0 comments on commit e4c4ee1

Please sign in to comment.