1919 status ,
2020)
2121from fastapi .dependencies .models import Dependant
22- from pydantic import parse_obj_as
22+ from pydantic import TypeAdapter
2323from pydantic .types import Json
2424from typing_extensions import Annotated
2525
@@ -98,14 +98,17 @@ def _enrich_statement_with_authority(
9898) -> None :
9999 # authority: Information about whom or what has asserted the statement is true.
100100 # https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#249-authority
101- statement ["authority" ] = current_user .agent
101+ statement ["authority" ] = current_user .agent .model_dump (
102+ exclude_none = True , mode = "json"
103+ )
102104
103105
104106def _parse_agent_parameters (agent_obj : dict ) -> AgentParameters :
105107 """Parse a dict and return an AgentParameters object to use in queries."""
106108 # Transform agent to `dict` as FastAPI cannot parse JSON (seen as string)
107109
108- agent = parse_obj_as (BaseXapiAgent , agent_obj )
110+ adapter = TypeAdapter (BaseXapiAgent )
111+ agent = adapter .validate_python (agent_obj )
109112
110113 agent_query_params = {}
111114 if isinstance (agent , BaseXapiAgentWithMbox ):
@@ -119,7 +122,7 @@ def _parse_agent_parameters(agent_obj: dict) -> AgentParameters:
119122 agent_query_params ["account__home_page" ] = agent .account .homePage
120123
121124 # Overwrite `agent` field
122- return AgentParameters .construct (** agent_query_params )
125+ return AgentParameters .model_construct (** agent_query_params )
123126
124127
125128def strict_query_params (request : Request ) -> None :
@@ -141,7 +144,7 @@ def strict_query_params(request: Request) -> None:
141144
142145@router .get ("" )
143146@router .get ("/" )
144- async def get ( # noqa: PLR0913
147+ async def get ( # noqa: PLR0912, PLR0913
145148 request : Request ,
146149 current_user : Annotated [
147150 AuthenticatedUser ,
@@ -169,7 +172,7 @@ async def get( # noqa: PLR0913
169172 None ,
170173 description = "Filter, only return Statements matching the specified Verb id" ,
171174 ),
172- activity : Optional [IRI ] = Query (
175+ activity : Optional [str ] = Query (
173176 None ,
174177 description = (
175178 "Filter, only return Statements for which the Object "
@@ -334,7 +337,14 @@ async def get( # noqa: PLR0913
334337 # Overwrite `agent` field
335338 query_params ["agent" ] = _parse_agent_parameters (
336339 json .loads (query_params ["agent" ])
337- )
340+ ).model_dump (mode = "json" , exclude_none = True )
341+
342+ # Coerce `verb` and `activity` as IRI
343+ if query_params .get ("verb" ):
344+ query_params ["verb" ] = IRI (query_params ["verb" ])
345+
346+ if query_params .get ("activity" ):
347+ query_params ["activity" ] = IRI (query_params ["activity" ])
338348
339349 # mine: If using scopes, only restrict users with limited scopes
340350 if settings .LRS_RESTRICT_BY_SCOPES :
@@ -346,7 +356,9 @@ async def get( # noqa: PLR0913
346356
347357 # Filter by authority if using `mine`
348358 if mine :
349- query_params ["authority" ] = _parse_agent_parameters (current_user .agent )
359+ query_params ["authority" ] = _parse_agent_parameters (
360+ current_user .agent .model_dump (mode = "json" )
361+ ).model_dump (mode = "json" , exclude_none = True )
350362
351363 if "mine" in query_params :
352364 query_params .pop ("mine" )
@@ -355,7 +367,7 @@ async def get( # noqa: PLR0913
355367 try :
356368 query_result = await await_if_coroutine (
357369 BACKEND_CLIENT .query_statements (
358- params = RalphStatementsQuery .construct (
370+ params = RalphStatementsQuery .model_construct (
359371 ** {** query_params , "limit" : limit }
360372 ),
361373 target = current_user .target ,
@@ -418,7 +430,7 @@ async def put(
418430 LRS Specification:
419431 https://github.com/adlnet/xAPI-Spec/blob/1.0.3/xAPI-Communication.md#211-put-statements
420432 """
421- statement_as_dict = statement .dict (exclude_unset = True )
433+ statement_as_dict = statement .model_dump (exclude_unset = True , mode = "json" )
422434 statement_id = str (statement_id )
423435
424436 statement_as_dict .update (id = str (statement_as_dict .get ("id" , statement_id )))
@@ -516,7 +528,9 @@ async def post( # noqa: PLR0912
516528
517529 # Enrich statements before forwarding
518530 statements_dict = {}
519- for statement in (x .dict (exclude_unset = True ) for x in statements ):
531+ for statement in (
532+ x .model_dump (exclude_unset = True , mode = "json" ) for x in statements
533+ ):
520534 _enrich_statement_with_id (statement )
521535 # Requests with duplicate statement IDs are considered invalid
522536 if statement ["id" ] in statements_dict :
0 commit comments