-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathchain_of_responsibility.py
More file actions
67 lines (44 loc) · 1.95 KB
/
Copy pathchain_of_responsibility.py
File metadata and controls
67 lines (44 loc) · 1.95 KB
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
from __future__ import annotations
from abc import ABC, abstractmethod
class HandlerChain(ABC):
def __init__(self, input_header: HandlerChain):
self.next_header = input_header
@abstractmethod
def add_header(self, input_header: str):
pass
def do_next(self, input_header: str):
if self.next_header:
return self.next_header.add_header(input_header)
return input_header
class AuthenticationHeader(HandlerChain):
def __init__(self, token: str, next_header: HandlerChain = None):
super().__init__(next_header)
self.token = token
def add_header(self, input_header: str):
h = f"{input_header}\nAuthorization: {self.token}"
return self.do_next(h)
class ContentTypeHeader(HandlerChain):
def __init__(self, content_type: str, next_header: HandlerChain = None):
super().__init__(next_header)
self.content_type = content_type
def add_header(self, input_header: str):
h = f"{input_header}\nContentType: {self.content_type}"
return self.do_next(h)
class BodyPayloadHeader(HandlerChain):
def __init__(self, body: str, next_header: HandlerChain = None):
super().__init__(next_header)
self.body = body
def add_header(self, input_header: str):
h = f"{input_header}\n{self.body}"
return self.do_next(h)
if __name__ == '__main__':
authentication_header = AuthenticationHeader("123456")
content_type_header = ContentTypeHeader("json")
body_header = BodyPayloadHeader("Body: {\"username\" = \"john\"}")
authentication_header.next_header = content_type_header
content_type_header.next_header = body_header
message_with_authentication = authentication_header.add_header("Header with authentication")
message_without_authentication = content_type_header.add_header("Header without authentication")
print(message_with_authentication)
print()
print(message_without_authentication)