Commit 3c7f9c47 authored by Jussi Kukkonen's avatar Jussi Kukkonen

Make GcWebService return GError on error

Also patch the providers to forward the error to clients
parent 9d1eb858
......@@ -70,6 +70,7 @@
#include <libxml/uri.h> /* for xmlURIEscapeStr */
#include "gc-web-service.h"
#include "geoclue-error.h"
G_DEFINE_TYPE (GcWebService, gc_web_service, G_TYPE_OBJECT)
......@@ -145,7 +146,7 @@ gc_web_service_build_xpath_context (GcWebService *self)
/* fetch data from url, save into self->response */
static gboolean
gc_web_service_fetch (GcWebService *self, gchar *url)
gc_web_service_fetch (GcWebService *self, gchar *url, GError **error)
{
void* ctxt = NULL;
gint len;
......@@ -156,20 +157,29 @@ gc_web_service_fetch (GcWebService *self, gchar *url)
self->response = NULL;
xmlXPathFreeContext (self->xpath_ctx);
self->xpath_ctx = NULL;
g_assert (url);
xmlNanoHTTPInit();
ctxt = xmlNanoHTTPMethod (url, "GET", NULL, NULL, NULL, 0);
if (!ctxt) {
g_printerr ("xmlNanoHTTPMethod did not get a response\n");
*error = g_error_new (GEOCLUE_ERROR,
GEOCLUE_ERROR_NOT_AVAILABLE,
g_strdup_printf ("xmlNanoHTTPMethod did not get a response from %s\n", url));
return FALSE;
}
output = xmlBufferCreate ();
while ((len = xmlNanoHTTPRead (ctxt, buf, sizeof(buf))) > 0) {
if (xmlBufferAdd (output, buf, len) != 0) {
g_printerr ("xmlBufferAdd failed\n");
break;
xmlNanoHTTPClose(ctxt);
xmlBufferFree (output);
*error = g_error_new (GEOCLUE_ERROR,
GEOCLUE_ERROR_FAILED,
g_strdup_printf ("libxml error (xmlBufferAdd failed)"));
return FALSE;
}
}
xmlNanoHTTPClose(ctxt);
......@@ -178,7 +188,7 @@ gc_web_service_fetch (GcWebService *self, gchar *url)
self->response = g_memdup (xmlBufferContent (output), self->response_length);
xmlBufferFree (output);
return (self->response_length > 0);
return TRUE;
}
static xmlXPathObject*
......@@ -304,7 +314,7 @@ gc_web_service_add_namespace (GcWebService *self, gchar *namespace, gchar *uri)
* Return value: %TRUE on success.
*/
gboolean
gc_web_service_query (GcWebService *self, ...)
gc_web_service_query (GcWebService *self, GError **error, ...)
{
va_list list;
gchar *key, *value, *esc_value, *tmp, *url;
......@@ -316,12 +326,12 @@ gc_web_service_query (GcWebService *self, ...)
/* read the arguments one key-value pair at a time,
add the pairs to url as "?key1=value1&key2=value2&..." */
va_start (list, self);
va_start (list, error);
key = va_arg (list, char*);
while (key) {
value = va_arg (list, char*);
esc_value = (gchar *)xmlURIEscapeStr ((xmlChar *)value, NULL);
g_return_val_if_fail (esc_value, FALSE);
if (first_pair) {
tmp = g_strdup_printf ("%s?%s=%s", url, key, esc_value);
first_pair = FALSE;
......@@ -335,7 +345,7 @@ gc_web_service_query (GcWebService *self, ...)
}
va_end (list);
if (!gc_web_service_fetch (self, url)) {
if (!gc_web_service_fetch (self, url, error)) {
g_free (url);
return FALSE;
}
......
......@@ -41,7 +41,7 @@ GType gc_web_service_get_type (void);
void gc_web_service_set_base_url (GcWebService *self, gchar *url);
gboolean gc_web_service_add_namespace (GcWebService *self, gchar *namespace, gchar *uri);
gboolean gc_web_service_query (GcWebService *self, ...);
gboolean gc_web_service_query (GcWebService *self, GError **error, ...);
gboolean gc_web_service_get_string (GcWebService *self, gchar **value, gchar *xpath);
gboolean gc_web_service_get_double (GcWebService *self, gdouble *value, gchar *xpath);
......
......@@ -114,14 +114,12 @@ geoclue_geonames_address_to_position (GcIfaceGeocode *iface,
*fields = GEOCLUE_POSITION_FIELDS_NONE;
if (countrycode && postalcode) {
if (!gc_web_service_query (obj->postalcode_geocoder,
if (!gc_web_service_query (obj->postalcode_geocoder, error,
"postalcode", postalcode,
"country", countrycode,
"maxRows", "1",
"style", "FULL",
(char *)0)) {
g_set_error (error, GEOCLUE_ERROR,
GEOCLUE_ERROR_NOT_AVAILABLE, "Web service query failed");
return FALSE;
}
if (gc_web_service_get_double (obj->postalcode_geocoder,
......@@ -134,14 +132,12 @@ geoclue_geonames_address_to_position (GcIfaceGeocode *iface,
0, 0);
}
} else if (countrycode && locality) {
if (!gc_web_service_query (obj->place_geocoder,
if (!gc_web_service_query (obj->place_geocoder, error,
"name", locality,
"country", countrycode,
"maxRows", "1",
"style", "FULL",
(char *)0)) {
g_set_error (error, GEOCLUE_ERROR,
GEOCLUE_ERROR_NOT_AVAILABLE, "Web service query failed");
return FALSE;
}
if (gc_web_service_get_double (obj->place_geocoder,
......@@ -186,7 +182,7 @@ geoclue_geonames_position_to_address (GcIfaceReverseGeocode *iface,
}
g_ascii_dtostr (lat, G_ASCII_DTOSTR_BUF_SIZE, latitude);
g_ascii_dtostr (lon, G_ASCII_DTOSTR_BUF_SIZE, longitude);
if (!gc_web_service_query (obj->rev_place_geocoder,
if (!gc_web_service_query (obj->rev_place_geocoder, error,
"lat", lat,
"lng", lon,
"featureCode","PPL", /* http://www.geonames.org/export/codes.html*/
......@@ -199,8 +195,6 @@ geoclue_geonames_position_to_address (GcIfaceReverseGeocode *iface,
"maxRows", "1",
"style", "FULL",
(char *)0)) {
g_set_error (error, GEOCLUE_ERROR,
GEOCLUE_ERROR_NOT_AVAILABLE, "Web service query failed");
return FALSE;
}
......
......@@ -198,7 +198,7 @@ geoclue_gsmloc_get_position (GcIfacePosition *iface,
*timestamp = time (NULL);
}
if (!gc_web_service_query (gsmloc->web_service,
if (!gc_web_service_query (gsmloc->web_service, error,
"action", "get",
"format", "xml",
"nid1", mcc,
......@@ -206,10 +206,6 @@ geoclue_gsmloc_get_position (GcIfacePosition *iface,
"lac", lac,
"cid", cid,
(char *)0)) {
g_set_error (error, GEOCLUE_ERROR,
GEOCLUE_ERROR_NOT_AVAILABLE,
"Web service query failed");
g_free (mcc);
g_free (mnc);
g_free (lac);
......
......@@ -81,9 +81,7 @@ geoclue_hostip_get_position (GcIfacePosition *iface,
*fields = GEOCLUE_POSITION_FIELDS_NONE;
if (!gc_web_service_query (obj->web_service, (char *)0)) {
g_set_error (error, GEOCLUE_ERROR,
GEOCLUE_ERROR_NOT_AVAILABLE, "Web service query failed");
if (!gc_web_service_query (obj->web_service, error, (char *)0)) {
return FALSE;
}
......@@ -123,9 +121,7 @@ geoclue_hostip_get_address (GcIfaceAddress *iface,
gchar *country = NULL;
gchar *country_code = NULL;
if (!gc_web_service_query (obj->web_service, (char *)0)) {
g_set_error (error, GEOCLUE_ERROR,
GEOCLUE_ERROR_NOT_AVAILABLE, "Web service query failed");
if (!gc_web_service_query (obj->web_service, error, (char *)0)) {
return FALSE;
}
......
......@@ -145,12 +145,9 @@ geoclue_plazes_get_position (GcIfacePosition *iface,
return FALSE;
}
if (!gc_web_service_query (plazes->web_service,
if (!gc_web_service_query (plazes->web_service, error,
PLAZES_KEY_MAC, mac,
(char *)0)) {
g_set_error (error, GEOCLUE_ERROR,
GEOCLUE_ERROR_NOT_AVAILABLE,
"Web service query failed");
g_free (mac);
return FALSE;
}
......@@ -207,12 +204,9 @@ geoclue_plazes_get_address (GcIfaceAddress *iface,
return FALSE;
}
if (!gc_web_service_query (plazes->web_service,
if (!gc_web_service_query (plazes->web_service, error,
PLAZES_KEY_MAC, mac,
(char *)0)) {
g_set_error (error, GEOCLUE_ERROR,
GEOCLUE_ERROR_NOT_AVAILABLE,
"Web service query failed");
g_free (mac);
return FALSE;
}
......
......@@ -102,15 +102,13 @@ geoclue_yahoo_address_to_position (GcIfaceGeocode *iface,
locality = get_address_value (address, GEOCLUE_ADDRESS_KEY_LOCALITY);
region = get_address_value (address, GEOCLUE_ADDRESS_KEY_REGION);
if (!gc_web_service_query (yahoo->web_service,
if (!gc_web_service_query (yahoo->web_service, error,
"appid", YAHOO_GEOCLUE_APP_ID,
"street", street,
"zip", postalcode,
"city", locality,
"state", region,
(char *)0)) {
g_set_error (error, GEOCLUE_ERROR,
GEOCLUE_ERROR_NOT_AVAILABLE, "Web service query failed");
return FALSE;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment