Skip to content

Commit d03c161

Browse files
committed
docs(endpoint/interactions): more docs
1 parent 97eef72 commit d03c161

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

nextcore/endpoint/interactions/controller.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
# DEALINGS IN THE SOFTWARE.
2121

2222
from __future__ import annotations
23+
from logging import getLogger
2324
from typing import TYPE_CHECKING
2425
from ...common.maybe_coro import maybe_coro
26+
from ...common.json import json_dumps
2527
from .request import InteractionRequest
2628
from .response import InteractionResponse
2729
from .request_verifier import RequestVerifier
@@ -33,6 +35,8 @@
3335

3436
RequestCheck: TypeAlias = Callable[[InteractionRequest], Union[Awaitable[None], None]]
3537

38+
logger = getLogger(__name__)
39+
3640
# TODO: The controller name is stupid.
3741
class InteractionController:
3842
"""A class for handling endpoint interactions"""
@@ -41,14 +45,48 @@ def __init__(self, public_key: str | bytes) -> None:
4145
self.request_checks: list[RequestCheck] = [self.check_has_required_headers, self.check_has_valid_signature]
4246

4347
async def handle_interaction_request(self, request: InteractionRequest) -> InteractionResponse:
48+
"""Callback for handling a interaction request
49+
50+
.. note::
51+
This is meant to be called by a web-framework adapter.
52+
53+
Parameters
54+
----------
55+
request:
56+
The HTTP request that was received for this interaction
57+
58+
Returns
59+
-------
60+
InteractionResponse:
61+
What to respond with.
62+
63+
"""
4464
try:
4565
await self.verify_request(request)
4666
except RequestVerificationError as error:
4767
return InteractionResponse(401, error.reason)
68+
except:
69+
logger.exception("Error occured while handling interaction")
70+
error_response = {"detail": f"Internal check error. Check the {__name__} logger for details"}
71+
return InteractionResponse(500, json_dumps(error_response))
4872

4973
return InteractionResponse(status_code=200, body="{\"type\": 1}")
5074

5175
async def verify_request(self, request: InteractionRequest) -> None:
76+
"""Try all request checks
77+
78+
Parameters
79+
----------
80+
request:
81+
The request made
82+
Raises
83+
------
84+
RequestVerificationError:
85+
The request checks did not succeed, so you should not let it execute.
86+
The reason it did not succeed is also in the error.
87+
Exception:
88+
A error in one of the checks was raised. In this case,
89+
"""
5290
for check in self.request_checks:
5391
await maybe_coro(check, request)
5492

0 commit comments

Comments
 (0)