def get_order_from_id(self, id): """ Returns the order with the given id. @type id: str @param id: The id of the order. @rtype: Order @return: The order if it exists, None otherwise. """ args = 'id=%d' % id url = self.address + '/order/get/?' + args result = self.opener.open(url) if result.getcode() != 200: raise Exception(response) return Order.from_xml(result.read())
def get_response(self): data = parse_qs(self.data) logger = self.daemon.logger order_db = self.daemon.parent.get_order_db() if self.path == '/order/': logger.debug('Parsing order from HTTP request.') order = Order.from_xml(data['xml'][0]) logger.debug('XML order parsed complete.') self.daemon.order_incoming_event(order) return 'application/json', json.dumps(order.get_id()) elif self.path == '/order/get/': id = int(self.args.get('id')) order = order_db.get_order(id = id) return order.toxml() elif self.path == '/order/count/': return str(order_db.count_orders()) elif self.path == '/order/status/': order_id = int(self.args['id']) order = order_db.get_order(id = order_id) progress = order_db.get_order_progress_from_id(order_id) if not order: raise Exception('no such order id') closed = order.get_closed_timestamp() if closed is not None: closed = str(closed) response = {'status': order.get_status(), 'progress': progress, 'closed': closed} return 'application/json', json.dumps(response) elif self.path == '/order/list/': # Fetch the orders. offset = int(self.args.get('offset', 0)) limit = min(100, int(self.args.get('limit', 100))) orders = order_db.get_orders(offset = offset, limit = limit) # Assemble an XML document containing the orders. xml = etree.Element('xml') for order in orders: xml.append(order.toetree()) return etree.tostring(xml, pretty_print = True) elif self.path == '/task/get/': id = int(self.args.get('id')) task = order_db.get_task(id = id) return task.toxml() elif self.path == '/task/count/': order_id = self.args.get('order_id') if order_id: n_tasks = order_db.count_tasks(order_id = int(order_id)) else: n_tasks = order_db.count_tasks() return 'application/json', json.dumps(n_tasks) elif self.path == '/task/list/': # Fetch the tasks. order_id = int(self.args.get('order_id')) offset = int(self.args.get('offset', 0)) limit = min(100, int(self.args.get('limit', 100))) tasks = order_db.get_tasks(order_id = order_id, offset = offset, limit = limit) # Assemble an XML document containing the orders. xml = etree.Element('xml') for task in tasks: xml.append(task.toetree()) return etree.tostring(xml, pretty_print = True) elif self.path == '/log/': task_id = int(self.args.get('task_id')) task = order_db.get_task(id = task_id) filename = task.get_logfile() if filename and os.path.isfile(filename): with open(filename) as file: return file.read() else: return '' elif self.path == '/trace/': task_id = int(self.args.get('task_id')) task = order_db.get_task(id = task_id) filename = task.get_tracefile() if filename and os.path.isfile(filename): with open(filename) as file: return file.read() else: return '' else: raise Exception('no such API call')
def get_response(self): data = parse_qs(self.data) logger = self.daemon.logger order_db = self.daemon.parent.get_order_db() if self.path == '/order/': logger.debug('Parsing order from HTTP request.') order = Order.from_xml(data['xml'][0]) logger.debug('XML order parsed complete.') self.daemon.order_incoming_event(order) return 'application/json', json.dumps(order.get_id()) elif self.path == '/order/get/': id = int(self.args.get('id')) order = order_db.get_order(id = id) return order.toxml() elif self.path == '/order/count/': order_id = self.args.get('order_id') service = self.args.get('service') descr = self.args.get('description') status = self.args.get('status') created_by = self.args.get('created_by') return str(order_db.count_orders(id = order_id, service = service, description = descr, status = status, created_by = created_by)) elif self.path == '/order/status/': order_id = int(self.args['id']) order = order_db.get_order(id = order_id) progress = order_db.get_order_progress_from_id(order_id) if not order: raise Exception('no such order id') closed = order.get_closed_timestamp() if closed is not None: closed = str(closed) response = {'status': order.get_status(), 'progress': progress, 'closed': closed} return 'application/json', json.dumps(response) elif self.path == '/order/list/': # Fetch the orders. offset = int(self.args.get('offset', 0)) limit = min(100, int(self.args.get('limit', 100))) order_id = self.args.get('order_id') service = self.args.get('service') descr = self.args.get('description') status = self.args.get('status') created_by = self.args.get('created_by') orders = order_db.get_orders(id = order_id, service = service, description = descr, status = status, created_by = created_by, offset = offset, limit = limit) # Assemble an XML document containing the orders. xml = etree.Element('xml') for order in orders: xml.append(order.toetree()) return etree.tostring(xml, pretty_print = True) elif self.path == '/task/get/': id = int(self.args.get('id')) task = order_db.get_task(id = id) return task.toxml() elif self.path == '/task/count/': order_id = self.args.get('order_id') if order_id: n_tasks = order_db.count_tasks(order_id = int(order_id)) else: n_tasks = order_db.count_tasks() return 'application/json', json.dumps(n_tasks) elif self.path == '/task/list/': # Fetch the tasks. order_id = int(self.args.get('order_id')) offset = int(self.args.get('offset', 0)) limit = min(100, int(self.args.get('limit', 100))) tasks = order_db.get_tasks(order_id = order_id, offset = offset, limit = limit) # Assemble an XML document containing the orders. xml = etree.Element('xml') for task in tasks: xml.append(task.toetree()) return etree.tostring(xml, pretty_print = True) elif self.path == '/log/': task_id = int(self.args.get('task_id')) task = order_db.get_task(id = task_id) filename = task.get_logfile() if filename and os.path.isfile(filename): with open(filename) as file: return file.read() else: return '' elif self.path == '/trace/': task_id = int(self.args.get('task_id')) task = order_db.get_task(id = task_id) filename = task.get_tracefile() if filename and os.path.isfile(filename): with open(filename) as file: return file.read() else: return '' else: raise Exception('no such API call')