HTTPEndpoint Question

Hi,

I create a REST-API with starlette and just discoverd HTTPEndpoint to use for a route that will have 4 http-Methods. So I thougth it would be cleaner to use an HTTPEndpoint-Class to do so. What I´m trying to do is actually just some CRUDing so I thought I can put the code, that fetches objects from the storage somehow seperate and reuse it in alle 4 methods (get, post, put, delete). All the fetching is done in a try/except block witch is returning some 4xx status-codes if the request IDs are not there or malformed:

try:
    self.id = UUID(request.path_params['nodeID'])
except ValueError:
    return Response(status_code=400)
try:
    self.node = tree.node(self.id)
except KeyError:
    return Response(status_code=404)

But with this solution I need to call this code somehow through the starlette.endpoints.HTTPEndpoint.dispatcher so that the returning Response-Objects get actually executed or raise HTTPException which I need to catch again.

What I had in mind fist is to overright __init__() and do all this object-fetching there but __init__() must not retrun the Respons-Obj neither.

So how is it ment to work within a HTTPEndpoint class to share some code between the http-method functions?

I just digged deeper and found out, that I had a mistake when setting up the ExceptionMiddleware and that was why raising the HTTPException did´t work.

But anyway, not sure if overwriting __init_() is best practice here:

class NodeEndpoint(HTTPEndpoint):

    def __init__(self, scope, receive, send):
        super().__init__(scope, receive, send)
        request = Request(self.scope, receive=self.receive)
        try:
            self.id = UUID(request.path_params['nodeID'])
        except ValueError:
            raise HTTPException(status_code=400)
        try:
            self.node = tree.node(self.id)
        except KeyError:
            raise HTTPException(status_code=404)

But now the request is created two times.

  1. in my overwritten __init__()
  2. in the HTTPEndpoint.dispatcher() function

Maybe it would make sense to create self.response within HTTPEndpoint.__init__() which is usable inside the Endpoint-Obj ?
Any thougths on that?

Thanks