-
-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In this update, I propose an enhancement to annotate endpoints with their accepted request types, such as "POST", "GET", "PUT", and others. The intention is to ensure appropriate handling of errors when the incoming request method does not match the expected method for an endpoint. If an endpoint lacks annotations, it defaults to accepting any request method. It's important to note that the current version of Responder only supports this functionality within class-based views. For instance, leveraging the annotation approach: `@api.route("/", methods=["POST"])` This enhancement streamlines the handling of request methods, promoting more explicit endpoint definition and ensuring accurate request handling within the Responder framework. See other examples in the `examples/enpoint_request_methods.py` file. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Summary by CodeRabbit - **New Features** - Expanded API capabilities with new route handlers for various HTTP methods. - Introduced asynchronous request handling for improved performance. - Implemented a `GreetingResource` class to manage greeting-related routes. - **Enhancements** - Updated the `add_route` function to support specifying HTTP methods. - **Tests** - Added tests for new endpoint request methods and route handling. - **Documentation** - Adjusted documentation to reflect the new usage of HTTP methods in route declarations. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
4 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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,33 @@ | ||
import responder | ||
|
||
api = responder.API() | ||
|
||
|
||
@api.route("/{greeting}") | ||
async def greet(req, resp, *, greeting): # any request method. | ||
resp.text = f"{greeting}, world!" | ||
|
||
|
||
@api.route("/me/{greeting}", methods=["POST"]) | ||
async def greet_me(req, resp, *, greeting): | ||
resp.text = f"POST - {greeting}, world!" | ||
|
||
|
||
@api.route("/class/{greeting}") | ||
class GreetingResource: | ||
def on_get(self, req, resp, *, greeting): | ||
resp.text = f"GET class - {greeting}, world!" | ||
resp.headers.update({"X-Life": "42"}) | ||
resp.status_code = api.status_codes.HTTP_201 | ||
|
||
def on_post(self, req, resp, *, greeting): | ||
resp.text = f"POST class - {greeting}, world!" | ||
resp.headers.update({"X-Life": "42"}) | ||
|
||
def on_request(self, req, resp, *, greeting): # any request method. | ||
resp.text = f"any class - {greeting}, world!" | ||
resp.headers.update({"X-Life": "42"}) | ||
|
||
|
||
if __name__ == "__main__": | ||
api.run() |
This file contains 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 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 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