class App(Application): ran = False pump: IPump = Inject() heater: IHeater = Inject() def run(self) -> None: assert isinstance(self.pump, IPump) assert isinstance(self.heater, IHeater) App.ran = True
class Mid1Middleware(Middleware): ''' Registry - is a place to store your variables/objects that you need in a global scope. ''' registry: Registry = Inject() def process_request(self, req: falcon.Request, resp: falcon.Response) -> None: token = req.auth print('TTTTTTTTTTTTTTTTTOKEN TOKEN TOKEN') print(repr(req.auth)) print('TOKEN TOKEN TOKEN') if not token: return token_type, _, access_token = token.partition(" ") if token_type == "Bearer": self.registry.access_token = access_token # type: ignore def process_resource( self, req: falcon.Request, resp: falcon.Response, resource: Resource, params: Dict[str, Any], ) -> None: pass # do nothing # raise BaseException('%%%% -- BB process_resource-- test test test %%%%') def process_response(self, req: falcon.Request, resp: falcon.Response, resource: Resource) -> None: pass
class Heater(IHeater): extra_pump: IPump = Inject('extra_pump') def heat(self) -> None: print("Heating...") def __repr__(self): return '<Heater id=%s\nextra_pump=%r>' % (id(self), self.extra_pump)
class CoffeeMaker: heater: IHeater = Inject() @inject def __init__(self, pump: IPump): self.pump = pump def make_coffee(self): return "heater: %r\npump: %r" % (self.heater, self.pump)
class WhoAmICommand(Command): registry: Registry = Inject() def handle(self) -> Dict[str, bool]: try: if self.registry.access_token is None: raise NotAuthenticatedError except AttributeError: raise NotAuthenticatedError return {"is_admin": False}
class AuthenticationMiddleware(Middleware): registry: Registry = Inject() def process_request(self, req: falcon.Request, resp: falcon.Response) -> None: token = req.auth relative_uri = req.relative_uri if not token: if relative_uri not in settings.NO_AUTH_REQUIRED.uris: if relative_uri in settings.NO_SESSION_TOKEN_REQUIRED.uris: raise falcon.HTTPUnauthorized("Unauthorized") raise falcon.HTTPForbidden("Forbidden") if token: token_type, _, access_token = token.partition(" ") if token_type != "Bearer": falcon.HTTPForbidden("Forbidden") if is_correct_api_key(access_token): level = 3 elif not access_token: level = 1 else: level = 2 auth_type = next(i for i in settings.AUTH_CLASSES if relative_uri in i.uris) auth = auth_type.__class__(auth_type.uris, level) if auth < auth_type: if level == 1: raise falcon.HTTPUnauthorized("Unauthorized") raise falcon.HTTPForbidden("Forbidden") self.registry.access_token = access_token # type: ignore def process_resource( self, req: falcon.Request, resp: falcon.Response, resource: Resource, params: Dict[str, Any], ) -> None: pass # do nothing def process_response(self, req: falcon.Request, resp: falcon.Response, resource: Resource) -> None: pass
class RegistryMiddleware(Middleware): registry: Registry = Inject() def process_request(self, req: falcon.Request, resp: falcon.Response) -> None: self.registry.initialize() def process_resource( self, req: falcon.Request, resp: falcon.Response, resource: Resource, params: Dict[str, Any], ) -> None: pass # do nothing def process_response( self, req: falcon.Request, resp: falcon.Response, resource: Resource ) -> None: self.registry.clean()
class Pump(PumpInterface): heater: HeaterInterface = Inject() def __repr__(self): return '<Pump id=%s heater=%r>' % (id(self), self.heater)
class Heater(HeaterInterface): amazing_pump: ExtraPumpInterface = Inject() def __repr__(self): return '<Heater id=%s\namazing_pump=%r>' % ( id(self), self.amazing_pump)
class CoffeeMaker: heater: HeaterInterface = Inject() pump: PumpInterface = Inject() def make_coffee(self): return "heater: %r\npump: %r" % (self.heater, self.pump)
class AccountService: """AccountService aka AccountRepo""" connector: interfaces.IConnector = Inject() def initial_accounts(self) -> models.Account: return
class PumpTest(IPump): heater: IHeater = Inject() def __repr__(self): return '<PumpTest id=%s heater=%r>' % (id(self), self.heater)