{"id":13802033,"url":"https://github.com/zcattacz/ujrpc","last_synced_at":"2025-05-13T12:32:19.250Z","repository":{"id":162058705,"uuid":"636666724","full_name":"zcattacz/ujrpc","owner":"zcattacz","description":"json rpc for micropython","archived":false,"fork":false,"pushed_at":"2023-05-31T03:20:56.000Z","size":23,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-04-22T12:33:32.172Z","etag":null,"topics":["json","micropython","rpc"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zcattacz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-05-05T11:16:47.000Z","updated_at":"2024-04-14T21:12:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"9aea7915-479a-41a6-93d4-ca135afac664","html_url":"https://github.com/zcattacz/ujrpc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcattacz%2Fujrpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcattacz%2Fujrpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcattacz%2Fujrpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcattacz%2Fujrpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zcattacz","download_url":"https://codeload.github.com/zcattacz/ujrpc/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225217939,"owners_count":17439712,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","micropython","rpc"],"created_at":"2024-08-04T00:01:33.687Z","updated_at":"2024-11-18T17:30:46.074Z","avatar_url":"https://github.com/zcattacz.png","language":"Python","funding_links":[],"categories":["Libraries"],"sub_categories":["Communications"],"readme":"ujrpc \n=============\n\nAdapted from [SimpleJsonRpc](https://github.com/moritz-wundke/simplejsonrpc) , tested on Micropython ESP32, ESP32-S2, CPython 3.9\n\nChanges:\n----\n- adapted for micropython\n- fix handling of error, parameter and batched request\n- shorten code to save space\n- add asyncio support\n- removed __call__(), use handle_rpc(request)/handle_rpca(request) instead\n\nSimple JSON-RPC 2.0 compilant middleware for python using decorators. With just a few lines of code you get a JSONRPC 2.0 compilant web API running and ready for your needs!\n\nInstall on Micropython\n----\n\ncopy ujrpc.py to micropython:\n\n```sh\nmpremote fs cp ujrpc.py :/\n```\n\nUsage\n----\n\nJust create a service class to store your API calls and decorate your methods:\n\n```python\nfrom ujrpc import JRPCService\n\njrpc = JRPCService(api_version=1)\n\n# jrpc.debug = False\n\n@jrpc.fn(name='add', doc='test1: add')\ndef add(r, a, b):\n    return a + b\n\n@jrpc.fn(name='echo', doc='test2: echo')\ndef echo(r, msg):\n    return msg\n\n@jrpc.fn(name='login', doc='Method used to log a user in')\ndef login(r, user_name, user_pass):\n    (...)\n\nrequests = \"\"\"[\n{\"jsonrpc\": \"2.0\",  \"method\": \"add\", \"params\": [11,22], \"id\": 1},\n{\"jsonrpc\": \"2.0\",  \"method\": \"echo\", \"params\": [\"Hello\"], \"id\": 2},\n]\"\"\"\n\n# in mqtt_as callbacks or picoweb route handlers:\nresponse = jrpc.handle_rpc(requests)\n\nprint(response)\n# [{\"jsonrpc\": \"2.0\", \"id\": 1, \"result\": 33}, \n#  {\"jsonrpc\": \"2.0\", \"id\": 2, \"result\": \"Hello\"}]\n\ntry:\n    import uasyncio as asyncio\nexcept:\n    import asyncio\n\nclass TestAsync():\n    def __init__(self):\n        self.cal_cnt = 0\n    @jrpc.fn(name=\"async_add\")\n    async def add(self, a, b):\n        self.cal_cnt += 1\n        return a+b\n    @jrpc.fn(name=\"product\")\n    def prd(self, a, b):\n        self.cal_cnt += 1\n        return a*b\n\nrequests = \"\"\"[\n{\"jsonrpc\": \"2.0\",  \"method\": \"async_add\", \"params\": [11,22], \"id\": 1},\n{\"jsonrpc\": \"2.0\",  \"method\": \"product\", \"params\": [33,44], \"id\": 2},\n]\"\"\"\nasync def main():\n    ta = TestAsync()\n    jrpc.bind_self = ta # pass ta as 1st parameter to decorated fn, instead of jrpc\n    print(\"cal_cnt=\", ta.cal_cnt)\n    response = await jrpc.handle_rpca(requests)\n    print(response)\n    print(\"cal_cnt=\", ta.cal_cnt)\n\nasyncio.run(main())\n\n# cal_cnt= 0\n# [{\"jsonrpc\": \"2.0\", \"id\": 1, \"result\": 33}, {\"jsonrpc\": \"2.0\", \"id\": 2, \"result\": 1452}]\n# cal_cnt= 2\n\n```\n\n\nLicense\n----\n\nThe MIT License (MIT)\n\nCopyright (c) 2014 Moritz Wundke\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzcattacz%2Fujrpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzcattacz%2Fujrpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzcattacz%2Fujrpc/lists"}