-
Notifications
You must be signed in to change notification settings - Fork 0
/
responses.py
54 lines (35 loc) · 1.15 KB
/
responses.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
from abc import ABC, abstractmethod
import json
class BaseResponse(ABC):
header = "text/plain"
@abstractmethod
def __init__(self, response):
pass
@abstractmethod
def get_bytes_response(self):
pass
class JSONResponse(BaseResponse):
header = "application/json"
def __init__(self, response):
self.response = json.dumps(response)
def get_bytes_response(self):
return self.response.encode("utf-8")
class HTMLResponse(BaseResponse):
header = "text/html"
def __init__(self, file_path):
with open(f"templates/{file_path}", "r", encoding="utf-8") as file:
self.response = file.read()
def get_bytes_response(self):
return self.response.encode("utf-8")
class HTMLTextResponse(BaseResponse):
header = "text/html"
def __init__(self, response):
self.response = response
def get_bytes_response(self):
return self.response.encode("utf-8")
class TextResponse(BaseResponse):
header = "text/plain"
def __init__(self, response):
self.response = response
def get_bytes_response(self):
return self.response.encode("utf-8")