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

Add utils to build web servlets #11

Closed
wants to merge 3 commits into from
Closed

Conversation

babolivier
Copy link

@babolivier babolivier commented Dec 3, 2021

Lifted almost verbatim from https://github.com/matrix-org/sydent/blob/main/sydent/http/servlets/__init__.py

The reason behind this PR is that I'm looking at rewriting https://github.com/matrix-org/matrix-content-scanner in Python and I realised a lot of code to help creating and managing web servlets lives in Sydent, and I'd rather move it in here than copy it over to a new project.

I haven't added tests, mostly because I'm not sure how to test this kind of things.

@babolivier babolivier requested a review from a team as a code owner December 3, 2021 16:10
@clokep
Copy link
Member

clokep commented Dec 3, 2021

Would it make more sense to use the JsonResource (which has a wholeeeeee bunch of other stuff it depends on, unfortunately) from Synapse instead? I'm much more confident that code works properly than what's in Sydent.

@babolivier
Copy link
Author

babolivier commented Dec 3, 2021

Would it make more sense to use the JsonResource (which has a wholeeeeee bunch of other stuff it depends on, unfortunately) from Synapse instead? I'm much more confident that code works properly than what's in Sydent.

Not sure, I feel like Synapse's JsonResource might be a bit overengineered for just a use case of "I want to make a servlet that renders JSON".

@clokep
Copy link
Member

clokep commented Dec 3, 2021

Yeah, that's fair. I guess I like the way _AsyncResource works more than using the decorators? Not a big deal though.


def json_servlet_async(fn: Callable[..., JsonDict]) -> Callable[..., int]:
async def render(
fn: Callable[..., Any], self: Any, request: Request, **kwargs: Any
Copy link
Contributor

Choose a reason for hiding this comment

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

We await and json encode the return from fn below, so we probably want it to return Awaitable[JsonDict]?

Suggested change
fn: Callable[..., Any], self: Any, request: Request, **kwargs: Any
fn: Callable[..., Awaitable[JsonDict]], self: Any, request: Request, **kwargs: Any

Copy link
Contributor

Choose a reason for hiding this comment

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

fn: The handler to run.
self: The current object.
request: The request to process.
args: The arguments to pass to the function.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the *args argument missing?


class MatrixRestError(Exception):
"""
Handled by the jsonwrap wrapper. Any servlets that don't use this
Copy link
Contributor

Choose a reason for hiding this comment

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

Does jsonwrap refer to the decorators below?

Copy link
Author

Choose a reason for hiding this comment

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

Err, yes, I forgot to rename it here.

@babolivier
Copy link
Author

After having progressed a bit more into the content scanner rewrite, I'm not sure this is how I want it this to look like. I'll revisit once I'm back from holidays.

@babolivier
Copy link
Author

Closing this as we'll probably want to go another way.

@babolivier babolivier closed this Apr 26, 2022
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.

4 participants