11import base64
22import binascii
33import logging
4- from typing import Any , ClassVar , cast
54from urllib import parse
65
76from cryptography .fernet import Fernet , InvalidToken
87from models_library .invitations import InvitationContent , InvitationInputs
98from models_library .products import ProductName
10- from pydantic import HttpUrl , ValidationError , parse_obj_as
9+ from pydantic import ConfigDict , HttpUrl , TypeAdapter , ValidationError
1110from starlette .datastructures import URL
1211
1312_logger = logging .getLogger (__name__ )
1413
1514
15+ def _to_initial (v : str ):
16+ return v [0 ]
17+
18+
1619class InvalidInvitationCodeError (Exception ):
1720 ...
1821
@@ -25,7 +28,7 @@ def serialize(cls, model_obj: InvitationContent) -> str:
2528 """Exports to json using *short* aliases and values in order to produce shorter codes"""
2629 model_w_short_aliases_json : str = cls .construct (
2730 ** model_obj .dict (exclude_unset = True )
28- ).json (exclude_unset = True , by_alias = True )
31+ ).model_dump_json (exclude_unset = True , by_alias = True )
2932 # NOTE: json arguments try to minimize the amount of data
3033 # serialized. The CONS is that it relies on models in the code
3134 # that might change over time. This might lead to some datasets in codes
@@ -40,31 +43,13 @@ def deserialize(cls, raw_json: str) -> InvitationContent:
4043 ** model_w_short_aliases .dict (exclude_unset = True )
4144 )
4245
43- class Config :
44- allow_population_by_field_name = True # NOTE: can parse using field names
45- allow_mutation = False
46- anystr_strip_whitespace = True
46+ model_config = ConfigDict (
4747 # NOTE: Can export with alias: short aliases to minimize the size of serialization artifact
48- fields : ClassVar [dict [str , Any ]] = {
49- "issuer" : {
50- "alias" : "i" ,
51- },
52- "guest" : {
53- "alias" : "g" ,
54- },
55- "trial_account_days" : {
56- "alias" : "t" ,
57- },
58- "extra_credits_in_usd" : {
59- "alias" : "e" ,
60- },
61- "product" : {
62- "alias" : "p" ,
63- },
64- "created" : {
65- "alias" : "c" ,
66- },
67- }
48+ alias_generator = _to_initial ,
49+ populate_by_name = True , # NOTE: can parse using field names
50+ frozen = True ,
51+ str_strip_whitespace = True ,
52+ )
6853
6954
7055#
@@ -81,7 +66,7 @@ def _build_link(
8166 # Adds query to fragment
8267 base_url = f"{ base_url .rstrip ('/' )} /"
8368 url = URL (base_url ).replace (fragment = f"{ r } " )
84- return cast (HttpUrl , parse_obj_as ( HttpUrl , f"{ url } " ) )
69+ return TypeAdapter (HttpUrl ). validate_python ( f"{ url } " )
8570
8671
8772def _fernet_encrypt_as_urlsafe_code (
0 commit comments