Skip to content
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

feat: add support nested routes prefix and multiple auth handler #889

Closed
wants to merge 5 commits into from

Conversation

yomaaf
Copy link
Contributor

@yomaaf yomaaf commented Jul 10, 2024

Description

This PR fixes #865 #708

This PR adds the ability to nested route and ability multiple auth handler.
The example already added to integration tests

Copy link

vercel bot commented Jul 10, 2024

@yomaaf is attempting to deploy a commit to the sparckles Team on Vercel.

A member of the Team first needs to authorize it.

@yomaaf yomaaf marked this pull request as draft July 11, 2024 04:15
@yomaaf yomaaf marked this pull request as ready for review July 11, 2024 04:15
@yomaaf
Copy link
Contributor Author

yomaaf commented Jul 11, 2024

Hey @sansyrox please check this out. I made some adjustment to enable nested routes

@sansyrox
Copy link
Member

Hey @yomaaf 👋

Thank you for the PR 😄 I will have a look soon. @VishnuSanal , could you have an initial look?

Copy link
Contributor

@VishnuSanal VishnuSanal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@yomaaf
Copy link
Contributor Author

yomaaf commented Jul 12, 2024

Please let me know if this code need some adjustment @sansyrox @VishnuSanal, because I'm also working on #708 issue based on this code. I found out your framework and want to use it on production. But need this nested function and multiple authentication. I read the comment on #708 issue and have different approach. Will PR soon.

@sansyrox
Copy link
Member

Hey @yomaaf 👋

Thanks for the PR. I am with limited access till Sunday. I will do a thorough review by Sunday 😄

Comment on lines 398 to 402
def include_router(self, router, added_routes=None):
"""
The method to include the routes from another router

:param router Robyn: the router object to include the routes from
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need an added_route param here? Could you please the usage in the docstring?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add the typing here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry I want to comment in this conversation but reply on another conversation instead. My mistake.
#889 (comment)

Comment on lines +441 to +442
self.sub_router = []
self.temp_routes = [] # Temporary storage for routes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain the idea behind these memebers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi I'm sorry there is some issue before when register route. I already fix this. Please check again. I also add some feature to handle multiple auth handler like I said in comment before.

Copy link
Contributor Author

@yomaaf yomaaf Jul 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also add some pytest to ensure the function is working

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is we save all the nested subrouter and route before the main robyn call include router. Like what I explain in this comment #889 (comment)

Comment on lines 458 to 460
def get(self, endpoint: str, const: bool = False, auth_required: bool = False):
def inner(handler):
self.add_temp_route(HttpMethod.GET, endpoint, handler, const, auth_required)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, why did you have to refactor the original implementation here?

^^ I am trying to understand you thoughts here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original implementation of the get, post, etc., decorators calls super(), which registers the route when the decorator is used. However, when nested subrouters are used, the decorator has already been called, so the route retains the old prefix instead of adopting the nested prefix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original implementation of the get, post, etc., decorators calls super(), which registers the route when the decorator is used. However, when nested subrouters are used, the decorator has already been called, so the route retains the old prefix instead of adopting the nested prefix.

Could you please add it in comment/docstring in the codebase?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course. I'll add it.

@sansyrox
Copy link
Member

Hey @yomaaf ,

I have a few questions.

* add multiple auth handler
* add basic authorization getter
* add route params auth handler name
@yomaaf yomaaf changed the title feat: add support nested routes prefix feat: add support nested routes prefix and multiple auth handler Jul 13, 2024
Copy link

codspeed-hq bot commented Jul 13, 2024

CodSpeed Performance Report

Merging #889 will not alter performance

Comparing yomaaf:feature/nested-route (4d2e77d) with main (5e7bcbe)

Summary

✅ 110 untouched benchmarks

🆕 20 new benchmarks

Benchmarks breakdown

Benchmark main yomaaf:feature/nested-route Change
🆕 test_invalid_authentication_header_basic[async] N/A 4.7 ms N/A
🆕 test_invalid_authentication_header_basic[sync] N/A 4.7 ms N/A
🆕 test_invalid_authentication_header_bearer_2[async] N/A 4.7 ms N/A
🆕 test_invalid_authentication_header_bearer_2[sync] N/A 4.7 ms N/A
🆕 test_invalid_authentication_no_token_basic[async] N/A 4.7 ms N/A
🆕 test_invalid_authentication_no_token_basic[sync] N/A 4.7 ms N/A
🆕 test_invalid_authentication_no_token_bearer_2[async] N/A 4.7 ms N/A
🆕 test_invalid_authentication_no_token_bearer_2[sync] N/A 4.7 ms N/A
🆕 test_invalid_authentication_token_basic[async] N/A 4.7 ms N/A
🆕 test_invalid_authentication_token_basic[sync] N/A 4.7 ms N/A
🆕 test_invalid_authentication_token_bearer_2[async] N/A 4.7 ms N/A
🆕 test_invalid_authentication_token_bearer_2[sync] N/A 4.7 ms N/A
🆕 test_nested_router_invalid_authentication_header N/A 4.9 ms N/A
🆕 test_nested_router_invalid_authentication_no_token N/A 4.9 ms N/A
🆕 test_nested_router_invalid_authentication_token N/A 4.9 ms N/A
🆕 test_nested_router_valid_authentication N/A 5 ms N/A
🆕 test_valid_authentication_basic[async] N/A 4.8 ms N/A
🆕 test_valid_authentication_basic[sync] N/A 4.8 ms N/A
🆕 test_valid_authentication_bearer_2[async] N/A 4.8 ms N/A
🆕 test_valid_authentication_bearer_2[sync] N/A 4.8 ms N/A

@sansyrox
Copy link
Member

Hey @yomaaf 👋

Thanks for the PR but this PR is doing a lot of things 😅

Could you split the PR in two parts

  • Add support for nested routes in one
  • Add multiple auth handlers in the other

I am recommending the above as it will take a lot of time to get it landed 😄

@yomaaf
Copy link
Contributor Author

yomaaf commented Jul 14, 2024

Hi, @sansyrox.

I will do that. I'll separate the PR, but maybe not today. Will do in a few days because I need to catch up on another project. In the meantime, I'll create my own branch in my repository and use pip install from it till the PRs land. I'm going to push it as soon as possible. Thanks

@yomaaf yomaaf closed this Jul 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Questions about Nested SubRouter
3 participants