Package: drakaina
drakaina{width=200px height=205px}❗ WIPModule for simple RPC service implementationQuickstartDrakaina may be installed viapipand requires Python 3.7 or higher :pipinstalldrakainaA minimal Drakaina example is:fromdrakainaimportremote_procedurefromdrakaina.wsgiimportWSGIHandler@remote_proceduredefmy_method():return"Hello Bro! ✋️"@remote_procedure(name="something.get")defget_some_string():return"You called `something.get`."@remote_procedure(provide_request=True)defdo_something_with_environ(request):returnf"You called `do_something_with_environ`. Request:{request}."@remote_procedure()deftell_the_middleware_something():return"You called `tell_the_middleware_something`. It has a some extra conditions."asyncdefasynchronous_procedure():awaitasyncio.sleep(5)return"Ding-Dong 🔔!"""">>> JsonRPCv2().handle({"jsonrpc": "2.0", "method": "my_method", "id": 1})or define WSGI application"""app=WSGIHandler(route="/jrpc")Drakaina may be ran with any WSGI-compliant server,
such asGunicorn.gunicornmain:appFeaturesSerializers layer.json,orjson,ujsonandmsgpackserializers.login_requiredandcheck_permissionsdecorators.WSGI protocol implementationCORS middlewareJWT Authorization middleware.Compatible with simple middlewares for others wsgi-frameworks,
like asWerkzeug,FlaskDocumentationInstallationpipinstalldrakainaMiddlewaresCORSJWTDrakaina may be installed viapipand requires Python 3.7 or higher :pipinstalldrakaina[jwt]A minimal Drakaina example is:fromfunctoolsimportpartialfromdrakainaimportENV_IS_AUTHENTICATEDfromdrakainaimportENV_USER_IDfromdrakainaimportremote_procedurefromdrakainaimportcheck_permissionsfromdrakainaimportlogin_requiredfromdrakainaimportmatch_anyfromdrakaina.contrib.jwt.middlewareimportJWTAuthenticationMiddlewarefromdrakaina.wsgiimportWSGIHandlerimportuser_store@login_required@remote_procedure(provide_request=True)defmy_method(request):assertrequest[ENV_IS_AUTHENTICATED]returnf"Hello Bro ✋! Your ID={request[ENV_USER_ID]}"@check_permissions(["user_read","user:admin","username:johndoe"],match_any)@remote_proceduredefmy_method():return"Hello Bro! ✋️"defget_user(request,payload):user_id=request[ENV_USER_ID]orpayload["user_id"]returnuser_store.get(id=user_id)defget_jwt_scopes(request,payload):# here `scp` is the key for the scopes value in the token payloadreturnpayload.get("scp")app=WSGIHandler(middlewares=[partial(JWTAuthenticationMiddleware,secret_phrase="_secret_",credentials_required=True,auth_scheme="Bearer",# token_getter=custom_implementation_get_token,user_getter=get_user,scopes_getter=get_jwt_scopes,# revoke_checker=is_revoked,)])Using with DjangoCreate filerpc_views.pyin your django application.
Define function and wrap itremote_proceduredecorator:fromdrakainaimportremote_procedure@remote_proceduredefmy_method():return"Hello, Django Bro! ✋"AddRPCViewclass to urlpatterns. Theas_viewmethod
must accept theautodiscoverargument as the name of
the remote procedure files.fromdjango.urlsimportpathfromdrakaina.contrib.django.viewsimportRPCViewurlpatterns=[...,path("api/",RPCView.as_view(autodiscover="rpc_views")),]JWT Authentication in your Django projectWrap an instance ofRPCViewwith theJWTAuthenticationMiddleware.fromdjango.urlsimportpathfromdrakaina.contrib.djangoimportRPCView,JWTAuthenticationMiddlewareurlpatterns=[...,path("api/",JWTAuthenticationMiddleware(RPCView.as_view(autodiscover="rpc_views"))),]Define the parameters in thesettings.pyfile....DRAKAINA_JWT_SECRET_KEY="__SECRET_KEY__"...LicenseApache License 2.0Artwork"drakaina.png" by Korolko Anastasia is licensed under(CC BY-SA 4.0).