Browse Source

Better handling of JSON requests

tags/nilmdb-1.2.4^0
Jim Paris 11 years ago
parent
commit
fe91ff59a3
2 changed files with 24 additions and 12 deletions
  1. +7
    -3
      nilmdb/client/client.py
  2. +17
    -9
      nilmdb/server/server.py

+ 7
- 3
nilmdb/client/client.py View File

@@ -26,6 +26,7 @@ class Client(object):
POST requests are sent with Content-Type 'application/json'
instead of the default 'x-www-form-urlencoded'."""
self.http = nilmdb.client.httpclient.HTTPClient(url, post_json)
self.post_json = post_json

# __enter__/__exit__ allow this class to be a context manager
def __enter__(self):
@@ -34,8 +35,11 @@ class Client(object):
def __exit__(self, exc_type, exc_value, traceback):
self.close()

def _json_param(self, data):
def _json_post_param(self, data):
"""Return compact json-encoded version of parameter"""
if self.post_json:
# If we're posting as JSON, we don't need to encode it further here
return data
return json.dumps(data, separators=(',',':'))

def close(self):
@@ -76,7 +80,7 @@ class Client(object):
metadata."""
params = {
"path": path,
"data": self._json_param(data)
"data": self._json_post_param(data)
}
return self.http.post("stream/set_metadata", params)

@@ -84,7 +88,7 @@ class Client(object):
"""Update stream metadata from a dictionary"""
params = {
"path": path,
"data": self._json_param(data)
"data": self._json_post_param(data)
}
return self.http.post("stream/update_metadata", params)



+ 17
- 9
nilmdb/server/server.py View File

@@ -231,26 +231,34 @@ class Stream(NilmApp):
@cherrypy.expose
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
@exception_to_httperror(NilmDBError, LookupError, TypeError)
@exception_to_httperror(NilmDBError, LookupError)
@cherrypy.tools.CORS_allow(methods = ["POST"])
def set_metadata(self, path, data):
"""Set metadata for the named stream, replacing any
existing metadata. Data should be a json-encoded
dictionary"""
data_dict = json.loads(data)
self.db.stream_set_metadata(path, data_dict)
"""Set metadata for the named stream, replacing any existing
metadata. Data can be json-encoded or a plain dictionary (if
it was sent as application/json to begin with)"""
if not isinstance(data, dict):
try:
data = dict(json.loads(data))
except TypeError as e:
raise NilmDBError("can't parse 'data' parameter: " + e.message)
self.db.stream_set_metadata(path, data)

# /stream/update_metadata?path=/newton/prep&data=<json>
@cherrypy.expose
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
@exception_to_httperror(NilmDBError, LookupError, TypeError)
@exception_to_httperror(NilmDBError, LookupError)
@cherrypy.tools.CORS_allow(methods = ["POST"])
def update_metadata(self, path, data):
"""Update metadata for the named stream. Data
should be a json-encoded dictionary"""
data_dict = json.loads(data)
self.db.stream_update_metadata(path, data_dict)
if not isinstance(data, dict):
try:
data = dict(json.loads(data))
except TypeError as e:
raise NilmDBError("can't parse 'data' parameter: " + e.message)
self.db.stream_update_metadata(path, data)

# /stream/insert?path=/newton/prep
@cherrypy.expose


Loading…
Cancel
Save