Show community: starlette-cbge - Generic endpoints for `starlette` that bind schema back-ends

I started drafting a library (add-on, extension) that should fill some gaps I faced.

The main idea is to have a high-level generic endpoint based on the starlette.endpoints.HTTPEndpoint that bind different schema back-ends over a generic interface and provides automatic schema generation.
That cover the request-response cycle flow, such as

  • retrieving the request data (path and query params, payload), deserialization
  • shaping the request data
  • getting additional context if required
  • validating the request data against the request schema
  • validating the action itself (such as for 403, 404, 409, etc.)
  • performing the action itself
  • serialization of the response data against the response schema
  • errors responses serialization for handled exceptions.

Every step is represented by separate functions that can be overridden in the particular project base classes to meet particular needs.
That should be also a base for more specific generic endpoints like “CollectionEndpoint”, “ItemEndpoint”.

Serialization/Deserialization is hooked over a generic interface that relies on the schema back-end implementation against the defined interface. That should also allow to easily add a custom back-end if it’s not currently covered by the library or override the current behavior if needed.

The initial target is to cover pydantic, typesystem and marshmallow and all of those have quite a fair of difference in the approaches, so the additional layer (over the interface) seems to be unavoidable.

The schema generator extends the starlette.schemas.SchemaGenerator and also calls the method defined in the schema back-end.

It also has the potential to cover common db ops across more specific endpoints, but it’s a bit early and arguable for that and might be better done with orm.

It’s quite a few birds for one stone to cover, so there might be some trade-offs.
As a draft, it’s pretty rough and ugly at the moment but highlights the whole idea and the example app is included - https://github.com/gvbgduh/starlette-cbge/tree/master/example_app.

It’s also my first serious open-source library and I want to put a certain amount of diligence in it, and while I’m at the beginning, I’d love to gather some comments, advice about the whole idea in general and internals architecture/implementations as well if it seems interesting and can be of any help.

So, any feedback would be much appreciated.

2 Likes