-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_example.py
72 lines (49 loc) · 1.57 KB
/
proxy_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import logging
from dataclasses import dataclass
from typing import Protocol
@dataclass
class Mail:
address: str = "DEFAULT ADDRESS"
class MailerInterface(Protocol):
def send(self, mail: Mail) -> None:
...
class JobWorker:
def __init__(self, mailer: MailerInterface) -> None:
self.mailer = mailer
def process(self) -> None:
print("ジョブの処理")
report_mail = Mail()
self.mailer.send(report_mail)
print("後処理")
class RealMailer:
def send(self, mail: Mail) -> None:
print("メール送信")
class LoggingInterface(Protocol):
def info(self, message: str) -> None:
...
class LoggingMailerProxy:
def __init__(
self, target: MailerInterface, logger: LoggingInterface
) -> None:
self.target = target
self.logger = logger
def send(self, mail: Mail) -> None:
self.logger.info(f"Before send {mail.address}")
self.target.send(mail)
self.logger.info(f"After send {mail.address}")
class StdlibLogger:
def __init__(self) -> None:
logger = logging.getLogger(__name__)
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
logger.setLevel(logging.INFO)
logger.addHandler(handler)
self.logger = logger
def info(self, message: str) -> None:
self.logger.info(message)
if __name__ == "__main__":
worker = JobWorker(RealMailer())
worker.process()
print()
other_worker = JobWorker(LoggingMailerProxy(RealMailer(), StdlibLogger()))
other_worker.process()