def __reqeust(self, method, url, **kwargs): """ 网路请求函数模板,对返回结果做统一校验 Args: method str 请求 method url str 请求 url/path **kwargs 传给 requests.request 函数 Returns: respJson jsonData json 数据 Raises: JoyrunRequestStatusError 请求状态码异常 JoyrunRetStateError ret 字段非 0 -> 请求结果异常 JoyrunSidInvalidError sid 失效,登录状态异常 """ if url[:7] != "http://" and url[:8] != "https://": url = "{base}/{path}".format( base=self.BaseUrl, path=url[1:] if url[0] == '/' else url) # //user/login/normal 需要保留两个 '/' ! resp = self.session.request(method, url, **kwargs) # self.logger.debug("request.url = %s" % resp.url) # self.logger.debug("request.headers = %s" % resp.request.headers) # self.logger.debug("request.body = %s" % resp.request.body) # self.logger.debug("response.headers = %s" % resp.headers) if not resp.ok: self.logger.error("request.url = %s" % resp.url) self.logger.error("request.headers = %s" % pretty_json(dict(resp.request.headers))) self.logger.error("session.cookies = %s" % pretty_json(self.session.cookies.get_dict())) if resp.request.method == 'POST': self.logger.error("request.body = %s" % resp.request.body) self.logger.error("response.text = %s" % resp.text) raise JoyrunRequestStatusError("response.ok error") respJson = resp.json() if respJson.get("ret") != "0": if respJson.get("ret") == "401": # sid 失效 raise JoyrunSidInvalidError("sid invalid") else: self.logger.error("response.json = %s" % pretty_json(respJson)) raise JoyrunRetStateError("response.json error") self.logger.debug("request.url = %s" % resp.url) self.logger.debug("response.json = %s" % pretty_json(respJson)) return respJson
def __request(self, method, url, verify_success=True, **kwargs): """ 网路请求函数模板,对返回结果做统一校验 Args: method str 请求 method url str 请求 url/path verify_success bool 是否校验 success 字段 (默认 true),对于无 success 返回值的请求不需要校验 **kwargs 传给 requests.request 函数 Returns: respJson jsonData json 数据 Raises: PKURunnerRequestStatusError 请求状态码异常 PKURunnerUnauthorizedError 鉴权失败 PKURunnerSuccessStateError success 状态为 false """ if url[:7] != "http://" and url[:8] != "https://": url = "{base}/{path}".format(base=self.BaseURL, path=url.lstrip("/")) headers = self.headers headers.update(kwargs.pop("headers", {})) resp = requests.request(method, url, headers=headers, **kwargs) if not resp.ok: if resp.text == "Unauthorized": raise PKURunnerUnauthorizedError("token invalid") else: self.logger.error("resp.headers = %s" % pretty_json(dict(resp.headers))) self.logger.error("resp.text = %s" % resp.text) raise PKURunnerRequestStatusError("resp.ok error") else: respJson = resp.json() if verify_success and not respJson.get("success"): self.logger.error(respJson) raise PKURunnerSuccessStateError("resp.json error") else: self.logger.debug( "resp.url = {url} \nresp.json = {json}".format( url=resp.url, json=pretty_json(respJson))) return respJson
def better_jsonify(obj): return application.response_class( pretty_json(obj), mimetype=application.config["JSONIFY_MIMETYPE"])
def gen(): yield b'Pulling image. Please wait! (streaming)<br/>' for line in doc.pull(image, stream=True): yield '{}<br/>'.format(pretty_json(line.decode('utf8')))
"--check", help="show 'config.ini' file", action="store_false") parser.add_option("-s", "--start", help="start uploading job with %s Client" % app, action="store_false") options, args = parser.parse_args() if options.check is not None: print("# -- Using %s Client [%s] -- #" % (app, __date__)) for section in config.sections(): if section in ["Base", app]: print("# -- Section [%s] -- #" % section) print(pretty_json(dict(config[section]))) elif options.start is not None: try: logger.info("Running %s Client [%s]" % (app, __date__)) client = Client() client.run() except Exception as err: logger.error("upload record failed !") raise err else: logger.info("upload record success !")
def gen(): yield b'Pulling image. Please wait! (streaming)\n' for line in doc.pull(config.repo, stream=True): yield pretty_json(line)
def gen(): yield b"Pulling image. Please wait! (streaming)<br/>" for line in doc.pull(image, stream=True): yield "{}<br/>".format(pretty_json(line.decode("utf8")))