def set_raw_by_ticketkey_host(): # # [Validation] # 1. must be content-type is applicaiton/json # 2. JSON must be parsed successfully # 3. JSON must has TicketKey # 4. save to DB # # 1. if request.headers['Content-Type'] != 'application/json': return response.bad_request("please send application/json") # 2. # http://flask.pocoo.org/docs/0.10/api/#flask.Request.get_json try: data = request.get_json() except Exception as ex: #criteria_blueprint.loo return response.bad_request("invalid JSON format") # 3. required_key = ['TicketKey', 'Host'] for key in required_key: if key not in data: return response.bad_request("{key} is not found".format(key=key)) # 4. dao = DataTable(region_name=current_app.config['DYNAMODB_REGION'], table_name=current_app.config['CRITERIA_TABLE'], logger=current_app.logger) result = dao.save(data) return response.created()
def test_save(self): self._create_table() table = DataTable(region_name=self.region_name, table_name=self.table_name) self.assertIsNone(table.find_all()) self._stuff_test_items(table, item_cnt=3) self.assertEqual(3, len(table.find_all()))
def test_find_all(self): self._create_table() table = DataTable(region_name=self.region_name, table_name=self.table_name) self.assertIsNone(table.find_all()) self._stuff_test_items(table) self.assertIsNotNone(table.find_all()) self.assertEqual(1, len(table.find_all()))
def get_criteria_by_ticketkey_host(ticket_key, host): criteria = DataTable(region_name=current_app.config['DYNAMODB_REGION'], table_name=current_app.config['CRITERIA_TABLE'], logger=current_app.logger) result = current_app.cache.get("criteria.ticket_key.%s.host.%s" % (ticket_key, host)) if result is None: result = criteria.find_by_ticketkey_host(ticket_key, host) current_app.cache.set("criteria.ticket_key.%s.host.%s" % (ticket_key, host), result) return response.get_json(result)
def get_all_criteria(): criteria = DataTable(region_name=current_app.config['DYNAMODB_REGION'], table_name=current_app.config['CRITERIA_TABLE'], logger=current_app.logger) result = current_app.cache.get('criteria.all') if result is None: result = criteria.find_all() current_app.cache.set('criteria.all', result) return response.get_json(result)
def set_criteria_by_ticketkey_host(): # # [Validation] # 1. must be content-type is applicaiton/json # 2. JSON must be parsed successfully # 3. JSON must has TicketKey and Host # 4. save to DB # 5. purge cache # 6. enqueue # # 1. if request.headers['Content-Type'] != 'application/json': current_app.logger.warn("received Content-Type %s" % request.headers['Content-Type']) return response.bad_request("please send application/json") # 2. # http://flask.pocoo.org/docs/0.10/api/#flask.Request.get_json try: data = request.get_json() except Exception as ex: current_app.logger.warn("couldn't parse JSON %s" % ex) return response.bad_request("invalid JSON format") # 3. required_key = ['TicketKey', 'Host'] for key in required_key: if key not in data: return response.bad_request("{key} is not found".format(key=key)) # 4. table = DataTable(region_name=current_app.config['DYNAMODB_REGION'], table_name=current_app.config['CRITERIA_TABLE'], logger=current_app.logger) result = table.save(data) # 5. current_app.cache.delete('criteria.all') current_app.cache.delete("criteria.ticket_key.%s" % data['TicketKey']) current_app.cache.delete("criteria.ticket_key.%s.host.%s" % (data['TicketKey'], data['Host'])) # 6. queue = Queue(region_name=current_app.config['SQS_REGIOM'], queue_name=current_app.config['SQS_NAME'], logger=current_app.logger) queue.push({key: data[key] for key in required_key}) return response.created()
def set_criteria_by_ticketkey_host(ticket_key, host): # # [Validation] # 1. must be content-type is applicaiton/json # 2. JSON must be parsed successfully # 3. validate JSON string # 4. save to DB # 5. purge cache # 6. enqueue # # 1. if request.headers['Content-Type'] != 'application/json': current_app.logger.warn("received Content-Type %s" % request.headers['Content-Type']) return response.bad_request("please send application/json") # 2. # http://flask.pocoo.org/docs/0.10/api/#flask.Request.get_json try: data = request.get_json() except Exception as ex: current_app.logger.warn("couldn't parse JSON %s" % ex) return response.bad_request("invalid JSON format") # 3. try: criteria = CriteriaData(**data).get_json() except Exception as ex: current_app.logger.error(ex) return response.bad_request(ex.message) # 4. table = DataTable(region_name=current_app.config['DYNAMODB_REGION'], table_name=current_app.config['CRITERIA_TABLE'], logger=current_app.logger) result = table.save(criteria, TicketKey=ticket_key, Host=host) # 5. cache.delete_memoized(get_all_criteria) cache.delete_memoized(get_criteria_by_ticketkey, ticket_key) cache.delete_memoized(get_criteria_by_ticketkey_host, ticket_key, host) # 6. queue = Queue(region_name=current_app.config['SQS_REGION'], queue_name=current_app.config['SQS_NAME'], logger=current_app.logger) queue.push({'TicketKey': ticket_key, 'Host': host}) return response.created()
def set_result_by_ticketkey_host(ticket_key, host): # # [Validation] # 1. must be content-type is applicaiton/json # 2. JSON must be parsed successfully # 3. validate JSON string # 4. save to DB # 5. purge cache # # 1. if request.headers['Content-Type'] != 'application/json': return response.bad_request("please send application/json") # 2. # http://flask.pocoo.org/docs/0.10/api/#flask.Request.get_json try: data = request.get_json() except Exception as ex: current_app.logger.error(ex) return response.bad_request("invalid JSON format") # 3. try: result = ResultData(**data).get_json() except Exception as ex: current_app.logger.error(ex) return response.bad_request(ex.message) # 4. dao = DataTable(region_name=current_app.config['DYNAMODB_REGION'], table_name=current_app.config['RESULT_TABLE'], logger=current_app.logger) dao.save(result, TicketKey=ticket_key, Host=host) # 5. cache.delete_memoized(get_all_result) cache.delete_memoized(get_result_by_ticketkey, ticket_key) cache.delete_memoized(get_result_by_ticketkey_host, ticket_key, host) return response.created()
def get_result_by_ticketkey_host(ticket_key, host): raw = DataTable(region_name=current_app.config['DYNAMODB_REGION'], table_name=current_app.config['RESULT_TABLE'], logger=current_app.logger) result = raw.find_by_ticketkey_host(ticket_key, host) return response.get_json(result)
def get_all_result(): raw = DataTable(region_name=current_app.config['DYNAMODB_REGION'], table_name=current_app.config['RESULT_TABLE'], logger=current_app.logger) result = raw.find_all() return response.get_json(result)
def get_criteria_by_ticketkey_host(ticket_key, host): criteria = DataTable(region_name=current_app.config['DYNAMODB_REGION'], table_name=current_app.config['CRITERIA_TABLE'], logger=current_app.logger) result = criteria.find_by_ticketkey_host(ticket_key, host) return response.get_json(result)