B
B
beduin012018-04-02 17:34:34
Python
beduin01, 2018-04-02 17:34:34

What is the best way to rework the validation and response function?

I'm using Falcon. Tell me how best to rework the response function. The current implementation clearly does not suit me. I see at least one obvious jamb. If we caught an error, then it does not answer anything:

class MyAPI():
  def on_post(self, req, resp):
    request_json = None
    try:
      request_json = json.loads(req.stream.read().decode('utf-8')) # do not needed in Python >=3.6
    except Exception as e:
      log.critical("JSONDecodeError: {0} Exception: {1}".format(req.stream.read().decode('utf-8'), e))
      
    ready_query = make_query(request_json) 
    resp.body = get_list_of_transactions_from_db(ready_query, request_json)

Is the part worth it at all?
ready_query = make_query(request_json) 
    resp.body = get_list_of_transactions_from_db(ready_query, request_json)

put in a try block?
But is this a good one?
class MyAPI():
  def on_post(self, req, resp):
    request_json = None
    ready_query = None
    error_msg = dict()
    try:
      request_json = json.loads(req.stream.read().decode('utf-8')) # do not needed in Python >=3.6
      ready_query = make_query(request_json) 
      resp.body = get_list_of_transactions_from_db(ready_query, request_json)
    except Exception as e:
      log.critical("Input JSONDecodeError: {0} Exception: {1}".format(req.stream.read().decode('utf-8'), e))
      error_msg['error'] = 'JSONDecodeError'
      resp.body = json.dumps(error_msg)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question