1
0
This commit is contained in:
2024-01-15 20:20:10 +00:00
parent c75ea4ea9d
commit 38cd64ea9a
87 changed files with 3946 additions and 2040 deletions

View File

@@ -1,12 +1,14 @@
# -*- coding: utf-8 -*-
# django-jsonrpc-aiohttp | (c) 2022-present Tomek Wójcik | MIT License
# bthlabs-jsonrpc-aiohttp | (c) 2022-present Tomek Wójcik | MIT License
import asyncio
import datetime
import logging
import os
import sys
from aiohttp import web
from bthlabs_jsonrpc_core import register_method
from bthlabs_jsonrpc_core.ext.jwt import ALGORITHMS, HMACKey, JWTCodec, KeyPair
from bthlabs_jsonrpc_aiohttp import JSONRPCView
@@ -20,12 +22,12 @@ formatter = logging.Formatter(
handler.setFormatter(formatter)
logger.addHandler(handler)
app_logger = logging.getLogger('bthlabs_jsonrpc_aiohttp_example.app')
jsonrpc_logger = logger = logging.getLogger('bthlabs_jsonrpc')
jsonrpc_logger = logging.getLogger('bthlabs_jsonrpc')
jsonrpc_logger.setLevel(logging.DEBUG)
jsonrpc_logger.addHandler(handler)
app_logger = logging.getLogger('bthlabs_jsonrpc_aiohttp_example.app')
async def app_on_startup(app):
logger.info('BTHLabs JSONRPC aiohttp integration example')
@@ -43,18 +45,30 @@ async def async_test(request, delay):
return 'It works!'
@register_method('hello', namespace='example')
@register_method('hello', namespace='jwt')
async def hello_example(request):
return 'Hello, Example!'
class JWTRPCView(JSONRPCView):
async def get_codec(self, request):
return JWTCodec(
KeyPair(
decode_key=HMACKey('thisisntasecrurekeydontuseitpls=', ALGORITHMS.HS256),
encode_key=HMACKey('thisisntasecrurekeydontuseitpls=', ALGORITHMS.HS256),
),
issuer='bthlabs_jsonrpc_aiohttp_example',
ttl=datetime.timedelta(seconds=3600),
)
def create_app(loop=None):
app = web.Application(logger=app_logger, loop=loop)
app.on_startup.append(app_on_startup)
app.add_routes([
web.post('/rpc', JSONRPCView()),
web.post('/example/rpc', JSONRPCView(namespace='example')),
web.post('/jwt/rpc', JWTRPCView(namespace='jwt')),
])
return app

View File

@@ -1,3 +1,3 @@
#!/bin/bash
# django-jsonrpc-aiohttp | (c) 2022-present Tomek Wójcik | MIT License
# bthlabs-jsonrpc-aiohttp | (c) 2022-present Tomek Wójcik | MIT License
exec adev runserver example.py