def _log(self, key, data, dtype, options: LogOptions = None): if self.local_server: self.local_server.log(key, data, dtype, options) else: # todo: make the json serialization more robust. Not priority b/c this' client-side. json = LogEntry(key, serialize(data), dtype, options)._asdict() self.session.post(self.url, json=json)
async def log_handler(self, req): import sanic from collections import Sequence if req.files: file, = req.files['file'] print(f"uploading: {file.name} len: {len(file.body)}") self.log(file.name, file.body, "byte", LogOptions(overwrite=True)) return sanic.response.json(dict(name=file.name, length=len(file.body), overwrite=True)) elif not req.json: cprint(f'request json is empty: {req.text}', 'red') return sanic.response.text("Request json is empty") log_entry = LogEntry(**req.json) print(f"writing: {log_entry.key} type: {log_entry.type} options: {log_entry.options}") data = deserialize(log_entry.data) if not log_entry: options = None elif log_entry.options is None: options = log_entry.options elif isinstance(log_entry.options, Sequence): options = LogOptions(*log_entry.options) elif isinstance(log_entry.options, dict): options = LogOptions(**log_entry.options) else: options = None self.log(log_entry.key, data, log_entry.type, options) return sanic.response.text('ok')
async def log_handler(self, req): import sanic if not req.json: print(f'request json is empty: {req.text}') return sanic.response.text("Reuqest json is empty") log_entry = LogEntry(**req.json) print("writing: {} type: {} options: {}".format(log_entry.key, log_entry.type, log_entry.options)) data = deserialize(log_entry.data) options = log_entry.options if log_entry.options is None else LogOptions(*log_entry.options) self.log(log_entry.key, data, log_entry.type, options) return sanic.response.text('ok')