async def retrieve_workflow(storeId): if not (storeId in workflows): logger.info('Workflow not registered to stock-analyzer\n') return Response( status=404, response="Workflow doesn't exist. Nothing to retrieve\n" ) else: logger.info('{} Workflow found on stock-analyzer\n'.format(storeId)) return Response( status=200, response=json.dumps(workflows[storeId]) + '\n' )
async def test_make_response_response(app: Quart) -> None: response = await app.make_response(Response('Result')) assert response.status_code == 200 assert (await response.get_data()) == b'Result' response = await app.make_response((Response('Result'), {'name': 'value'})) assert response.status_code == 200 assert (await response.get_data()) == b'Result' assert response.headers['name'] == 'value' response = await app.make_response((Response('Result'), 404, {'name': 'value'})) assert response.status_code == 404 assert (await response.get_data()) == b'Result' assert response.headers['name'] == 'value'
async def command(): c = request.args.get('command') auth = request.headers.get('Authorization', '').replace('Bearer ', '') try: t_jwt = jwt.decode( auth, twitch_ext_secret, algorithms=['HS256'], options={"require": ["channel_id", "opaque_user_id", "role"]}) except (InvalidSignatureError, ExpiredSignatureError, InvalidTokenError, MissingRequiredClaimError): response = Response('') response.status_code = 403 response.headers['Access-Control-Allow-Origin'] = '*' return response if t_jwt: if t_jwt.get('opaque_user_id', '')[0] == 'U': j = json.dumps({ 'e': 'BUTTON_COMMAND', 'd': { 'button': { 'command': c, }, 'user': { 'username': '******', }, }, }) else: app.logger.debug("JWT: " + str(t_jwt)) response = Response('') response.status_code = 403 response.headers['Access-Control-Allow-Origin'] = '*' return response else: app.logger.debug("JWT: " + str(t_jwt)) response = Response('') response.status_code = 403 response.headers['Access-Control-Allow-Origin'] = '*' return response await broadcast( j ) # after the websockets context issue is solved change this to send message only to target robot response = Response('OK') response.headers['Access-Control-Allow-Origin'] = '*' return response
def create_http_wire_response( request: Request, query_response: Message ) -> Response: logger = logging.getLogger("doh-server") logger.debug( "[HTTP] " + str(request.method) + " " + str(request.headers.get("Accept")) ) if isinstance(query_response, Message): query_response.id = 0 body = query_response.to_wire() response = Response(body, content_type=DOH_CONTENT_TYPE) response.headers["content-length"] = str(len(body)) return set_headers(request, response, query_response) else: return Response(query_response)
async def retrieve_workflow(storeId): '''REST API for requesting details of registered store''' if not (storeId in workflows): logger.info('Workflow not registered to delivery-assigner\n') return Response( status=404, response="Workflow doesn't exist. Nothing to retrieve\n" ) else: logger.info('{} Workflow found on delivery-assigner\n'.format(storeId)) return Response( status=200, response=json.dumps(workflows[storeId]) )
async def get_metrics(): #log.info(request.url) target = request.args.get('target') monitor_data = Perfdata(monitorconnection.MonitorConfig(), target) # Fetch performance data from Monitor start_time = time.monotonic() try: loop = asyncio.get_event_loop() fetch_perfdata_task = loop.create_task( monitor_data.get_service_metrics()) if monitorconnection.MonitorConfig().get_enable_scrape_metadata(): fetch_metadata_task = loop.create_task( monitor_data.get_host_metrics()) await fetch_metadata_task await fetch_perfdata_task scrape_duration = time.monotonic() - start_time monitor_data.add_perfdata( "scrape_duration_seconds", { 'hostname': target, 'server': monitorconnection.MonitorConfig().get_url() }, scrape_duration) log.info("scrape", { 'target': target, 'url': request.url, 'scrape_time': scrape_duration }) target_metrics = monitor_data.prometheus_format() resp = Response(target_metrics) resp.headers['Content-Type'] = CONTENT_TYPE_LATEST # after_request_func(resp) return resp except monitorconnection.ScrapeExecption as err: log.warn( f"{err.message}", { 'target': target, 'url': request.url, 'remote_url': err.url, 'err': err.err }) resp = Response("") resp.status_code = 500 return resp
async def key_exchange(self, GUID): try: data = await request.data pub_key = self.dispatch_event(Events.KEX, (GUID, request.remote_addr, data)) return Response(pub_key, content_type='application/octet-stream') except IPCException: return '', 400
async def html2img(): call_args = await request.form url = call_args.pop('url') img_format = call_args.get('type', 'png') async with visit_page(url) as page: pdf = await page.screenshot(**call_args) return Response(pdf, mimetype=('image/' + img_format))
async def get_anime_detail(token): """返回番剧详情页面信息""" detail = await self._agent.get_anime_detail(token) if not detail: return Response("Parse detail failed", status=404) ret = { "title": detail.title, "cover_url": f"{detail.cover_url}", "description": detail.desc, "category": detail.category, "module": detail.module, "play_lists": [] } for idx, playlist in enumerate(detail): lst = { "name": playlist.name, "num": playlist.num, "video_list": [] } # 一个播放列表 for episode, video in enumerate(playlist): video_path = f"{token}/{idx}/{episode}" lst["video_list"].append({ "name": video.name, "info": f"{self._domain}/anime/{video_path}", "player": f"{self._domain}/anime/{video_path}/player", }) ret["play_lists"].append(lst) return jsonify(ret)
async def space_checkout(): request_body = await request.get_data() request_payload = json.loads(request_body) await worker_input_queue.add_task_with_result_future( partial(aggregator.user_left_space, request_payload['user_id']), request.logger) return Response('Ok', mimetype='text/plain')
async def test_make_response_response(app: Quart) -> None: response = await app.make_response(Response("Result")) assert response.status_code == 200 assert (await response.get_data()) == b"Result" # type: ignore response = await app.make_response((Response("Result"), {"name": "value"})) assert response.status_code == 200 assert (await response.get_data()) == b"Result" # type: ignore assert response.headers["name"] == "value" response = await app.make_response((Response("Result"), 404, { "name": "value" })) assert response.status_code == 404 assert (await response.get_data()) == b"Result" # type: ignore assert response.headers["name"] == "value"
async def api_pronounce() -> Union[Response, str]: """Pronounce CMU phonemes or word using eSpeak""" assert core is not None download = request.args.get("download", "false").lower() == "true" data = await request.data pronounce_str = data.decode().strip() assert pronounce_str, "No string to pronounce" # phonemes or word pronounce_type = request.args.get("type", "phonemes") if pronounce_type == "phonemes": # Convert from Sphinx to espeak phonemes phoneme_result = await core.get_word_phonemes(pronounce_str) espeak_str = phoneme_result.phonemes else: # Speak word directly espeak_str = pronounce_str speak_result = await core.speak_word(espeak_str) wav_data = speak_result.wav_data espeak_phonemes = speak_result.phonemes if download: # Return WAV return Response(wav_data, mimetype="audio/wav") # Play through speakers core.play_wav_data(wav_data) return espeak_phonemes
async def download_route(): path = request.args.get('path') assert path is not None f = BytesIO() assert await download_block(path, f) == 0 f.seek(0) return Response(f)
async def make_response_with_range(self, range_field: str = None) -> Response: """ 读取远程的视频流,并伪装成本地的响应返回给客户端, 206 连续请求会导致连接中断, asyncio 库在 Windows 平台触发 ConnectionAbortedError, 偶尔出现 LocalProtocolError, 是 RFC2616 与 RFC7231 HEAD 请求冲突导致, See: https://bugs.python.org/issue26509 https://gitlab.com/pgjones/quart/-/issues/45 """ url = self._info.real_url proxy_headers = self._get_proxy_headers(url) if range_field is not None: proxy_headers["range"] = range_field logger.debug(f"Client request stream range: {range_field}") await self.init_session() resp = await self.get(url, headers=proxy_headers) if not resp: return Response(b"", status=404) @stream_with_context async def stream_iter(): while chunk := await resp.content.read(4096): yield chunk
async def api_text_to_speech() -> Union[bytes, str]: """Speak a sentence with text to speech system.""" global last_sentence repeat = request.args.get("repeat", "false").strip().lower() == "true" play = request.args.get("play", "true").strip().lower() == "true" language = request.args.get("language") voice = request.args.get("voice") siteId = request.args.get("siteId") data = await request.data sentence = last_sentence if repeat else data.decode().strip() assert core is not None result = await core.speak_sentence(sentence, play=play, language=language, voice=voice, siteId=siteId) last_sentence = sentence if not play: # Return WAV data instead of speaking return Response(result.wav_data, mimetype="audio/wav") return sentence
async def signal_onboard(): request_body = await request.get_data() request_payload = json.loads(request_body) await worker_input_queue.add_task_with_result_future( partial(aggregator.onboard_new_signal_user, request_payload['user_id']), request.logger) return Response('Ok', mimetype='text/plain')
async def notification_test(): request_body = await request.get_data() request_payload = json.loads(request_body) await worker_input_queue.add_task_with_result_future( partial(aggregator.send_notification_test, request_payload['user_id']), request.logger) return Response('Ok', mimetype='text/plain')
async def _handle_http(self, adapter: str): request: Request = _request data: bytes = await request.get_data() # type: ignore if adapter not in self._adapters: logger.warning(f'Unknown adapter {adapter}. ' 'Please register the adapter before use.') raise exceptions.NotFound() BotClass = self._adapters[adapter] http_request = HTTPRequest(request.http_version, request.scheme, request.path, request.query_string, dict(request.headers), request.method, data) self_id, response = await BotClass.check_permission(self, http_request) if not self_id: raise exceptions.Unauthorized( description=(response and response.body or b"").decode()) if self_id in self._clients: logger.warning("There's already a reverse websocket connection," "so the event may be handled twice.") bot = BotClass(self_id, http_request) asyncio.create_task(bot.handle_message(data)) return Response(response and response.body or "", response and response.status or 200)
async def api_process(): """MaryTTS-compatible /process endpoint""" if request.method == "POST": data = parse_qs((await request.data).decode()) text = data.get("INPUT_TEXT", [""])[0] voice = data.get("VOICE", [""])[0] else: text = request.args.get("INPUT_TEXT", "") voice = request.args.get("VOICE", "") # <VOICE>;<VOCODER> vocoder: typing.Optional[str] = args.larynx_quality if ";" in voice: voice, vocoder = voice.split(";", maxsplit=1) wav_bytes = await text_to_wav( text, voice, vocoder=vocoder, denoiser_strength=args.larynx_denoiser_strength, noise_scale=args.larynx_noise_scale, length_scale=args.larynx_length_scale, ) return Response(wav_bytes, mimetype="audio/wav")
async def _handle_http_event(self) -> Response: if self._secret: if 'X-Signature' not in request.headers: self.logger.warning('signature header is missed') abort(401) sec = self._secret sec = sec.encode('utf-8') if isinstance(sec, str) else sec sig = hmac.new(sec, await request.get_data(), 'sha1').hexdigest() if request.headers['X-Signature'] != 'sha1=' + sig: self.logger.warning('signature header is invalid') abort(403) payload = await request.json if not isinstance(payload, dict): abort(400) if request.headers['X-Self-ID'] in self._wsr_api_clients: self.logger.warning( 'there is already a reverse websocket api connection, ' 'so the event may be handled twice.') response = await self._handle_event(payload) if isinstance(response, dict): return jsonify(response) return Response('', 204)
async def m3u8_chunk_proxy(token, playlist, episode, url): """代理访问视频的某一块数据""" proxy = await self._agent.get_anime_proxy(token, int(playlist), int(episode)) if not proxy: return Response("m3u8 chunk proxy error", status=404) return await proxy.make_response_for_chunk(url)
async def handle_500(err): from framework.objects import SETTINGS, jwt_service tb = "".join(format_exception(type(err), err, err.__traceback__)) token = request.headers.get("Authorization", request.cookies.get("token")) parsed = await jwt_service.verify_login_token(token, True) if not parsed: meta = {} else: meta = {"id": parsed["id"]} meta["time"] = datetime.utcnow() # Send traceback and metadata in base64 and encrypted. data = json.dumps({"tb": tb, "d": meta}, cls=DatetimeJSONEncoder).encode() c = AES.new(SETTINGS["AES_KEY"], AES.MODE_CTR) digest = base64.encodebytes(c.encrypt(data)) nonce = base64.b64encode(c.nonce) res = nonce + b"." + digest # TODO: Sentry error reporting integration return Response(res.decode(), 500)
def custom_response(res, status_code): """ Custom Response Function """ return Response(mimetype="application/json", response=json.dumps(res), status=status_code)
async def load_all_files(): "Loads all the files in the source directory" # if not (is_db_connection_available()): # return Response(('{"Error": "Could not connect to the database"}'), status=404, mimetype='application/json') list_of_files = [ f for f in os.listdir(Configuration.FILE_DIRECTORY) if f.endswith(Configuration.INPUT_FILE_EXTENSION) ] batch_stats = BatchSummary() if len(list_of_files) > 0: batch_stats.set_batch_start_date_time() for cur_file in list_of_files: file_stats = FileSummary() file_stats.set_file_name(cur_file) try: process_task = loop.create_task( process_file(cur_file, file_stats)) file_stats = await process_task batch_stats.add_file(file_stats) finally: pass #loop.close() batch_stats.set_batch_end_date_time() return json.dumps(batch_stats.toJSON()) else: return Response(('{"Error": "No files found on folder %s"}' % Configuration.FILE_DIRECTORY), status=404, mimetype='application/json')
async def wrapper(*args, **kwargs): if request.headers.get('Authorization') == None: return Response(json.dumps({"error": "Bad request"}), 400) cred = request.headers['Authorization'] if cred == None or len(cred.split("&")) != 2: return Response(json.dumps({"error": "Forbidden"}), 403) database = db.Database() username, password = cred.split("&") isValidUser = database.check_valid_cred(username, password) if isValidUser != "Success": return Response(json.dumps({"error": "Forbidden"}), 403)
async def delete(self, *args, **kwargs): ''' Get Example view ''' return Response(json.dumps({'result': 'admin example works'}), status=300, mimetype='application/json')
async def messages(): # Main bot message handler. if "application/json" in request.headers["Content-Type"]: body = await request.json else: return Response("", status=415) activity = Activity().deserialize(body) auth_header = (request.headers["Authorization"] if "Authorization" in request.headers else "") try: await ADAPTER.process_activity(activity, auth_header, BOT.on_turn) return Response("", status=201) except Exception as exception: raise exception
async def telegram_token(): request_body = await request.get_data() request_payload = json.loads(request_body) token = await worker_input_queue.add_task_with_result_future( partial(aggregator.create_telegram_connect_token, request_payload['user_id']), request.logger) return Response(token.encode('utf-8'), mimetype='text/plain')
async def stage(self, GUID): try: stage_file = self.dispatch_event(Events.ENCRYPT_STAGE, (GUID, request.remote_addr, self["Comms"])) self.dispatch_event(Events.SESSION_STAGED, f'Sending stage ({sys.getsizeof(stage_file)} bytes) -> {request.remote_addr} ...') return Response(stage_file, content_type='application/octet-stream') except IPCException: return '', 400
async def telegram_disconnect(): request_body = await request.get_data() request_payload = json.loads(request_body) await worker_input_queue.add_task_with_result_future( partial(aggregator.delete_telegram_id_for_user, request_payload['user_id']), request.logger) return Response('Ok', mimetype='text/plain')