/* *Delete location feed from cosm. Return EINA_TRUE if success. */ Eina_Bool cosm_location_feed_delete(Location *location) { Ecore_Con_Url *cosm_url = NULL; char *s; //Don't delete if no cosm feedid cosm or null location if(!location || (location_cosm_feedid_get(location) == 0) || !edams_settings_cosm_apikey_get()) return EINA_FALSE; int feedid = location_cosm_feedid_get(location); debug(MSG_COSM, _("Deleting Cosm feed '%s'..."), location_name_get(location)); ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, _url_feed_delete_complete_cb, (void*)feedid); asprintf(&s, "http://api.cosm.com/v2/feeds/%d", location_cosm_feedid_get(location)); cosm_url = ecore_con_url_custom_new(s, "DELETE"); if (!cosm_url) { debug(MSG_ERROR, _("Can't create Ecore_Con_Url object")); return EINA_FALSE; } //ecore_con_url_verbose_set(cosm_url, edams_settings_debug_get()); ecore_con_url_additional_header_add(cosm_url, "X-ApiKey", edams_settings_cosm_apikey_get()); if(!ecore_con_url_post(cosm_url, (void*)s, strlen(s), NULL)) { debug(MSG_ERROR, _("Can't realize url PUT request")); return EINA_FALSE; } FREE(s); return EINA_TRUE; }/*cosm_location_feed_delete*/
/* *Add location feed to cosm. Return EINA_TRUE if success. */ Eina_Bool cosm_location_feed_add(Location *location) { Ecore_Con_Url *cosm_url = NULL; char *s; //Don't add cosm url feed if already there... if(!location || (location_cosm_feedid_get(location) != 0) || !edams_settings_cosm_apikey_get()) return EINA_FALSE; debug(MSG_COSM, _("Creating cosm feed '%s'..."), location_name_get(location)); ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, (Ecore_Event_Handler_Cb)_url_feed_add_complete_cb,NULL); cosm_url = ecore_con_url_custom_new("http://api.cosm.com/v2/feeds", "POST"); if (!cosm_url) { debug(MSG_COSM, _("Can't create Ecore_Con_Url object")); return EINA_FALSE; } //ecore_con_url_verbose_set(cosm_url, edams_settings_debug_get()); ecore_con_url_additional_header_add(cosm_url, "X-ApiKey", edams_settings_cosm_apikey_get());; ecore_con_url_data_set(cosm_url, (void*)location); char *locale = setlocale(LC_NUMERIC, NULL); setlocale(LC_NUMERIC, "POSIX"); cJSON *root,*fmt; root=cJSON_CreateObject(); asprintf(&s, "%s sensor.basic", location_name_get(location)); cJSON_AddItemToObject(root, "title", cJSON_CreateString(s)); FREE(s); cJSON_AddItemToObject(root, "version", cJSON_CreateString("1.0.0")); cJSON_AddItemToObject(root, "location", fmt=cJSON_CreateObject()); cJSON_AddStringToObject(fmt, "name", location_name_get(location)); //cJSON_AddStringToObject(fmt, "description", location_description_get(location)); cJSON_AddStringToObject(fmt, "disposition", "fixed"); cJSON_AddStringToObject(fmt, "exposure", "indoor"); cJSON_AddStringToObject(fmt, "domain", "physical"); if(location_latitude_get(location) != -1) cJSON_AddNumberToObject(fmt, "lat", location_latitude_get(location)); if(location_longitude_get(location) != -1) cJSON_AddNumberToObject(fmt, "lon", location_longitude_get(location)); s = cJSON_PrintUnformatted(root); cJSON_Delete(root); setlocale(LC_NUMERIC, locale); if(!ecore_con_url_post(cosm_url, (void*)s, strlen(s), NULL)) { debug(MSG_COSM, _("Can't realize url PUT request")); return EINA_FALSE; } FREE(s); return EINA_TRUE; }/*cosm_location_feed_add*/
/* *Update datastream from cosm with device's data. Return EINA_TRUE if success. */ Eina_Bool cosm_device_datastream_update(Location *location, Widget *widget) { Ecore_Con_Url *cosm_url = NULL; char *s; /*Don't add cosm device datastream if no feed available*/ if(!location || (location_cosm_feedid_get(location) == 0) || !edams_settings_cosm_apikey_get()) return EINA_FALSE; ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, _url_datastream_update_complete_cb, NULL); asprintf(&s, "http://api.cosm.com/v2/feeds/%d", location_cosm_feedid_get(location)); cosm_url = ecore_con_url_custom_new(s, "PUT"); if (!cosm_url) { debug(MSG_COSM, _("Can't create Ecore_Con_Url object")); return EINA_FALSE; } FREE(s); //ecore_con_url_verbose_set(cosm_url, edams_settings_debug_get()); ecore_con_url_additional_header_add(cosm_url, "X-ApiKey", edams_settings_cosm_apikey_get()); cJSON *root, *datastreams, *fmt,*unit; root=cJSON_CreateObject(); cJSON_AddStringToObject(root,"version", "1.0.0"); cJSON_AddItemToObject(root, "datastreams", datastreams=cJSON_CreateArray()); fmt=cJSON_CreateObject(); cJSON_AddStringToObject(fmt,"current_value", widget_xpl_current_get(widget)); cJSON_AddStringToObject(fmt,"id", widget_xpl_device_get(widget)); if(xpl_type_to_unit_symbol(widget_xpl_type_get(widget))) { cJSON_AddItemToObject(fmt, "unit", unit=cJSON_CreateObject()); cJSON_AddStringToObject(unit,"label", xpl_type_to_units(widget_xpl_type_get(widget))); cJSON_AddStringToObject(unit,"symbol", xpl_type_to_unit_symbol(widget_xpl_type_get(widget))); } cJSON_AddItemToArray(datastreams, fmt); s = cJSON_PrintUnformatted(root); cJSON_Delete(root); if(!ecore_con_url_post(cosm_url, (void*)s, strlen(s), "text/json")) { debug(MSG_COSM, _("Can't realize url PUT request")); return EINA_FALSE; } FREE(s); return EINA_TRUE; }/*cosm_device_datastream_update*/
static Ecore_Con_Url * _fetch(Eina_Strbuf *sb) { Ecore_Con_Url *f; const char *qs; qs = eina_strbuf_string_get(sb); if (!qs) return NULL; f = ecore_con_url_new(qs); if (!f) return NULL; ecore_con_url_additional_header_add (f, "user-agent", "Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19"); ecore_con_url_get(f); return f; }
END_TEST #endif #ifdef ECORE_CON_HTTP_TEST_URL START_TEST(ecore_con_test_ecore_con_url_post) { Ecore_Con_Url *ec_url; url_test *info; int ret; char link[] = ECORE_CON_HTTP_TEST_URL; char url_data[] = "test"; char *username = NULL, *password = NULL; char url[4096]; ret = eina_init(); fail_if(ret != 1); ret = ecore_con_url_init(); fail_if(ret != 1); fail_unless(_parse_url(link, url, &username, &password, NULL, NULL)); fprintf (stderr, "HTTP: \n url = %s \n username = %s \n password = %s \n", url, username, password); ecore_con_url_pipeline_set(EINA_TRUE); fail_unless (ecore_con_url_pipeline_get()); ec_url = ecore_con_url_custom_new(url, "POST"); fail_unless (ec_url); ecore_con_url_additional_header_add(ec_url, "User-Agent", "blablabla"); ecore_con_url_verbose_set(ec_url, EINA_TRUE); ecore_con_url_httpauth_set(ec_url, username, password, EINA_FALSE); ecore_con_url_time(ec_url, ECORE_CON_URL_TIME_IFMODSINCE, 0); fail_unless(ecore_con_url_post(ec_url, url_data, 4, NULL)); ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, _url_compl_cb, info); ret = ecore_con_url_shutdown(); fail_if(ret != 0); ret = eina_shutdown(); }