Comments (1)
Idea - simply add context argument to every implementation of http method calls
Implementation:
Implementation would require modifying every HTTP method handler in the resource.mixins
module. Example:
class ListMixin(BaseMixin):
...
def on_get(self, req, resp, handler=None, **kwargs):
kwargs['context'] = req.context
self.handle(
handler or self.list, req, resp, **kwargs
)
As the effect developer may use three methods to provide custom per-request context variables:
-
using HTTP method overrides
class MyResource(ListAPI): def on_get(req, resp, **kwargs): req.context['session'] = Session() super().on_get(req, resp, **kwargs) def list(self, params, meta, context, **kwargs): # context object content available here ...
-
using middleware
class SessionMiddleware: def process_resource(self, req, resp, resource, params): req.context['session'] = Session()
-
using hooks:
def create_session(req, resp, resource, params): req.context['session'] = Session() @falcon.before(create_session) class MyResource(ListAPI): def on_get(req, resp, **kwargs): req.context['session'] = Session() super().on_get(req, resp, **kwargs) def list(self, params, meta, context, **kwargs): # context object content available here ...
Pros of this approach are:
- simple implementation
- low risk of breaking backwards incompatibility
- compatible with existing techniques of providing context in falcon
- compatible with any existing falcon extension built using hooks or middleware
Also if we improve support for falcon hooks (per-method falcon hooks with falcon.before
) this solution will still work as expected.
Backward compatibility concerns:
Good practice is to allow passing any set of keyword arguments to resource manipulation methods using generic and basic resources in graceful. Anyway we cannot be sure that every developer uses this approach. We should expect that graceful users sometimes do following:
class MyResourceList(ListAPI):
def list(params, meta):
pass
# or
class MyResourceItem(RetrieveAPI):
def retrieve(params, meta, object_id): # <= object id from URI template
pass
We could ensure backwards compatibility by using class keyword arguments:
class MyResourceItemWithCtx(RetrieveAPI, with_context=True):
def retrieve(params, meta, context, **kwargs):
pass
class MyResourceItemWithoutCtx(RetrieveAPI, with_context=False):
def retrieve(params, meta, **kwargs):
pass
This is feature of Python 3 but we do not target anything below Python 3.3. Of course default for with_context
for 0.x
branch would be False
and for 1.x
would be True
from graceful.
Related Issues (20)
- Consider getting rid of default param value in "raw" or at least deserialising it only once HOT 3
- Problem: incompatibility with falcon hooks
- Add docs section about mixins
- Add missing "version added" and "version changed" specifiers for all previous features and releases
- librelist doesnt seem to work HOT 1
- Rewrite tests in test_generic to pytest-style HOT 1
- 405 when using PUT method HOT 1
- Field validation error uses field source names instead of representation names.
- Writable *-sourced fields break object validation on POST/PUT/PATCH HOT 2
- Support for star-like writable fields HOT 1
- Add "write_only" fields
- Improperly handled fields with many=True
- Better validation error representation.
- Mark all keyword arguments in BaseField as keyword only
- Auth: allow to set detailed authorization requirements for specific HTTP methods
- Revisit terminology and class names.
- Support for serializer's method fields.
- A StringField's value become "None" instead of None when client submit it as null. HOT 5
- Support for OpenApi Generator's graceful generator? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graceful.