Comments (8)
Hi @john-g-g,
If you were using aiohttp's class-based views, you'd get the aiohttp context within your jsonrpc method:
class MyView(web.View):
@methods.add
async def ping(self):
aio_http_request = self.request
statsd = self.request.app.config['statsd_client']
# ...
async def post(self):
request = await self.request.text()
response = await methods.dispatch(request)
return web.json_response(response)
app.router.add_route('*', '/', MyView)
However I've just tried this and it doesn't work right now, but if I fix that would it suit your situation?
from jsonrpcserver.
Failing that your idea sounds good, my only suggestion would be to pass the values in an object like:
context = {'request': aio_http_request, 'statsd': statsd}
response = await methods.dispatch(jsonrpc_request, context=context)
And methods would receive the context object:
@methods.add
async def ping(context):
statsd = context['statsd']
# ...
from jsonrpcserver.
I was able to access the aiohttp values in my method like this:
from aiohttp import web
from jsonrpcserver.aio import dispatch
from jsonrpcserver.response import NotificationResponse
class MyView(web.View):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.methods = (self.ping,)
async def ping(self):
request = self.request
# ...
return 'pong'
async def post(self):
request = await self.request.text()
response = await dispatch(self.methods, request)
if isinstance(response, NotificationResponse):
return web.Response()
else:
return web.json_response(response)
app = web.Application()
app.router.add_route('*', '/', MyView)
if __name__ == '__main__':
web.run_app(app, port=5000)
from jsonrpcserver.
The best I can do currently is this workaround:
async def rpc_handler(request):
raw_request = await request.text()
json_request = json.loads(raw_request)
json_request.setdefault('params', {}).update({'request': request})
response = await methods.dispatch(json_request)
return web.json_response(response)
And then in every method I have access to the original request object. It works but I don't like it.
@methods.add
async def my_method(request):
pass
from jsonrpcserver.
Ok, I'll add the ability to pass extra data through dispatch
.
from jsonrpcserver.
I'm at Pycon this weekend, but I will get to this next week
from jsonrpcserver.
Thanks a lot!
from jsonrpcserver.
Ok, I've released a new version (3.5.0) which allows you to pass context through to the methods.
Here's how your code would look @semirook
@methods.add
async def my_method(context=None):
# Use request here...
pass
async def rpc_handler(request):
raw_request = await request.text()
response = await methods.dispatch(raw_request, context=request)
if response.is_notification:
return web.Response()
else:
return web.json_response(response)
Still, class-based views are a better option if you can use them. (see above)
from jsonrpcserver.
Related Issues (20)
- Update jsonschema to >=4 HOT 1
- Change build system interface to pyproject.toml HOT 1
- Support for custom parameters' validators HOT 2
- Use custom logger instead of logging.root HOT 1
- Remove package_data from setup.py
- Paramterize the unit tests HOT 1
- Add assert_called_once_with to mocking unit tests
- Add test section to extras_require in setup.py
- (Version 6) Adjust required Python version HOT 1
- Add installation instruction to README
- Upgrade pre-commit hooks
- Enable all Pylint errors
- Type check tests and examples
- Use importlib.resources instead of "open" in setup.py HOT 1
- method decorator does not work with custom dict anymore in v5.0.9
- Change linter from Pylint to Ruff HOT 1
- Blocking task with status callbacks HOT 2
- Always shut down server when using serve()
- The serve() method will get stuck HOT 1
- More high-level tools 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 jsonrpcserver.