Skip to content

Commit 03c7ce1

Browse files
committed
feat: Provide SignedSession with send_signed shorthand
* No need to craft a request object before sending
1 parent 55efb83 commit 03c7ce1

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

tests/test_signed_session.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,33 @@ async def test_sending_signed_request(synthetic_request_signer, respx_mock):
2929
)
3030

3131
async with signed_session:
32-
resp = await signed_session.send_signed(request)
32+
resp = await signed_session.send_request_signed(request)
3333

3434
assert route.called
3535
assert resp.request.headers.get("Signature") is not None
36+
37+
@pytest.mark.asyncio
38+
async def test_sending_signed(synthetic_request_signer, respx_mock):
39+
route = respx_mock.post("https://xsts.auth.xboxlive.com").mock(
40+
return_value=Response(200, json=get_response_json("auth_xsts_token"))
41+
)
42+
43+
signed_session = SignedSession(synthetic_request_signer)
44+
45+
method="POST"
46+
url="https://xsts.auth.xboxlive.com/xsts/authorize"
47+
headers={"x-xbl-contract-version": "1"}
48+
data={
49+
"RelyingParty": "http://xboxlive.com",
50+
"TokenType": "JWT",
51+
"Properties": {
52+
"UserTokens": ["eyJWTblabla"],
53+
"SandboxId": "RETAIL",
54+
},
55+
}
56+
57+
async with signed_session:
58+
resp = await signed_session.send_signed(method, url, headers=headers, data=data)
59+
60+
assert route.called
61+
assert resp.request.headers.get("Signature") is not None

xbox/webapi/common/signed_session.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,17 @@ def _prepare_signed_request(self, request: httpx.Request) -> httpx.Request:
3636
request.headers["Signature"] = signature
3737
return request
3838

39-
async def send_signed(self, request: httpx.Request) -> httpx.Response:
39+
async def send_request_signed(self, request: httpx.Request) -> httpx.Response:
4040
"""
4141
Shorthand for prepare signed + send
4242
"""
4343
prepared = self._prepare_signed_request(request)
4444
return await self.send(prepared)
45+
46+
async def send_signed(self, method: str, url: str, **kwargs):
47+
"""
48+
Shorthand for creating request + prepare signed + send
49+
"""
50+
request = httpx.Request(method, url, **kwargs)
51+
prepared = self._prepare_signed_request(request)
52+
return await self.send(prepared)

0 commit comments

Comments
 (0)