2020# DEALINGS IN THE SOFTWARE.
2121
2222from __future__ import annotations
23+ from logging import getLogger
2324from typing import TYPE_CHECKING
2425from ...common .maybe_coro import maybe_coro
26+ from ...common .json import json_dumps
2527from .request import InteractionRequest
2628from .response import InteractionResponse
2729from .request_verifier import RequestVerifier
3335
3436 RequestCheck : TypeAlias = Callable [[InteractionRequest ], Union [Awaitable [None ], None ]]
3537
38+ logger = getLogger (__name__ )
39+
3640# TODO: The controller name is stupid.
3741class 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