[geocode-glib/gnome-maps: 1/6] geoip-lookup: Use same JSON format as other free services



commit 6bd5056c7490fef0db8aec1597a46bede5b97649
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Thu Mar 21 20:14:26 2013 +0200

    geoip-lookup: Use same JSON format as other free services
    
    While there is no standard on format of JSON strings returned by geoip
    services, we better use the same format used by existing free services.
    Since freegeoip.net is the most famous one I know, I followed their
    format. hostip.info is another that seems to be using the same format[1]
    as well.
    
    This would also mean that apps can easily switch between our service and
    other. Since our service is not yet running anywhere online, this will
    also make it easy for any app to use our Ipclient API already w/o having
    to figure the external IP of the machine they are running on.
    
    One thing we do differently than these other services is handling of
    errors. Both freegeoip.net and hostip.info silently ignore invalid IP
    and give bugos values while we provide proper errors in the JSON response.
    
    This patch also adds some NULL checks.
    
    [1] http://www.hostip.info/use.html
    
    https://bugzilla.gnome.org/show_bug.cgi?id=696525

 geocode-glib/geocode-ip-server/geoip-lookup.c |  143 +++++++++----------------
 1 files changed, 53 insertions(+), 90 deletions(-)
---
diff --git a/geocode-glib/geocode-ip-server/geoip-lookup.c b/geocode-glib/geocode-ip-server/geoip-lookup.c
index 7a367c5..bcfb52d 100644
--- a/geocode-glib/geocode-ip-server/geoip-lookup.c
+++ b/geocode-glib/geocode-ip-server/geoip-lookup.c
@@ -14,43 +14,21 @@ enum ERROR_CODES {
         DATABASE_ERR
 };
 static char *error_message_array [] = {
-        "Invalid IP address input",
-        "Can not find the IP address in the database",
+        "Invalid IP address '%s'",
+        "Can not find the IP address '%s' in the database",
         "Can not open GeoLiteCity/GeoIP Binary database. Set GEOIP_DATABASE_PATH env variable."
 };
 
 static void
-add_json_object_for_address (JsonBuilder *builder,
-                             const char  *name,
-                             const char  *code,
-                             const char  *type)
-{
-        if (!name && !code)
-                return;
-        json_builder_begin_object (builder);
-        if (name) {
-                json_builder_set_member_name (builder, "name");
-                json_builder_add_string_value (builder, name);
-        }
-        if (code) {
-                json_builder_set_member_name (builder, "code");
-                json_builder_add_string_value (builder, code);
-        }
-        json_builder_set_member_name (builder, "type");
-        json_builder_add_string_value (builder, type);
-        json_builder_end_object (builder);
-}
-
-static void
 print_error_in_json (int        error_code,
                      const char *extra_info)
 {
-        g_print ("{\"results\":\"%s",error_message_array[error_code]);
+        g_print ("{\"error_code\":%d, \"error_message\":\"", error_code);
         if (extra_info)
-                g_print (" - %s\"", extra_info);
+                g_print (error_message_array[error_code], extra_info);
         else
-                g_print ("\"");
-        g_print (",\"status\":\"ERROR\"}\n");
+                g_print (error_message_array[error_code]);
+        g_print ("\"}\n");
 }
 
 static JsonBuilder*
