Git Product home page Git Product logo

Comments (8)

bcb avatar bcb commented on May 28, 2024

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.

bcb avatar bcb commented on May 28, 2024

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.

bcb avatar bcb commented on May 28, 2024

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.

semirook avatar semirook commented on May 28, 2024

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.

bcb avatar bcb commented on May 28, 2024

Ok, I'll add the ability to pass extra data through dispatch.

from jsonrpcserver.

bcb avatar bcb commented on May 28, 2024

I'm at Pycon this weekend, but I will get to this next week

from jsonrpcserver.

semirook avatar semirook commented on May 28, 2024

Thanks a lot!

from jsonrpcserver.

bcb avatar bcb commented on May 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.