Skip to content

Commit

Permalink
Merge pull request #31 from wegroupwolves/feature/jsonrpc-request-id-…
Browse files Browse the repository at this point in the history
…as-ctx-var

Be able to retrieve the jsonrpc id 'out of thin air'.
  • Loading branch information
sevaho authored Aug 2, 2024
2 parents 96cddd2 + e796564 commit e6cdc5f
Show file tree
Hide file tree
Showing 7 changed files with 740 additions and 593 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ jobs:
strategy:
matrix:
# python-version: ["3.9", "3.10", "3.11"]
python-version: ["3.10"]
poetry-version: ["1.2.1"]
python-version: ["3.11"]
poetry-version: ["1.8.3"]

services:
nats:
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ static-check: ## Static code check
security: ## Run security check
poetry export -f requirements.txt --without-hashes | sed 's/; .*//' > /tmp/req.txt
sed -i '/^typing-extensions/d' /tmp/req.txt
sed -i '/^anyio/d' /tmp/req.txt
poetry run safety check -r /tmp/req.txt
poetry run bandit -lll -r .
poetry run vulture . --min-confidence 95
Expand Down
2 changes: 2 additions & 0 deletions natsapi/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import inspect
import logging
import secrets
from natsapi.context import CTX_JSONRPC_ID


from ssl import create_default_context
Expand Down Expand Up @@ -105,6 +106,7 @@ async def _handle_request(self, msg):
request = JsonRPCRequest.parse_raw(msg.data)
request.id = request.id or uuid4()

CTX_JSONRPC_ID.set(request.id)
subject = msg.subject

if subject not in self.routes and request.method:
Expand Down
5 changes: 5 additions & 0 deletions natsapi/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import contextvars

CTX_JSONRPC_ID = contextvars.ContextVar("jsonrpc_id")

__all__ = ["CTX_JSONRPC_ID"]
1,305 changes: 715 additions & 590 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ generate-setup-file = false
[tool.poetry.dependencies]
python = "^3.9"

pydantic = "^1.10.2"
pydantic = "^1.10.13"
nats-py = {extras = ["nkeys"], version = "^2.2.0"}
watchgod = "^0.8.2"

Expand Down
14 changes: 14 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from pydantic import BaseModel
from natsapi.models import JsonRPCReply, JsonRPCRequest
from natsapi.context import CTX_JSONRPC_ID

from natsapi import NatsAPI, SubjectRouter
from natsapi.exceptions import JsonRPCException
Expand Down Expand Up @@ -167,3 +169,15 @@ async def test_each_nats_request_should_have_different_id(app, natsapi_mock):

# then:
assert natsapi_mock.payloads["foobar"][0]["id"] != natsapi_mock.payloads["foobar"][1]["id"]


async def test_send_request_should_store_jsonrpc_id_in_contextvars(app):
@app.request(subject="foo")
async def _(app):
return {"jsonrpc_id": CTX_JSONRPC_ID.get()}

json_rpc_payload = JsonRPCRequest(params={"foo": 1}, method=None, timeout=60)
reply_raw = await app.nc.nats.request("natsapi.development.foo", json_rpc_payload.json().encode(), 60, headers=None)
reply = JsonRPCReply.parse_raw(reply_raw.data)

assert reply.result["jsonrpc_id"] == str(json_rpc_payload.id)

0 comments on commit e6cdc5f

Please sign in to comment.