@@ -71,37 +49,55 @@ add_result_attr_to_json_tree (const char *ipaddress,
 
         json_builder_begin_object (builder); /* begin */
 
-        json_builder_set_member_name (builder, "results");
-        json_builder_begin_object (builder); /* begin results object */
-
-        json_builder_set_member_name (builder, ipaddress);
-        json_builder_begin_array (builder); /* begin ipaddress array */
+        json_builder_set_member_name (builder, "ip");
+        json_builder_add_string_value (builder, ipaddress);
 
-        json_builder_begin_object (builder); /* begin ipaddress object */
-
-        json_builder_set_member_name (builder, "location");
-        json_builder_begin_object (builder); /* begin location object */
         json_builder_set_member_name (builder, "latitude");
         json_builder_add_double_value (builder, gir->latitude);
         json_builder_set_member_name (builder, "longitude");
         json_builder_add_double_value (builder, gir->longitude);
-        json_builder_end_object (builder); /* end location object */
 
-        json_builder_set_member_name (builder, "address");
-        json_builder_begin_array (builder); /* begin address array */
+        const char *accuracy = "country";
 
-        /* Before adding any entry check if that's NULL.
-           If NULL then don't add it to the JSON output.
-        */
-        add_json_object_for_address (builder, gir->city, NULL, "city");
-        add_json_object_for_address (builder,
-                                     GeoIP_region_name_by_code (gir->country_code, gir->region),
-                                     gir->region,
-                                     "region");
-        add_json_object_for_address (builder, NULL, gir->postal_code, "postalcode");
-        add_json_object_for_address (builder, gir->country_name, gir->country_code, "country");
+        /* Country level information */
+        if (gir->country_name != NULL) {
+                json_builder_set_member_name (builder, "country_name");
+                json_builder_add_string_value (builder, gir->country_name);
+        }
+        if (gir->country_code != NULL) {
+                json_builder_set_member_name (builder, "country_code");
+                json_builder_add_string_value (builder, gir->country_code);
+
+                /* Region level information */
+                if (gir->region != NULL) {
+                    accuracy = "region";
+                    json_builder_set_member_name (builder, "region");
+                    json_builder_add_string_value (builder,
+                                                   GeoIP_region_name_by_code (gir->country_code, 
gir->region));
+                }
+        }
+        if (gir->area_code > 0) {
+                json_builder_set_member_name (builder, "areacode");
+                json_builder_add_int_value (builder, gir->area_code);
+        }
 
-        json_builder_end_array (builder); /* end address array  */
+        /* City level information */
+        if (gir->city != NULL) {
+                accuracy = "city";
+                json_builder_set_member_name (builder, "city");
+                json_builder_add_string_value (builder, gir->city);
+        }
+        if (gir->postal_code != NULL) {
+                json_builder_set_member_name (builder, "zipcode");
+                json_builder_add_string_value (builder, gir->postal_code);
+        }
+        if (gir->metro_code > 0) {
+                json_builder_set_member_name (builder, "metro_code");
+                json_builder_add_int_value (builder, gir->metro_code);
+        }
+
+        json_builder_set_member_name (builder, "accuracy");
+        json_builder_add_string_value (builder, accuracy);
 
         timezone = GeoIP_time_zone_by_country_and_region(gir->country_code, gir->region);
         if (timezone) {
@@ -109,21 +105,9 @@ add_result_attr_to_json_tree (const char *ipaddress,
                 json_builder_add_string_value (builder, timezone);
         }
 
-        json_builder_set_member_name (builder, "accuracy");
-        json_builder_add_string_value (builder, "city");
-
         json_builder_set_member_name (builder, "attribution");
         json_builder_add_string_value (builder, attribution_text);
 
-        json_builder_end_object (builder); /* end ipaddress object */
-
-        json_builder_end_array (builder); /* end ipaddress array */
-
-        json_builder_end_object (builder); /* end results object */
-
-        json_builder_set_member_name (builder, "status");
-        json_builder_add_string_value (builder, "OK");
-
         json_builder_end_object (builder); /* end */
 
         GeoIPRecord_delete (gir);
@@ -151,25 +135,13 @@ add_result_attr_to_json_tree_geoipdb (const char *ipaddress,
 
         json_builder_begin_object (builder); /* begin */
 
-        json_builder_set_member_name (builder, "results");
-        json_builder_begin_object (builder); /* begin results object */
+        json_builder_set_member_name (builder, "ip");
+        json_builder_add_string_value (builder, ipaddress);
 
-        json_builder_set_member_name (builder, ipaddress);
-        json_builder_begin_array (builder); /* begin ipaddress array */
-        json_builder_begin_object (builder); /* begin ipaddress object */
-
-        json_builder_set_member_name (builder, "address");
-        json_builder_begin_array (builder); /* begin address array */
-
-        /* Before adding any entry check if that's NULL.
-           If NULL then don't add it to the JSON output.
-        */
-        add_json_object_for_address (builder,
-                                     GeoIP_country_name_by_addr (gi, ipaddress),
-                                     GeoIP_country_code_by_addr (gi, ipaddress),
-                                     "country");
-
-        json_builder_end_array (builder); /* end address array  */
+        json_builder_set_member_name (builder, "country_code");
+        json_builder_add_string_value (builder, country_code);
+        json_builder_set_member_name (builder, "country_name");
+        json_builder_add_string_value (builder, country_name);
 
         json_builder_set_member_name (builder, "accuracy");
         json_builder_add_string_value (builder, "country");
@@ -177,15 +149,6 @@ add_result_attr_to_json_tree_geoipdb (const char *ipaddress,
         json_builder_set_member_name (builder, "attribution");
         json_builder_add_string_value (builder, attribution_text);
 
-        json_builder_end_object (builder); /* end ipaddress object */
-
-        json_builder_end_array (builder); /* end ipaddress array */
-
-        json_builder_end_object (builder); /* end results object */
-
-        json_builder_set_member_name (builder, "status");
-        json_builder_add_string_value (builder, "OK");
-
         json_builder_end_object (builder); /* end */
 
         return builder;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]