async def analyze(request): data = await request.body() img_data = json.loads(data.decode('utf-8'))['data'] img_bytes = base64.b64decode(img_data) try: return sl_res.JSONResponse( dict( prediction=util.predict_bytes(learner, io.BytesIO(img_bytes)))) except PIL.UnidentifiedImageError: return sl_res.JSONResponse(dict(error='Cannot read image'))
async def queues( owner_id: github_types.GitHubAccountIdType, redis_cache: utils.RedisCache = fastapi.Depends( # noqa: B008 redis.get_redis_cache ), ) -> responses.Response: queues: typing.Dict[ str, typing.Dict[str, typing.List[int]] ] = collections.defaultdict(dict) async for queue in redis_cache.scan_iter( match=f"merge-*~{owner_id}~*", count=10000 ): queue_type, _, repo_id, branch = queue.split("~") if queue_type == "merge-queue": queues[repo_id][branch] = [ int(pull) async for pull, _ in redis_cache.zscan_iter(queue) ] elif queue_type == "merge-train": train_raw = await redis_cache.get(queue) train = typing.cast(merge_train.Train.Serialized, json.loads(train_raw)) _, _, repo_id, branch = queue.split("~") queues[repo_id][branch] = [ int(c["user_pull_request_number"]) for c in train["cars"] ] + [int(wp[0]) for wp in train["waiting_pulls"]] return responses.JSONResponse(status_code=200, content=queues)
async def refresh_pull( owner_login: github_types.GitHubLogin, repo_name: github_types.GitHubRepositoryName, pull_request_number: github_types.GitHubPullRequestNumber, action: github_types.GitHubEventRefreshActionType = "user", redis_links: redis_utils.RedisLinks = fastapi.Depends( # noqa: B008 redis.get_redis_links), ) -> responses.Response: action = RefreshActionSchema(action) installation_json = await github.get_installation_from_login(owner_login) async with github.aget_client(installation_json) as client: try: repository = await client.item(f"/repos/{owner_login}/{repo_name}") except http.HTTPNotFound: return responses.JSONResponse(status_code=404, content="repository not found") await utils.send_pull_refresh( redis_links.stream, repository, action=action, pull_request_number=pull_request_number, source="API", ) return responses.Response("Refresh queued", status_code=202)
async def refresh_branch( owner_login: github_types.GitHubLogin, repo_name: github_types.GitHubRepositoryName, branch: str, redis_cache: utils.RedisCache = fastapi.Depends( # noqa: B008 redis.get_redis_cache), redis_stream: utils.RedisStream = fastapi.Depends( # noqa: B008 redis.get_redis_stream), ) -> responses.Response: installation_json = await github.get_installation_from_login(owner_login) async with github.aget_client(installation_json) as client: try: repository = await client.item(f"/repos/{owner_login}/{repo_name}") except http.HTTPNotFound: return responses.JSONResponse(status_code=404, content="repository not found") await utils.send_branch_refresh( redis_cache, redis_stream, repository, action="user", source="API", ref=github_types.GitHubRefType(f"refs/heads/{branch}"), ) return responses.Response("Refresh queued", status_code=202)
async def get_stats( owner_id: github_types.GitHubAccountIdType, redis_links: redis_utils.RedisLinks = fastapi.Depends( # noqa: B008 redis.get_redis_links ), ) -> responses.Response: last_seen_at = await last_seen.get(redis_links.cache, owner_id) seats = await count_seats.Seats.get( redis_links.cache, write_users=False, owner_id=owner_id ) data = seats.jsonify() if data["organizations"]: if len(data["organizations"]) > 1: raise RuntimeError( "count_seats.Seats.get() returns more than one organization" ) repos = data["organizations"][0]["repositories"] else: repos = [] return responses.JSONResponse( { "repositories": repos, "last_seen_at": None if last_seen_at is None else last_seen_at.isoformat(), } )
async def refresh_pull( owner: github_types.GitHubLogin, repo_name: github_types.GitHubRepositoryName, pull_request_number: github_types.GitHubPullRequestNumber, action: github_types.GitHubEventRefreshActionType = "user", redis_cache: utils.RedisCache = fastapi.Depends( # noqa: B008 redis.get_redis_cache), redis_stream: utils.RedisStream = fastapi.Depends( # noqa: B008 redis.get_redis_stream), ) -> responses.Response: action = RefreshActionSchema(action) async with github.aget_client(owner_name=owner) as client: try: repository = await client.item(f"/repos/{owner}/{repo_name}") except http.HTTPNotFound: return responses.JSONResponse(status_code=404, content="repository not found") await utils.send_refresh( redis_cache, redis_stream, repository, pull_request_number=pull_request_number, action=action, ) return responses.Response("Refresh queued", status_code=202)
async def simulator(request: requests.Request) -> responses.JSONResponse: token = request.headers.get("Authorization") if token: token = token[6:] # Drop 'token ' data = SimulatorSchema(await request.json()) if data["pull_request"]: loop = asyncio.get_running_loop() title, summary = await loop.run_in_executor( None, functools.partial( _sync_simulator, data["mergify.yml"]["pull_request_rules"], *data["pull_request"], token=token, ), ) else: title, summary = ("The configuration is valid", None) return responses.JSONResponse(status_code=200, content={ "title": title, "summary": summary })
async def refresh_branch( owner: github_types.GitHubLogin, repo_name: github_types.GitHubRepositoryName, branch: str, redis_cache: utils.RedisCache = fastapi.Depends( # noqa: B008 redis.get_redis_cache ), redis_stream: utils.RedisStream = fastapi.Depends( # noqa: B008 redis.get_redis_stream ), ) -> responses.Response: async with github.aget_client(owner_name=owner) as client: try: repository = await client.item(f"/repos/{owner}/{repo_name}") except http.HTTPNotFound: return responses.JSONResponse( status_code=404, content="repository not found" ) await github_events.send_refresh( redis_cache, redis_stream, repository, ref=github_types.GitHubRefType(f"refs/heads/{branch}"), ) return responses.Response("Refresh queued", status_code=202)
async def rate_limited_handler( request: requests.Request, exc: engine_exceptions.RateLimited) -> responses.JSONResponse: return responses.JSONResponse( status_code=403, content={ "message": "Organization or user has hit GitHub API rate limit" }, )
async def voluptuous_errors( request: requests.Request, exc: voluptuous.Invalid ) -> responses.JSONResponse: # Replace payload by our own if isinstance(exc, voluptuous.MultipleInvalid): payload = {"errors": list(map(voluptuous_error, sorted(exc.errors, key=str)))} else: payload = {"errors": [voluptuous_error(exc)]} return responses.JSONResponse(status_code=400, content=payload)
async def simulator(request: requests.Request): payload = await request.json() loop = asyncio.get_running_loop() title, summary = await loop.run_in_executor( None, functools.partial(_sync_simulator, payload)) return responses.JSONResponse(status_code=200, content={ "title": title, "summary": summary })
async def simulator( request: requests.Request, redis_cache: utils.RedisCache = fastapi.Depends( # noqa: B008 redis.get_redis_cache ), ) -> responses.JSONResponse: token = request.headers.get("Authorization") if token: token = token[6:] # Drop 'token ' try: raw_json = await request.json() except json.JSONDecodeError: return responses.JSONResponse(status_code=400, content="invalid json") data = SimulatorSchema(raw_json) if data["pull_request"]: title, summary = await _simulator( redis_cache, data["mergify.yml"]["pull_request_rules"], owner=data["pull_request"][0], repo=data["pull_request"][1], pull_number=data["pull_request"][2], token=token, ) else: title, summary = ("The configuration is valid", None) pull_request_rules_conditions = [ [cond.tree for cond in rule.conditions] for rule in data["mergify.yml"]["pull_request_rules"] ] return responses.JSONResponse( status_code=200, content={ "title": title, "summary": summary, "conditions": { "pull_request_rules": pull_request_rules_conditions, }, }, )
async def queues(installation_id): queues = collections.defaultdict(dict) async for queue in _AREDIS_CACHE.scan_iter( match=f"strict-merge-queues~{installation_id}~*"): _, _, owner, repo, branch = queue.split("~") queues[owner + "/" + repo][branch] = [ int(pull) async for pull, _ in _AREDIS_CACHE.zscan_iter(queue) ] return responses.JSONResponse(status_code=200, content=queues)
async def get_torrent(request: Request, torrent: str) -> responses.JSONResponse: ''' Gets the top 3 torrents as a JSON :torrent: (str) -> The name of the torrent whether it be a movie, game, book, etc. :return: (JSON) JSON of top 3 torrents ''' return responses.JSONResponse(pirate_api.get_torrents(torrent))
async def queues(installation_id): redis = await utils.get_aredis_for_cache() queues = collections.defaultdict(dict) async for queue in redis.scan_iter( match=f"strict-merge-queues~{installation_id}~*"): _, _, owner, repo, branch = queue.split("~") queues[owner + "/" + repo][branch] = [ int(pull) async for pull, _ in redis.zscan_iter(queue) ] return responses.JSONResponse(status_code=200, content=queues)
async def voluptuous_errors(request: requests.Request, exc: voluptuous.Invalid): # FIXME(sileht): remove error at payload root payload = voluptuous_error(exc) payload["errors"] = [] if isinstance(exc, voluptuous.MultipleInvalid): payload["errors"].extend( map(voluptuous_error, sorted(exc.errors, key=str))) else: payload["errors"].extend(voluptuous_error(exc)) return responses.JSONResponse(status_code=400, content=payload)
async def queues(owner_id): global _AREDIS_CACHE queues = collections.defaultdict(dict) async for queue in _AREDIS_CACHE.scan_iter( match=f"merge-queue~{owner_id}~*"): _, _, repo_id, branch = queue.split("~") queues[repo_id][branch] = [ int(pull) async for pull, _ in _AREDIS_CACHE.zscan_iter(queue) ] return responses.JSONResponse(status_code=200, content=queues)
async def event_testing_handler_get(number: int = None): # pragma: no cover async with await _AREDIS_CACHE.pipeline() as p: if number is None: await p.lrange("events-testing", 0, -1) await p.delete("events-testing") values = (await p.execute())[0] else: for _ in range(number): await p.lpop("events-testing") values = await p.execute() data = [json.loads(i) for i in values if i is not None] return responses.JSONResponse(content=data)
async def refresh_repo( owner: github_types.GitHubLogin, repo_name: github_types.GitHubRepositoryName) -> responses.Response: global _AREDIS_STREAM, _AREDIS_CACHE async with github.aget_client(owner_name=owner) as client: try: repository = await client.item(f"/repos/{owner}/{repo_name}") except http.HTTPNotFound: return responses.JSONResponse(status_code=404, content="repository not found") await github_events.send_refresh(_AREDIS_CACHE, _AREDIS_STREAM, repository) return responses.Response("Refresh queued", status_code=202)
async def queues_by_owner_id(owner_id): global _AREDIS_CACHE queues = collections.defaultdict(dict) async for queue in _AREDIS_CACHE.scan_iter( match=f"merge-queue~{owner_id}~*"): _, _, repo_id, branch = queue.split("~") async with github.aget_client(owner_id=owner_id) as client: try: repo = await client.item(f"/repositories/{repo_id}") except exceptions.RateLimited: return responses.JSONResponse( status_code=403, content={ "message": f"{client.auth.owner} account with {client.auth.owner_id} ID, rate limited by GitHub" }, ) queues[client.auth.owner + "/" + repo["name"]][branch] = [ int(pull) async for pull, _ in _AREDIS_CACHE.zscan_iter(queue) ] return responses.JSONResponse(status_code=200, content=queues)
async def queues(installation_id): installation = await github_app.get_installation_from_id(installation_id) queues = collections.defaultdict(dict) async for queue in _AREDIS_CACHE.scan_iter( match=f"merge-queue~{installation['account']['id']}~*"): _, _, repo_id, branch = queue.split("~") owner = installation["account"]["login"] async with await github.aget_client(owner) as client: try: repo = await client.item(f"/repositories/{repo_id}") except exceptions.RateLimited: return responses.JSONResponse( status_code=403, content={ "message": f"{installation['account']['type']} account `{owner}` rate limited by GitHub" }, ) queues[owner + "/" + repo["name"]][branch] = [ int(pull) async for pull, _ in _AREDIS_CACHE.zscan_iter(queue) ] return responses.JSONResponse(status_code=200, content=queues)
async def refresh_pull( owner: github_types.GitHubLogin, repo_name: github_types.GitHubRepositoryName, pull_request_number: github_types.GitHubPullRequestNumber, action: github_types.GitHubEventRefreshActionType = "user", ) -> responses.Response: action = RefreshActionSchema(action) async with github.aget_client(owner_name=owner) as client: try: repository = await client.item(f"/repos/{owner}/{repo_name}") except http.HTTPNotFound: return responses.JSONResponse(status_code=404, content="repository not found") global _AREDIS_STREAM, _AREDIS_CACHE await github_events.send_refresh( _AREDIS_CACHE, _AREDIS_STREAM, repository, pull_request_number=pull_request_number, action=action, ) return responses.Response("Refresh queued", status_code=202)
async def process_token( bearer_token: dict, request: Request ) -> Union[AuthorizeResponse, AUTH_FAILURE]: """Post a bearer token to Discord, and return a JWT and username.""" interaction_start = datetime.datetime.now() try: user_details = await fetch_user_details(bearer_token["access_token"]) except httpx.HTTPStatusError: AUTH_FAILURE.delete_cookie("token") return AUTH_FAILURE user_id = user_details["id"] member = await get_member(request.state.db, user_id, force_refresh=True) max_age = datetime.timedelta(seconds=int(bearer_token["expires_in"])) token_expiry = interaction_start + max_age data = { "token": bearer_token["access_token"], "refresh": bearer_token["refresh_token"], "user_details": user_details, "in_guild": bool(member), "expiry": token_expiry.isoformat() } token = jwt.encode(data, SECRET_KEY, algorithm="HS256") user = User(token, user_details, member) response = responses.JSONResponse({ "username": user.display_name, "expiry": token_expiry.isoformat() }) await set_response_token(response, request, token, bearer_token["expires_in"]) return response
async def redis_errors( request: requests.Request, exc: aredis.exceptions.ConnectionError) -> responses.JSONResponse: statsd.increment("redis.client.connection.errors") LOG.warning("FastAPI lost Redis connection", exc_info=exc) return responses.JSONResponse(status_code=503)
import jwt from pydantic.fields import Field from pydantic.main import BaseModel from spectree.response import Response from starlette import responses from starlette.authentication import requires from starlette.requests import Request from backend import constants from backend.authentication.user import User from backend.constants import SECRET_KEY from backend.discord import fetch_bearer_token, fetch_user_details, get_member from backend.route import Route from backend.validation import ErrorMessage, api AUTH_FAILURE = responses.JSONResponse({"error": "auth_failure"}, status_code=400) class AuthorizeRequest(BaseModel): token: str = Field(description="The access token received from Discord.") class AuthorizeResponse(BaseModel): username: str = Field("Discord display name.") expiry: str = Field("ISO formatted timestamp of expiry.") async def process_token( bearer_token: dict, request: Request ) -> Union[AuthorizeResponse, AUTH_FAILURE]:
def status(request): return sl_res.JSONResponse(dict(status='OK'))