-
Notifications
You must be signed in to change notification settings - Fork 32
Feature: URL Parameters #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8e2967b
Changed how routes are mapped to handler functions, added _HTTPRoutes
michalpokusa c95290f
Replaced route_handlers dict with _HTTPRoutes object in HTTPServer
michalpokusa 78cfa0e
Updated examples and added new ones hat present added functionality
michalpokusa 030390a
Fix: Prevent creating empty query param
michalpokusa cf69035
Removed old deprecation error
michalpokusa e2a4761
Made variable name and docstring more verbose
michalpokusa 813c532
Optimization, reduced number of re.match calls per request
michalpokusa 994a7e8
Minor changes to repr of _HTTPRoute and _HTTPRoutes
michalpokusa 8f516cd
Replaced NotImplementedError in example with prints
michalpokusa 4fb06a0
Fix to CI
michalpokusa d70e2c5
Updated README.rst
michalpokusa e6ad4d0
Minor changes in docs for examples
michalpokusa d5817b0
Refactor for removing the need for saving last match group
michalpokusa 7ddc32a
Removed unnecessary indentation in docstring example
michalpokusa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# SPDX-FileCopyrightText: 2022 Dan Halbert for Adafruit Industries | ||
# | ||
# SPDX-License-Identifier: Unlicense | ||
|
||
import secrets # pylint: disable=no-name-in-module | ||
|
||
import socketpool | ||
import wifi | ||
|
||
from adafruit_httpserver.mime_type import MIMEType | ||
from adafruit_httpserver.request import HTTPRequest | ||
from adafruit_httpserver.response import HTTPResponse | ||
from adafruit_httpserver.server import HTTPServer | ||
|
||
|
||
ssid, password = secrets.WIFI_SSID, secrets.WIFI_PASSWORD # pylint: disable=no-member | ||
|
||
print("Connecting to", ssid) | ||
wifi.radio.connect(ssid, password) | ||
print("Connected to", ssid) | ||
|
||
pool = socketpool.SocketPool(wifi.radio) | ||
server = HTTPServer(pool) | ||
|
||
|
||
class Device: | ||
def turn_on(self): | ||
raise NotImplementedError | ||
|
||
def turn_off(self): | ||
raise NotImplementedError | ||
|
||
|
||
def get_device(device_id: str) -> Device: # pylint: disable=unused-argument | ||
""" | ||
This is a **made up** function that returns a `Device` object. | ||
""" | ||
return Device() | ||
|
||
|
||
@server.route("/device/<device_id>/action/<action>") | ||
@server.route("/device/emergency-power-off/<device_id>") | ||
def perform_action(request: HTTPRequest, device_id: str, action: str = None): | ||
""" | ||
Performs an "action" on a specified device. | ||
""" | ||
|
||
device = get_device(device_id) | ||
|
||
if action == "turn_on": | ||
device.turn_on() | ||
elif action == "turn_off" or action is None: | ||
device.turn_off() | ||
|
||
with HTTPResponse(request, content_type=MIMEType.TYPE_TXT) as response: | ||
response.send(f"Action ({action}) performed on device with ID: {device_id}") | ||
|
||
|
||
@server.route("/something/<route_param_1>/<route_param_2>") | ||
def different_name_parameters( | ||
request: HTTPRequest, | ||
handler_param_1: str, # pylint: disable=unused-argument | ||
handler_param_2: str = None, # pylint: disable=unused-argument | ||
): | ||
""" | ||
Presents that the parameters can be named anything. | ||
|
||
``route_param_1`` -> ``handler_param_1`` | ||
``route_param_2`` -> ``handler_param_2`` | ||
""" | ||
|
||
with HTTPResponse(request, content_type=MIMEType.TYPE_TXT) as response: | ||
response.send("200 OK") | ||
|
||
|
||
print(f"Listening on http://{wifi.radio.ipv4_address}:80") | ||
server.serve_forever(str(wifi.radio.ipv4_address)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this function intended to have two
@server.route()
s on it?I tried testing it as was able to get successfull responses from
/device/<device_id>/action/<action>
But I just get 'connection timed out' error with no real response when I try
/device/emergency-power-off/<device_id>
. I'm guessing it may have been leftover from some testing durrent development and can be removed? Or if it does actually belong something may need tweaked in order to make it functional.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for testing it 👍
Yes, this is intentional, not a leftover. I did it to show that it is possible to have multiple routes for single handler, like Flask.
I also test it on ESP32-S2 TFT, including the
/device/emergency-power-off/<device_id>
.My guess is, that error you are encountering is in fact due to the
action
defaulting toNone
, thus entering secondif
withdevice.turn_off()
which is raisingNotImplementedError
and not returning properHTTPResponse
.Please check if that is the case,
and if yes do you think it should be changed?(I updated the example to useprint
instead ofNotImplementedError
)It is only an example but I agree that it might be confusing.