Exemple #1
0
	async def middlewareHandler(self, WebRequest:web.Request, handler:Callable) -> web.Response:
		WebRequest.__class__ = ExtendedRequest # people told me to never do this... well f**k it i do it anyways
		WebRequest:ExtendedRequest
		WebRequest.WebUser = None
		WebRequest.DiscordUser = None
		WebRequest.TwitchUser = None

		try:
			if str(handler.__name__) in ["_handle"]:
				raise FileNotFoundError()

			if not self.BASE.Active.web:
				return await self.Tree.Api.errors.apiNotAllowed(self, WebRequest, msg="Web is disabled and will be shutdown soon")

			if not self.BASE.Active.api and WebRequest.path.startswith("/api"):
				return await self.Tree.Api.errors.apiNotAllowed(self, WebRequest, msg="API endpoint is not enabled")

			response:web.Response = await handler(self, WebRequest)
			return response

		except web.HTTPException as HTTPEx:
			return self.response(
				body=json.dumps(dict(msg=HTTPEx.reason, status=HTTPEx.status)),
				status=HTTPEx.status,
				content_type='application/json'
			)

		except FileNotFoundError:
			if WebRequest.path.startswith("/api"):
				return await self.Tree.Api.errors.apiUnknown(self, WebRequest)
			else:
				return await self.Tree.errors.notFound(self, WebRequest)

		except Exception as e:
			tb:str = traceback.format_exc()
			self.BASE.Logger.error(f"(Web) Error in request: {str(e)}\n{tb}")
			error:str = str(e) if CliArgs.get("debug") == "all" else "Unknown error"
			return self.response(
				status=500,
				body=json.dumps(dict(msg=error, status=500)),
				content_type='application/json'
			)
Exemple #2
0
async def getDiscordUserInfo(cls: "WebIndex", WebRequest: Request,
                             **kwargs: Any) -> DiscordWebUserInfo:
    """
		Tryes to get a DiscordUser, takes get, post, and cookie in process
		kwargs are given to DiscordWebUserInfo

		DiscordWebUserInfo kwargs:
			force_method
			phaaze_discord_session
	"""

    if hasattr(WebRequest, "DiscordUser"):
        cls.Web.BASE.Logger.debug(
            f"(Web) Used stored discord infos: {str(WebRequest.DiscordUser)}",
            require="web:debug")
        return WebRequest.DiscordUser

    DiscordUser: DiscordWebUserInfo = DiscordWebUserInfo(
        cls.Web.BASE, WebRequest, **kwargs)
    await DiscordUser.auth()
    WebRequest.DiscordUser = DiscordUser

    return WebRequest.DiscordUser