def set(self, res, method, url, *args, **kwargs): key = self.compute_key(method, url, *args, **kwargs) d = {} for field in self.RESPONSE_JSONABLE_FIELDS: d[field] = res.__dict__[field] d["_content"] = b64encode(d["_content"]) s = json_dumps(d) with open("{}/{}.json".format(self.REQUEST_CACHE_DIR, key), "w") as f: f.write(s)
def compute_key(cls, method, url, *args, **kwargs): filename, co_name, lineno = capture_caller_info() params = {"args": args, "kwargs": kwargs} params_str = json_dumps(str(params)) m = md5() m.update(params_str.encode("utf-8")) s = "{}::{}::{}__{}__{}__{}".format(filename, co_name, 0, method, url, m.hexdigest()) s = s.replace("/", ".").replace(":", ".").replace("?", ".") return s
def dumps(self, obj): options = {"cls": self.encoder} if self.encoder else {} return json_dumps(obj, **options)
def get_human_token(self): assert isinstance(self, Human), "Human 인스턴스여야 합니다." mjson_str = json_dumps(self.mjson) return encrypt(settings.HUMAN_TOKEN_KEY, mjson_str)
def dumps(self, obj): options = {"cls": self.encoder} if self.encoder else {} options["ensure_ascii"] = True return json_dumps(obj, **options)