Settled how to report non-existing values in reader protocol.
authorStanislaw Klekot <dozzie@jarowit.net>
Mon, 2 Jan 2017 21:21:48 +0000 (22:21 +0100)
committerStanislaw Klekot <dozzie@jarowit.net>
Mon, 2 Jan 2017 21:21:48 +0000 (22:21 +0100)
Instead of HTTP/404, it's better to return empty string/`null', in similar way
to reporting non-existing value groups and origins.

bin/statetip
doc/manpages/protocols.rst
pylib/statetip/__init__.py
src/statip_reader_client.erl

index 63c7001..1596ba8 100755 (executable)
@@ -289,18 +289,19 @@ else: # MODE_READER
                 origin = None
 
             value = reader.get(name, origin, key)
-            if options.print_json:
-                mode = "json"
-            else:
-                mode = "string"
-
-            for field in options.print_fields:
-                if field == "all":
-                    dump(value, mode = "json")
-                elif field == "info":
-                    dump(value["info"], mode = "json")
-                elif field == "state" or field == "severity":
-                    dump(value[field], mode)
+            if value is not None:
+                if options.print_json:
+                    mode = "json"
+                else:
+                    mode = "string"
+
+                for field in options.print_fields:
+                    if field == "all":
+                        dump(value, mode = "json")
+                    elif field == "info":
+                        dump(value["info"], mode = "json")
+                    elif field == "state" or field == "severity":
+                        dump(value[field], mode)
     except KeyboardInterrupt:
         pass
     except statetip.StateTipException, e:
index 2de1b99..8057923 100644 (file)
@@ -121,6 +121,10 @@ contain all the other keys. Example (wrapped for readability):
     {"name": "interfaces", "origin": "web01", "key": "vlan512",
       "state": "up", "severity": "expected", "info": {"device": "eth0"}}
 
+If requested value doesn't exist, it's either reported as ``null``
+(``/json/<name>/<origin>/<key>`` path) or empty string
+(``/list/<name>/<origin>/<key>`` path).
+
 Beside ``/list/...`` and ``/json/...`` prefixes, there are two more paths
 available:
 
index 0f6e3b5..322cefb 100644 (file)
@@ -275,12 +275,12 @@ class StateTipReader:
         :type origin: string or ``None``
         :param key: value's key
         :type key: string
-        :return: dictionary
+        :return: dictionary or ``None``
         :throws: :exc:`AddressError`, :exc:`NetworkError`, :exc:`Timeout`,
             :exc:`ValueError`
 
-        Retrieve a specific value. Returned value will similar to the
-        following dictionary:
+        Retrieve a specific value (or ``None`` if value doesn't exist).
+        Returned value will similar to the following dictionary:
 
         .. code-block:: python
 
index 16d7ff4..a365c8f 100644 (file)
@@ -257,10 +257,14 @@ process_request(_State = #state{path = Path, headers = _Headers}) ->
       case statip_value:get_value(Name, Origin, Key) of
         Value = #value{} ->
           {ok, JSON} = statip_value:to_json(Name, Origin, Value),
-          {ok, [content_type(Type)], [JSON, $\n]};
-        none ->
-          {error, 404}
-      end;
+          Reply = [JSON, $\n];
+        none when Type == json ->
+          {ok, JSON} = statip_json:encode(null),
+          Reply = [JSON, $\n];
+        none when Type == list ->
+          Reply = ""
+      end,
+      {ok, [content_type(Type)], Reply};
     {error, _Reason} ->
       {error, 404}
   end.