So I'm working with an an API that returns a JSON after I send a query to it. I wrote a wrapper class around that JSON response so I could handle any change in the JSON in one place. It also helps that I can access the values easily now.
Here is the wrapper I wrote:
class WitResponse:
def __init__(self, json_string):
self.json_string = json_string
self.wit_response = json.loads(json_string)
@property
def intent(self):
return self.wit_response["outcomes"][0]["intent"]
@property
def confidence(self):
return self.wit_response["outcomes"][0]["confidence"]
@property
def text(self):
return self.wit_response["_text"]
@property
def entites(self):
return self.wit_response["outcomes"][0]["entites"]
@property
def msg_id(self):
return self.wit_response["msg_id"]
@property
def response(self):
return self.json_string.translate(None,"\n")+"\n" #Saves the reply in an already easy to send format
@property
def target(self):
return self.wit_response["outcomes"][0]["entities"]["target"][0]["value"]
An example of a very frequent key that can be found in the JSON is the target
key (as it can be seen above). Since it doesn't always exist calling the target
method will raise an exception. I'm looking for a solution to handle fields like that that'll be as elegant as possible.
At the moment I've just wrapped the the return statement in the target
method with a try,except block and returns None
if an exception is raised (the JSON didn't have a target
key).
I've also considered using a decorator that adds the try,except block however it didn't seem to work when I added the decorator above the @property decorator:
def None_Return_Value_Decorator(func):
def wrapper(self):
try:
value = func(self)
return value
except:
return None
return wrapper
Are there more elegant solutions? If not, can the decorator one work (and how do I fix it if it does)?