Skip to content

Commit 41edff8

Browse files
committed
Added CommonHTTPStatus
1 parent 132d36c commit 41edff8

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

adafruit_httpserver/response.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from socketpool import SocketPool
1111

1212
from .mime_type import MIMEType
13-
from .status import HTTPStatus, OK_200, NOT_FOUND_404
13+
from .status import HTTPStatus, CommonHTTPStatus
1414

1515
class HTTPResponse:
1616
"""Details of an HTTP response. Use in `HTTPServer.route` decorator functions."""
@@ -27,7 +27,7 @@ class HTTPResponse:
2727

2828
def __init__(
2929
self,
30-
status: HTTPStatus = OK_200,
30+
status: HTTPStatus = CommonHTTPStatus.OK_200,
3131
body: str = "",
3232
headers: Dict[str, str] = None,
3333
content_type: str = MIMEType.TXT,
@@ -52,7 +52,7 @@ def __init__(
5252
@staticmethod
5353
def _construct_response_bytes(
5454
http_version: str = "HTTP/1.1",
55-
status: HTTPStatus = OK_200,
55+
status: HTTPStatus = CommonHTTPStatus.OK_200,
5656
content_type: str = MIMEType.TXT,
5757
content_length: Union[int, None] = None,
5858
headers: Dict[str, str] = None,
@@ -93,9 +93,9 @@ def send(self, conn: Union[SocketPool.Socket, socket.socket]) -> None:
9393
except OSError:
9494
self._send_response(
9595
conn,
96-
status = NOT_FOUND_404,
96+
status = CommonHTTPStatus.NOT_FOUND_404,
9797
content_type = MIMEType.TXT,
98-
body = f"{NOT_FOUND_404} {self.filename}",
98+
body = f"{CommonHTTPStatus.NOT_FOUND_404} {self.filename}",
9999
)
100100
else:
101101
self._send_response(

adafruit_httpserver/server.py

Lines changed: 2 additions & 2 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 BAD_REQUEST_400
12+
from .status import CommonHTTPStatus
1313

1414
class HTTPServer:
1515
"""A basic socket-based HTTP server."""
@@ -106,7 +106,7 @@ def poll(self):
106106

107107
# If no handler exists and request method is not GET, return 400 Bad Request.
108108
else:
109-
response = HTTPResponse(status=BAD_REQUEST_400)
109+
response = HTTPResponse(status=CommonHTTPStatus.BAD_REQUEST_400)
110110

111111
response.send(conn)
112112
except OSError as ex:

adafruit_httpserver/status.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@ def __str__(self):
1717
return f"{self.code} {self.text}"
1818

1919

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")
20+
class CommonHTTPStatus(HTTPStatus):
21+
22+
OK_200 = HTTPStatus(200, "OK")
23+
"""200 OK"""
24+
25+
BAD_REQUEST_400 = HTTPStatus(400, "Bad Request")
26+
"""400 Bad Request"""
27+
28+
NOT_FOUND_404 = HTTPStatus(404, "Not Found")
29+
"""404 Not Found"""
30+
31+
INTERNAL_SERVER_ERROR_500 = HTTPStatus(500, "Internal Server Error")
32+
"""500 Internal Server Error"""

0 commit comments

Comments
 (0)