Skip to content

Commit 74660c6

Browse files
committed
Formatting/Linting
1 parent 9746be1 commit 74660c6

2 files changed

Lines changed: 34 additions & 28 deletions

File tree

src/a2a/server/apps/jsonrpc/jsonrpc_app.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
from sse_starlette.sse import EventSourceResponse
1313
from starlette.applications import Starlette
1414
from starlette.authentication import BaseUser
15+
from starlette.exceptions import HTTPException
1516
from starlette.requests import Request
1617
from starlette.responses import JSONResponse, Response
17-
from starlette.exceptions import HTTPException
18+
from starlette.status import HTTP_413_REQUEST_ENTITY_TOO_LARGE
1819

1920
from a2a.auth.user import UnauthenticatedUser
2021
from a2a.auth.user import User as A2AUser
@@ -234,10 +235,12 @@ async def _handle_requests(self, request: Request) -> Response:
234235
A2AError(root=InvalidRequestError(data=json.loads(e.json()))),
235236
)
236237
except HTTPException as e:
237-
if e.status_code == 413: # Payload Too Large
238-
return self._generate_error_response(
238+
if e.status_code == HTTP_413_REQUEST_ENTITY_TOO_LARGE:
239+
return self._generate_error_response(
239240
request_id,
240-
A2AError(root=InvalidRequestError(message="Payload too large")),
241+
A2AError(
242+
root=InvalidRequestError(message='Payload too large')
243+
),
241244
)
242245
raise e
243246
except Exception as e:

tests/server/apps/jsonrpc/test_serialization.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import json
21
from unittest import mock
32

43
import pytest
4+
5+
from pydantic import ValidationError
56
from starlette.testclient import TestClient
67

78
from a2a.server.apps import A2AFastAPIApplication, A2AStarletteApplication
@@ -10,15 +11,14 @@
1011
AgentCapabilities,
1112
AgentCard,
1213
In,
14+
InvalidRequestError,
15+
JSONParseError,
1316
Message,
1417
Part,
1518
Role,
1619
SecurityScheme,
1720
TextPart,
18-
JSONParseError,
19-
InvalidRequestError,
2021
)
21-
from pydantic import ValidationError
2222

2323

2424
@pytest.fixture
@@ -107,7 +107,10 @@ def test_handle_invalid_json(agent_card_with_api_key: AgentCard):
107107
app_instance = A2AStarletteApplication(agent_card_with_api_key, handler)
108108
client = TestClient(app_instance.build())
109109

110-
response = client.post('/', content='{ "jsonrpc": "2.0", "method": "test", "id": 1, "params": { "key": "value" }')
110+
response = client.post(
111+
'/',
112+
content='{ "jsonrpc": "2.0", "method": "test", "id": 1, "params": { "key": "value" }',
113+
)
111114
assert response.status_code == 200
112115
data = response.json()
113116
assert data['error']['code'] == JSONParseError().code
@@ -119,12 +122,12 @@ def test_handle_oversized_payload(agent_card_with_api_key: AgentCard):
119122
app_instance = A2AStarletteApplication(agent_card_with_api_key, handler)
120123
client = TestClient(app_instance.build())
121124

122-
large_string = "a" * 2_000_000 # 2MB string
125+
large_string = 'a' * 2_000_000 # 2MB string
123126
payload = {
124-
"jsonrpc": "2.0",
125-
"method": "test",
126-
"id": 1,
127-
"params": {"data": large_string},
127+
'jsonrpc': '2.0',
128+
'method': 'test',
129+
'id': 1,
130+
'params': {'data': large_string},
128131
}
129132

130133
# Starlette/FastAPI's default max request size is around 1MB.
@@ -139,7 +142,7 @@ def test_handle_oversized_payload(agent_card_with_api_key: AgentCard):
139142
data = response.json()
140143
assert data['error']['code'] == InvalidRequestError().code
141144
else:
142-
assert response.status_code == 413
145+
assert response.status_code == 413
143146
except Exception as e:
144147
# Depending on server setup, it might just drop the connection for very large payloads
145148
assert isinstance(e, (ConnectionResetError, RuntimeError))
@@ -151,25 +154,25 @@ def test_handle_unicode_characters(agent_card_with_api_key: AgentCard):
151154
app_instance = A2AStarletteApplication(agent_card_with_api_key, handler)
152155
client = TestClient(app_instance.build())
153156

154-
unicode_text = "こんにちは世界" # "Hello world" in Japanese
157+
unicode_text = 'こんにちは世界' # "Hello world" in Japanese
155158
unicode_payload = {
156-
"jsonrpc": "2.0",
157-
"method": "message/send",
158-
"id": "unicode_test",
159-
"params": {
160-
"message": {
161-
"role": "user",
162-
"parts": [{"kind": "text", "text": unicode_text}],
163-
"messageId": "msg-unicode"
159+
'jsonrpc': '2.0',
160+
'method': 'message/send',
161+
'id': 'unicode_test',
162+
'params': {
163+
'message': {
164+
'role': 'user',
165+
'parts': [{'kind': 'text', 'text': unicode_text}],
166+
'messageId': 'msg-unicode',
164167
}
165-
}
168+
},
166169
}
167170

168171
# Mock a handler for this method
169172
handler.on_message_send.return_value = Message(
170173
role=Role.agent,
171-
parts=[Part(root=TextPart(text=f"Received: {unicode_text}"))],
172-
messageId="response-unicode"
174+
parts=[Part(root=TextPart(text=f'Received: {unicode_text}'))],
175+
messageId='response-unicode',
173176
)
174177

175178
response = client.post('/', json=unicode_payload)
@@ -180,4 +183,4 @@ def test_handle_unicode_characters(agent_card_with_api_key: AgentCard):
180183
assert response.status_code == 200
181184
data = response.json()
182185
assert 'error' not in data or data['error'] is None
183-
assert data['result']['parts'][0]['text'] == f"Received: {unicode_text}"
186+
assert data['result']['parts'][0]['text'] == f'Received: {unicode_text}'

0 commit comments

Comments
 (0)