19
19
status ,
20
20
)
21
21
from fastapi .dependencies .models import Dependant
22
- from pydantic import parse_obj_as
22
+ from pydantic import TypeAdapter
23
23
from pydantic .types import Json
24
24
from typing_extensions import Annotated
25
25
@@ -98,14 +98,17 @@ def _enrich_statement_with_authority(
98
98
) -> None :
99
99
# authority: Information about whom or what has asserted the statement is true.
100
100
# 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
+ )
102
104
103
105
104
106
def _parse_agent_parameters (agent_obj : dict ) -> AgentParameters :
105
107
"""Parse a dict and return an AgentParameters object to use in queries."""
106
108
# Transform agent to `dict` as FastAPI cannot parse JSON (seen as string)
107
109
108
- agent = parse_obj_as (BaseXapiAgent , agent_obj )
110
+ adapter = TypeAdapter (BaseXapiAgent )
111
+ agent = adapter .validate_python (agent_obj )
109
112
110
113
agent_query_params = {}
111
114
if isinstance (agent , BaseXapiAgentWithMbox ):
@@ -119,7 +122,7 @@ def _parse_agent_parameters(agent_obj: dict) -> AgentParameters:
119
122
agent_query_params ["account__home_page" ] = agent .account .homePage
120
123
121
124
# Overwrite `agent` field
122
- return AgentParameters .construct (** agent_query_params )
125
+ return AgentParameters .model_construct (** agent_query_params )
123
126
124
127
125
128
def strict_query_params (request : Request ) -> None :
@@ -141,7 +144,7 @@ def strict_query_params(request: Request) -> None:
141
144
142
145
@router .get ("" )
143
146
@router .get ("/" )
144
- async def get ( # noqa: PLR0913
147
+ async def get ( # noqa: PLR0912, PLR0913
145
148
request : Request ,
146
149
current_user : Annotated [
147
150
AuthenticatedUser ,
@@ -169,7 +172,7 @@ async def get( # noqa: PLR0913
169
172
None ,
170
173
description = "Filter, only return Statements matching the specified Verb id" ,
171
174
),
172
- activity : Optional [IRI ] = Query (
175
+ activity : Optional [str ] = Query (
173
176
None ,
174
177
description = (
175
178
"Filter, only return Statements for which the Object "
@@ -334,7 +337,14 @@ async def get( # noqa: PLR0913
334
337
# Overwrite `agent` field
335
338
query_params ["agent" ] = _parse_agent_parameters (
336
339
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" ])
338
348
339
349
# mine: If using scopes, only restrict users with limited scopes
340
350
if settings .LRS_RESTRICT_BY_SCOPES :
@@ -346,7 +356,9 @@ async def get( # noqa: PLR0913
346
356
347
357
# Filter by authority if using `mine`
348
358
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 )
350
362
351
363
if "mine" in query_params :
352
364
query_params .pop ("mine" )
@@ -355,7 +367,7 @@ async def get( # noqa: PLR0913
355
367
try :
356
368
query_result = await await_if_coroutine (
357
369
BACKEND_CLIENT .query_statements (
358
- params = RalphStatementsQuery .construct (
370
+ params = RalphStatementsQuery .model_construct (
359
371
** {** query_params , "limit" : limit }
360
372
),
361
373
target = current_user .target ,
@@ -418,7 +430,7 @@ async def put(
418
430
LRS Specification:
419
431
https://github.com/adlnet/xAPI-Spec/blob/1.0.3/xAPI-Communication.md#211-put-statements
420
432
"""
421
- statement_as_dict = statement .dict (exclude_unset = True )
433
+ statement_as_dict = statement .model_dump (exclude_unset = True , mode = "json" )
422
434
statement_id = str (statement_id )
423
435
424
436
statement_as_dict .update (id = str (statement_as_dict .get ("id" , statement_id )))
@@ -516,7 +528,9 @@ async def post( # noqa: PLR0912
516
528
517
529
# Enrich statements before forwarding
518
530
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
+ ):
520
534
_enrich_statement_with_id (statement )
521
535
# Requests with duplicate statement IDs are considered invalid
522
536
if statement ["id" ] in statements_dict :
0 commit comments