Skip to content

Commit 85254e5

Browse files
committed
Changes to docstrings
1 parent e5ddaaf commit 85254e5

File tree

7 files changed

+40
-29
lines changed

7 files changed

+40
-29
lines changed

adafruit_httpserver/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class ResponseAlreadySentError(Exception):
4848

4949
class ServingFilesDisabledError(Exception):
5050
"""
51-
Raised when ``root_path`` is not set and there is no handler for `request`.
51+
Raised when ``root_path`` is not set and there is no handler for ``request``.
5252
"""
5353

5454

adafruit_httpserver/mime_types.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class MIMETypes:
2121
"""
2222

2323
DEFAULT = "text/plain"
24+
"""
25+
Default MIME type for unknown files.
26+
Can be changed using ``MIMETypes.configure(default_to=...)``.
27+
"""
2428

2529
REGISTERED = {
2630
".7z": "application/x-7z-compressed",
@@ -175,6 +179,13 @@ def configure(
175179
"""
176180
Allows to globally configure the MIME types.
177181
182+
It is recommended to **always** call this method before starting the ``Server``.
183+
Unregistering unused MIME types will **decrease overall memory usage**.
184+
185+
:param str default_to: The MIME type to use for unknown files.
186+
:param List[str] keep_for: File extensions to keep. All other will be unregistered.
187+
:param Dict[str, str] register: A dictionary mapping file extensions to MIME types.
188+
178189
Example::
179190
180191
MIMETypes.configure(
@@ -194,8 +205,10 @@ def configure(
194205
def get_for_filename(cls, filename: str, default: str = None) -> str:
195206
"""
196207
Return the MIME type for the given file name.
208+
If the file extension is not registered, ``default`` is returned.
197209
198210
:param str filename: The file name to look up.
211+
:param str default: Default MIME type to return if the file extension is not registered.
199212
"""
200213
if default is None:
201214
default = cls.DEFAULT

adafruit_httpserver/request.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
class Request:
2121
"""
2222
Incoming request, constructed from raw incoming bytes.
23-
It is passed as first argument to route handlers.
23+
It is passed as first argument to all route handlers.
2424
"""
2525

2626
connection: Union["SocketPool.Socket", "socket.socket"]
2727
"""
28-
Socket object usable to send and receive data on the connection.
28+
Socket object used to send and receive data on the connection.
2929
"""
3030

3131
client_address: Tuple[str, int]
@@ -42,7 +42,7 @@ class Request:
4242
"""Request method e.g. "GET" or "POST"."""
4343

4444
path: str
45-
"""Path of the request."""
45+
"""Path of the request, e.g. ``"/foo/bar"``."""
4646

4747
query_params: Dict[str, str]
4848
"""
@@ -56,7 +56,7 @@ class Request:
5656
"""
5757

5858
http_version: str
59-
"""HTTP version, e.g. "HTTP/1.1"."""
59+
"""HTTP version, e.g. ``"HTTP/1.1"``."""
6060

6161
headers: Headers
6262
"""
@@ -65,7 +65,7 @@ class Request:
6565

6666
raw_request: bytes
6767
"""
68-
Raw 'bytes' passed to the constructor and body 'bytes' received later.
68+
Raw 'bytes' that were received from the client.
6969
7070
Should **not** be modified directly.
7171
"""

adafruit_httpserver/response.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,13 @@ def route_func(request):
7373
"""The request that this is a response to."""
7474

7575
http_version: str
76+
7677
status: Status
78+
"""Status code of the response. Defaults to ``200 OK``."""
79+
7780
headers: Headers
81+
"""Headers to be sent in the response."""
82+
7883
content_type: str
7984
"""
8085
Defaults to ``text/plain`` if not set.
@@ -180,7 +185,8 @@ def send(
180185
@staticmethod
181186
def _check_file_path_is_valid(file_path: str) -> bool:
182187
"""
183-
Checks if ``file_path`` is valid.
188+
Checks if ``file_path`` does not contain backslashes or parent directory references.
189+
184190
If not raises error corresponding to the problem.
185191
"""
186192

adafruit_httpserver/route.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,6 @@ def route_func(request, my_parameter):
120120
request.path == "/example/123" # True
121121
my_parameter == "123" # True
122122
"""
123-
if not self._routes:
124-
return None
125-
126123
found_route, _route = False, None
127124

128125
for _route in self._routes:

adafruit_httpserver/server.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def route(self, path: str, methods: Union[str, List[str]] = GET) -> Callable:
5353
Decorator used to add a route.
5454
5555
:param str path: URL path
56-
:param str method: HTTP method: `"GET"`, `"POST"`, etc.
56+
:param str methods: HTTP method(s): ``"GET"``, ``"POST"``, ``["GET", "POST"]`` etc.
5757
5858
Example::
5959
@@ -227,9 +227,8 @@ def _handle_request(self, request: Request, handler: Union[Callable, None]):
227227

228228
def poll(self):
229229
"""
230-
Call this method inside your main event loop to get the server to
231-
check for new incoming client requests. When a request comes in,
232-
the application callable will be invoked.
230+
Call this method inside your main loop to get the server to check for new incoming client
231+
requests. When a request comes in, it will be handled by the handler function.
233232
"""
234233
try:
235234
conn, client_address = self._sock.accept()
@@ -255,10 +254,10 @@ def poll(self):
255254
return
256255
raise
257256

258-
def restrict_access(self, auths: List[Union[Basic, Bearer]]) -> None:
257+
def require_authentication(self, auths: List[Union[Basic, Bearer]]) -> None:
259258
"""
260-
Restricts access to the whole ``Server``.
261-
It applies to all routes and files in ``root_path``.
259+
Requires authentication for all routes and files in ``root_path``.
260+
Any non-authenticated request will be rejected with a 401 status code.
262261
263262
Example::
264263

adafruit_httpserver/status.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,33 @@ def __eq__(self, other: "Status"):
3232

3333

3434
OK_200 = Status(200, "OK")
35-
"""200 OK"""
35+
36+
CREATED_201 = Status(201, "Created")
37+
38+
ACCEPTED_202 = Status(202, "Accepted")
3639

3740
NO_CONTENT_204 = Status(204, "No Content")
38-
"""204 No Content"""
41+
42+
PARTIAL_CONTENT_206 = Status(206, "Partial Content")
3943

4044
TEMPORARY_REDIRECT_307 = Status(307, "Temporary Redirect")
41-
"""307 Temporary Redirect"""
4245

4346
PERMANENT_REDIRECT_308 = Status(308, "Permanent Redirect")
44-
"""308 Permanent Redirect"""
4547

4648
BAD_REQUEST_400 = Status(400, "Bad Request")
47-
"""400 Bad Request"""
4849

4950
UNAUTHORIZED_401 = Status(401, "Unauthorized")
50-
"""401 Unauthorized"""
5151

5252
FORBIDDEN_403 = Status(403, "Forbidden")
53-
"""403 Forbidden"""
5453

5554
NOT_FOUND_404 = Status(404, "Not Found")
56-
"""404 Not Found"""
5755

5856
METHOD_NOT_ALLOWED_405 = Status(405, "Method Not Allowed")
59-
"""405 Method Not Allowed"""
57+
58+
TOO_MANY_REQUESTS_429 = Status(429, "Too Many Requests")
6059

6160
INTERNAL_SERVER_ERROR_500 = Status(500, "Internal Server Error")
62-
"""500 Internal Server Error"""
6361

6462
NOT_IMPLEMENTED_501 = Status(501, "Not Implemented")
65-
"""501 Not Implemented"""
6663

6764
SERVICE_UNAVAILABLE_503 = Status(503, "Service Unavailable")
68-
"""503 Service Unavailable"""

0 commit comments

Comments
 (0)