async def enter_queue(request: Request): query = ( select(model.Ticket) .where(model.Service.id == request.parsed.id) .join(model.Service, model.Service.id == model.Ticket.service_id) .order_by(model.Ticket.enqueue_at.desc()) .limit(1) ) result = await request.connection.execute(query) last_queue_item = result.scalars().first() if last_queue_item is None: last_ticket_id = '000' else: last_ticket_id = last_queue_item.ticket_id query = ( select(model.Ticket) .where(model.Ticket.user_id == request.user.id) .order_by(model.Ticket.enqueue_at.desc()) .limit(1) ) last_ticket: model.Ticket = (await request.connection.execute(query)).scalars().first() if last_ticket and last_ticket.state != 'PROCESSED': raise HTTPException(409) query = ( select(model.Service.index) .where(model.Service.id == request.parsed.id) ) service_index = (await request.connection.execute(query)).scalar() new_queue_item = model.Ticket( user_id=request.user.id, service_id=request.parsed.id, ticket_id=generate_next_ticket(last_ticket_id, service_index), ) request.connection.add(new_queue_item) return ProtobufResponse(ticket_pb2.Ticket(ticket_id=new_queue_item.ticket_id))
async def get_run_events(request): run_uuid = request.path_params["run_uuid"] event_kind = request.path_params["event_kind"] if event_kind not in V1ArtifactKind.VALUES: raise HTTPException( detail="received an unrecognisable event {}.".format(event_kind), status_code=status.HTTP_400_BAD_REQUEST, ) event_names = request.query_params["names"] orient = request.query_params.get("orient") orient = orient or V1Events.ORIENT_DICT event_names = {e for e in event_names.split(",") if e} if event_names else set([]) events = await get_archived_operation_events(run_uuid=run_uuid, event_kind=event_kind, event_names=event_names, orient=orient) return UJSONResponse({"data": events})
async def app(request: Request) -> Response: try: body = None if body_field: if is_body_form: body = await request.form() else: body_bytes = await request.body() if body_bytes: body = await request.json() except Exception as e: logging.error(f"Error getting request body: {e}") raise HTTPException( status_code=400, detail="There was an error parsing the body" ) from e values, errors, background_tasks = await solve_dependencies( request=request, dependant=dependant, body=body ) if errors: raise RequestValidationError(errors) else: assert dependant.call is not None, "dependant.call must be a function" if is_coroutine: raw_response = await dependant.call(**values) else: raw_response = await run_in_threadpool(dependant.call, **values) if isinstance(raw_response, Response): if raw_response.background is None: raw_response.background = background_tasks return raw_response response_data = serialize_response( field=response_field, response=raw_response, include=response_model_include, exclude=response_model_exclude, by_alias=response_model_by_alias, skip_defaults=response_model_skip_defaults, ) return response_class( content=response_data, status_code=status_code, background=background_tasks, )
def area_all_data(): config = get_config() area_all = config.get_area_all() if area_all is None: raise HTTPException(status_code=status.HTTP_501_NOT_IMPLEMENTED, detail=f"The area: 'ALL' does not exist") return { "violation_threshold": area_all.violation_threshold, "notify_every_minutes": area_all.notify_every_minutes, "emails": ",".join(area_all.emails), "enable_slack_notifications": area_all.enable_slack_notifications, "daily_report": area_all.daily_report, "daily_report_time": area_all.daily_report_time, "occupancy_threshold": area_all.occupancy_threshold, "id": area_all.id, "name": area_all.name, "cameras": ",".join(area_all.cameras) }
async def delete_area(area_id: str, reboot_processor: Optional[bool] = True): """ Deletes the configuration related to the area <area_id> """ config_dict = extract_config() areas_name = [x for x in config_dict.keys() if x.startswith("Area")] areas = [map_section_from_config(x, config_dict) for x in areas_name] areas_ids = [area["id"] for area in areas] try: index = areas_ids.index(area_id) except ValueError: raise HTTPException(status_code=404, detail=f"The area: {area_id} does not exist") config_dict.pop(f"Area_{index}") config_dict = reestructure_areas((config_dict)) success = update_config(config_dict, reboot_processor) return handle_response(None, success, status.HTTP_204_NO_CONTENT)
async def __call__(self, scope, receive, send): request = Request(scope, receive, formats=get_formats()) response = Response(req=request, formats=get_formats()) path_params = scope.get("path_params", {}) before_requests = scope.get("before_requests", []) for before_request in before_requests.get("http", []): if asyncio.iscoroutinefunction(before_request): await before_request(request, response) else: await run_in_threadpool(before_request, request, response) views = [] if inspect.isclass(self.endpoint): endpoint = self.endpoint() on_request = getattr(endpoint, "on_request", None) if on_request: views.append(on_request) method_name = f"on_{request.method}" try: view = getattr(endpoint, method_name) views.append(view) except AttributeError: if on_request is None: raise HTTPException(status_code=status_codes.HTTP_405) else: views.append(self.endpoint) for view in views: # "Monckey patch" for graphql: explicitly checking __call__ if asyncio.iscoroutinefunction( view) or asyncio.iscoroutinefunction(view.__call__): await view(request, response, **path_params) else: await run_in_threadpool(view, request, response, **path_params) if response.status_code is None: response.status_code = status_codes.HTTP_200 await response(scope, receive, send)
async def init_crawl_browsers(self, browser_init_opts: Dict, start: bool = False) -> Dict: """Initializes this crawls browsers and if start is true starts each browser in the crawl :param browser_init_opts: Browser initialization options :param start: T/F indicating if the the intialized browser should be started :return: A dictionary containing information about the results of this operation """ errors = [] browsers = [] for _ in range(self.model.num_browsers): res = await self.manager.request_flock(browser_init_opts) reqid = res.get('reqid') if not reqid: if 'error' in res: errors.append(res['error']) continue if start: res = await self.manager.start_flock(reqid) if 'error' in res: errors.append(res['error']) else: browsers.append(reqid) await self.redis.sadd(self.browser_key, reqid) if errors: raise HTTPException(400, detail=errors) if start: self.model.status = 'running' await self.redis.hset(self.info_key, 'status', 'running') return { 'success': True, 'browsers': browsers, 'status': self.model.status, 'id': self.crawl_id, }
async def sources(request): """Fetch sources.""" if site(request, 'minimal') or not site(request, 'show_source_page'): raise HTTPException(status_code=404) sources_, authors = await asyncio.gather( request.app.state.database.fetch_all(SOURCE_QUERY, dict(site_id=site(request, 'id'))), request.app.state.database.fetch_all(AUTHOR_QUERY, dict(site_id=site(request, 'id'))) ) authors_by_source = defaultdict(list) for author in authors: authors_by_source[author['source_id']].append(author) return await template(request, 'sources.html', sources=[ dict(authors=authors_by_source.get(source['id']), **source) for source in sources_ ] )
async def create_camera(new_camera: CameraDTO, reboot_processor: Optional[bool] = True): """ Adds a new camera to the processor. """ config_dict = extract_config() cameras_name = [x for x in config_dict.keys() if x.startswith("Source_")] cameras = [map_camera(x, config_dict) for x in cameras_name] if new_camera.id in [camera["id"] for camera in cameras]: raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=bad_request_serializer( "Camera already exists", error_type="config duplicated camera")) camera_dict = map_to_camera_file_format(new_camera) config_dict[f"Source_{len(cameras)}"] = camera_dict success = update_config(config_dict, reboot_processor) if not success: return handle_response(camera_dict, success, status.HTTP_201_CREATED) return next((camera for camera in get_cameras(["withImage"]) if camera["id"] == camera_dict["Id"]), None)
async def config_calibrated_distance(camera_id: str, body: ConfigHomographyMatrix): """ Calibrates the camera <camera_id> receiving as input the coordinates of a square of size 3ft 3" by 3ft 3" (1m by 1m). """ dir_source = next((source for source in settings.config.get_video_sources() if source["id"] == camera_id), None) if not dir_source: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"The camera: {camera_id} does not exist") dir_path = get_camera_calibration_path(settings.config, camera_id) compute_and_save_inv_homography_matrix(points=body, destination=dir_path) sections = settings.config.get_sections() config_dict = {} for section in sections: config_dict[section] = settings.config.get_section_dict(section) config_dict[dir_source["section"]]["DistMethod"] = "CalibratedDistance" success = update_and_restart_config(config_dict) return handle_response(None, success, status.HTTP_204_NO_CONTENT)
async def delete_camera(camera_id: str): """ Deletes the configuration related to the camera <camera_id> """ config_dict = extract_config() camera_names = [x for x in config_dict.keys() if x.startswith("Source")] cameras = [map_camera(x, config_dict) for x in camera_names] cameras_ids = [camera["id"] for camera in cameras] try: index = cameras_ids.index(camera_id) except ValueError: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"The camera: {camera_id} does not exist") config_dict = delete_camera_from_areas(camera_id, config_dict) config_dict.pop(f"Source_{index}") config_dict = reestructure_cameras((config_dict)) success = update_and_restart_config(config_dict) return handle_response(None, success, status.HTTP_204_NO_CONTENT)
async def edit_camera(camera_id: str, edited_camera: SourceConfigDTO): """ Edits the configuration related to the camera <camera_id> """ edited_camera.id = camera_id config_dict = extract_config() camera_names = [x for x in config_dict.keys() if x.startswith("Source")] cameras = [map_camera(x, config_dict, []) for x in camera_names] cameras_ids = [camera["id"] for camera in cameras] try: index = cameras_ids.index(camera_id) except ValueError: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"The camera: {camera_id} does not exist") camera_dict = map_to_camera_file_format(edited_camera) config_dict[f"Source_{index}"] = map_to_camera_file_format(edited_camera) success = update_and_restart_config(config_dict) return handle_response(camera_dict, success)
async def verify_signature( request: Request, x_slack_signature: str = Header(...), x_slack_request_timestamp: str = Header(...), ): log.debug("Starting verification") body = await request.body() to_verify = str.encode("v0:" + str(x_slack_request_timestamp) + ":") + body our_hash = hmac.new( os.environ.get("SLACK_SIGNING_SECRET").encode(), to_verify, sha256).hexdigest() our_signature = "v0=" + our_hash if not hmac.compare_digest(x_slack_signature, our_signature): log.info("Slack verification failed") raise HTTPException(HTTP_403_FORBIDDEN, "Forbidden") log.debug("Verification successful")
async def get_related(self, id: Any, relationship: str, related_id: Any = None, *args, **kwargs) -> Response: team = Team.get_item(id) if not team: raise TeamNotFound if relationship == 'users': if not related_id: return await self.to_response(await self.serialize_related( team.users, many=True)) else: filtered_users = list( filter(lambda user: user.id == related_id, team.users)) if len(filtered_users) == 1: return await self.to_response(await self.serialize_related( filtered_users[0])) raise HTTPException(status_code=404)
async def delete_article_from_favorites( slug: str = Path(..., min_length=1), user: User = Depends(get_current_user_authorizer()), db: DataBase = Depends(get_database), ): async with db.pool.acquire() as conn: dbarticle = await get_article_or_404(conn, slug, user.username) if not dbarticle.favorited: raise HTTPException( status_code=HTTP_400_BAD_REQUEST, detail="You don't have this article in favorites", ) dbarticle.favorited = False dbarticle.favorites_count -= 1 async with conn.transaction(): await remove_article_from_favorites(conn, slug, user.username) return create_aliased_response(ArticleInResponse(article=dbarticle))
async def like_post( slug: str = Path(..., min_length=1), user: User = Depends(get_current_user_authorizer()), db: AsyncIOMotorClient = Depends(get_database), ): dbpost = await get_by_slug_or_404(db, slug, user.username, fx=get_post_by_slug) if dbpost.liked: raise HTTPException( status_code=HTTP_400_BAD_REQUEST, detail="You already added this post to likes", ) dbpost.liked = True dbpost.likes_count += 1 await add_post_to_likes(db, slug, user.username) return create_aliased_response(PostInResponse(post=dbpost))
def authenticate(request, tenant) -> bool: if "Authorization" not in request.headers: return False auth = request.headers["Authorization"] try: scheme, credentials = auth.split() if scheme.lower() != "basic": return False decoded = base64.b64decode(credentials).decode("ascii") except (ValueError, UnicodeDecodeError, binascii.Error): raise HTTPException(401) username, _, password = decoded.partition(":") valid_username, valid_password = request.app.state.config.encryption_credentials( tenant ) if username == valid_username and password == valid_password: return True return False
async def edit_periodic_task(periodic_task_name: str, edited_periodic_task: PeriodicTaskDTO, reboot_processor: Optional[bool] = True): """ Edits the configuration related to the periodic task <periodic_task_name>. """ edited_periodic_task.name = periodic_task_name config_dict = extract_config() edited_periodic_task_section = next( (key for key, value in config_dict.items() if key.startswith("PeriodicTask_") and value["Name"] == periodic_task_name), None) if not edited_periodic_task_section: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=f"The periodic_task: {periodic_task_name} does not exist") periodic_task_file = map_to_config_file_format(edited_periodic_task, True) config_dict[edited_periodic_task_section] = periodic_task_file success = update_config(config_dict, reboot_processor) return handle_response(periodic_task_file, success)
async def register(email: EmailStr = Body(...), code: str = Body(...), password: str = Body(...), db: AsyncIOMotorClient = Depends(get_database), rd: Redis = Depends(get_redis_database)): await check_free_username_and_email_mobile(db, email=email) # code 从redis校验邮箱验证码 if code != rd.get(email): raise HTTPException( status_code=HTTP_400_BAD_REQUEST, detail="验证码错误" ) # 组装默认参数,create_user只管insert user = UserInCreate(password=password, username='******'.format(int(time.time() * 1000)), email=email, channel=_ChannelEnum.RG, role=[1], thumb='https://www.baidu.com/img/flexible/logo/pc/result.png', activated=True) dbuser = await create_user(db, user) token = create_access_token(data={"id": dbuser.id}) return UserInResponse(user=User(**dbuser.dict(), token=token))
async def retrieve_instance(cls: models.BASE, session: models.Session, instance_id: UUID, options: Any = None) -> dict: """ Get an instance of cls by UUID """ query = session.query(cls) if options: query = query.options(options) def _retrieve(): instance = query.get(instance_id) if instance: return instance.as_dict() return None data = await run_in_threadpool(_retrieve) if data is None: raise HTTPException(status_code=404) return data
async def delete_view(cls, request): if not has_required_scope(request, cls.permission_scopes): raise HTTPException(403) if not cls.delete_form: raise MissingFormError() instance = await cls.get_object(request) context = cls.get_context(request) form_kwargs = { "form_cls": cls.delete_form, "data": instance, "obj": instance if not isinstance(instance, dict) else None, } if request.method == "GET": form = cls.get_form(**form_kwargs) context.update({"form": form, "object": instance}) return config.templates.TemplateResponse(cls.delete_template, context) data = await request.form() form = cls.get_form(**form_kwargs, formdata=data) if not form.validate(): context.update({"form": form, "object": instance}) return config.templates.TemplateResponse(cls.delete_template, context) try: await cls.do_delete(instance, form, request) message(request, "Deleted successfully", "success") except IntegrityError: message( request, "Could not be deleted due to being referenced by a related object", "error", ) return RedirectResponse(url=request.url_for(cls.url_names()["list"]), status_code=302)
async def list_view(cls, request): if not await cls.has_required_scope(request): raise HTTPException(403) context = cls.get_context(request) context.update( { "list_field_names": cls.list_field_names, "search_enabled": cls.search_enabled, "search": request.query_params.get("search"), "order_enabled": cls.order_enabled, "order_by": request.query_params.get("order_by"), "order_direction": request.query_params.get("order_direction"), } ) list_objects = cls.get_list_objects(request) if cls.paginate_by: paginator, page, list_objects, is_paginated = cls.paginate( request, list_objects ) context.update( { "paginator": paginator, "page_obj": page, "is_paginated": is_paginated, "list_objects": list_objects, } ) else: context.update( { "paginator": None, "page_obj": None, "is_paginated": False, "list_objects": list_objects, } ) return config.templates.TemplateResponse(cls.list_template, context)
async def get_run_events(request: Request) -> UJSONResponse: run_uuid = request.path_params["run_uuid"] event_kind = request.path_params["event_kind"] force = to_bool(request.query_params.get("force"), handle_none=True) if event_kind not in V1ArtifactKind.allowable_values: raise HTTPException( detail="received an unrecognisable event {}.".format(event_kind), status_code=status.HTTP_400_BAD_REQUEST, ) event_names = request.query_params["names"] orient = request.query_params.get("orient") orient = orient or V1Events.ORIENT_DICT event_names = {e for e in event_names.split(",") if e} if event_names else set([]) events = await get_archived_operation_events( run_uuid=run_uuid, event_kind=event_kind, event_names=event_names, orient=orient, check_cache=not force, ) return UJSONResponse({"data": events})
async def get_subject_id( bangumi_id: str, source: SupportWebsite, db: Database = Depends(get_db), ): if source == SupportWebsite.bilibili.value: r = await db.fetch_one( sa.select([sa.BangumiBilibili ]).where(sa.BangumiBilibili.season_id == bangumi_id)) else: r = await db.fetch_one( sa.select([sa.BangumiIqiyi ]).where(sa.BangumiIqiyi.bangumi_id == bangumi_id)) if not r: raise HTTPException(404) resp = { "source": source, "bangumi_id": bangumi_id, **r, } return resp
async def robots(request: Request) -> Response: try: body = await request.json() except JSONDecodeError: body = {} schema = SecretSchema() try: data = schema.load(body) except ValidationError as exc: return JSONResponse( {"errors": exc.messages}, status_code=status.HTTP_400_BAD_REQUEST ) if data["secret"] != secrets.ROBOTS: raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Secret is wrong, human." ) return PasswordResponse(passwords.GUESS_NUMBER)
async def post(self, *args, **kwargs) -> Response: json_body = await self.deserialize_body() user = User() username = json_body.get('username') if username: user.username = username else: raise HTTPException(status_code=400, detail='A valid `username` is required.') organization_id = json_body.get('organization') org = Organization.get_item(int(organization_id)) if not org: raise OrganizationNotFound user.organization = org user.save() result = await self.serialize(data=user) return await self.to_response(result, status_code=201)
async def delete_post_from_likes( slug: str = Path(..., min_length=1), user: User = Depends(get_current_user_authorizer()), db: AsyncIOMotorClient = Depends(get_database), ): dbpost = await get_by_slug_or_404(db, slug, user.username, fx=get_post_by_slug) if not dbpost.liked: raise HTTPException( status_code=HTTP_400_BAD_REQUEST, detail="You don't have this post in likes", ) dbpost.liked = False dbpost.likes_count -= 1 await remove_post_from_likes(db, slug, user.username) return create_aliased_response(PostInResponse(post=dbpost))
async def create_periodic_task(new_periodic_task: PeriodicTaskDTO, reboot_processor: Optional[bool] = True): """ Adds a periodic task. """ config_dict = extract_config() periodic_tasks_index = [ int(x[-1]) for x in config_dict.keys() if x.startswith("PeriodicTask_") ] periodic_tasks = get_periodic_tasks() if new_periodic_task.name in [ps["name"] for ps in periodic_tasks]: raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Periodict task already exists") periodic_task_file = map_to_config_file_format(new_periodic_task, True) index = 0 if periodic_tasks_index: index = max(periodic_tasks_index) + 1 config_dict[f"PeriodicTask_{index}"] = periodic_task_file success = update_config(config_dict, reboot_processor) return handle_response(periodic_task_file, success, status.HTTP_201_CREATED)
def get_auth_token(context: Context) -> Token: scope = None # Scopes will be validated later using Token.check_scope request = context.get('auth.request') config = context.get('config') if config.default_auth_client and 'authorization' not in request.headers: private_key = load_key(context, 'private.json') client = query_client(context, config.default_auth_client) grant_type = 'client_credentials' expires_in = int(datetime.timedelta(days=10).total_seconds()) token = create_access_token(context, private_key, client, grant_type, expires_in, client.scopes) request.headers = request.headers.mutablecopy() request.headers['authorization'] = f'Bearer {token}' resource_protector = context.get('auth.resource_protector') try: token = resource_protector.validate_request(scope, request) except JoseError as e: raise HTTPException(status_code=400, detail=e.error) return token
async def fold_round(game_id: str, user: User = Depends(get_current_user)): """ make the card swap before starting the round """ game = games.get(game_id) if not game: # ensure the game exists raise GAME_NOT_FOUND if user.uid not in game.players: raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail=f"Player {user.uid} not in Game.") res = game.event_player_fold(user) if res['requestValid']: await sio_emit_game_state(game_id) for uid in game.order: await sio_emit_player_state(game_id, uid) return game.get_cards(user)