Skip to content

Commit bb6ef75

Browse files
committed
Refactor in HTTPStatus
1 parent c0ca616 commit bb6ef75

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

adafruit_httpserver/server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .request import HTTPRequest
1010
from .response import HTTPResponse
1111
from .route import HTTPRoute
12-
from .status import HTTPStatus
12+
from .status import BAD_REQUEST_400
1313

1414
class HTTPServer:
1515
"""A basic socket-based HTTP server."""
@@ -107,9 +107,9 @@ def poll(self):
107107
elif request.method == HTTPMethod.GET:
108108
response = HTTPResponse(filename=request.path, root=self.root_path)
109109

110-
# If no handler exists and request method is not GET, return 500 Internal Server Error.
110+
# If no handler exists and request method is not GET, return 400 Bad Request.
111111
else:
112-
response = HTTPResponse(status=HTTPStatus.INTERNAL_SERVER_ERROR)
112+
response = HTTPResponse(status=BAD_REQUEST_400)
113113

114114
response.send(conn)
115115
except OSError as ex:

adafruit_httpserver/status.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
class HTTPStatus: # pylint: disable=too-few-public-methods
22
"""HTTP status codes."""
33

4-
def __init__(self, value, phrase):
4+
def __init__(self, code, text):
55
"""Define a status code.
66
77
:param int value: Numeric value: 200, 404, etc.
88
:param str phrase: Short phrase: "OK", "Not Found', etc.
99
"""
10-
self.value = value
11-
self.phrase = phrase
10+
self.code = code
11+
self.text = text
1212

1313
def __repr__(self):
14-
return f'HTTPStatus({self.value}, "{self.phrase}")'
14+
return f'HTTPStatus({self.code}, "{self.text}")'
1515

1616
def __str__(self):
17-
return f"{self.value} {self.phrase}"
17+
return f"{self.code} {self.text}"
1818

1919

20-
HTTPStatus.NOT_FOUND = HTTPStatus(404, "Not Found")
21-
"""404 Not Found"""
22-
HTTPStatus.OK = HTTPStatus(200, "OK") # pylint: disable=invalid-name
23-
"""200 OK"""
24-
HTTPStatus.INTERNAL_SERVER_ERROR = HTTPStatus(500, "Internal Server Error")
25-
"""500 Internal Server Error"""
20+
OK_200 = HTTPStatus(200, "OK")
21+
BAD_REQUEST_400 = HTTPStatus(400, "Bad Request")
22+
NOT_FOUND_404 = HTTPStatus(404, "Not Found")
23+
INTERNAL_SERVER_ERROR_500 = HTTPStatus(500, "Internal Server Error")

0 commit comments

Comments
 (0)