|
1 | | -from pydantic import BaseModel, HttpUrl, AnyUrl, Field, constr |
2 | | -from typing import Optional, Dict |
| 1 | +from pydantic import BaseModel, HttpUrl, AnyUrl, constr, field_serializer |
| 2 | +from typing import Dict |
3 | 3 | from typing_extensions import Literal |
4 | 4 |
|
5 | 5 |
|
6 | 6 | class ConnectEndpoints: |
7 | 7 | class Endpoint(BaseModel): |
8 | | - type: str = None |
| 8 | + type: Literal['phone', 'app', 'websocket', 'sip', 'vbc'] = None |
9 | 9 |
|
10 | 10 | class PhoneEndpoint(Endpoint): |
11 | | - type = Field('phone', const=True) |
12 | | - number: constr(regex=r'^[1-9]\d{6,14}$') |
13 | | - dtmfAnswer: Optional[constr(regex='^[0-9*#p]+$')] |
14 | | - onAnswer: Optional[Dict[str, HttpUrl]] |
| 11 | + type: Literal['phone'] = 'phone' |
| 12 | + |
| 13 | + number: constr(pattern=r'^[1-9]\d{6,14}$') |
| 14 | + dtmfAnswer: constr(pattern='^[0-9*#p]+$') = None |
| 15 | + onAnswer: Dict[str, HttpUrl] = None |
| 16 | + |
| 17 | + @field_serializer('onAnswer') |
| 18 | + def serialize_dt(self, oa: Dict[str, HttpUrl], _info): |
| 19 | + if oa is None: |
| 20 | + return oa |
| 21 | + |
| 22 | + return {k: str(v) for k, v in oa.items()} |
15 | 23 |
|
16 | 24 | class AppEndpoint(Endpoint): |
17 | | - type = Field('app', const=True) |
| 25 | + type: Literal['app'] = 'app' |
18 | 26 | user: str |
19 | 27 |
|
20 | 28 | class WebsocketEndpoint(Endpoint): |
21 | | - type = Field('websocket', const=True) |
| 29 | + type: Literal['websocket'] = 'websocket' |
| 30 | + |
22 | 31 | uri: AnyUrl |
23 | 32 | contentType: Literal['audio/l16;rate=16000', 'audio/l16;rate=8000'] |
24 | | - headers: Optional[dict] |
| 33 | + headers: dict = None |
| 34 | + |
| 35 | + @field_serializer('uri') |
| 36 | + def serialize_uri(self, uri: AnyUrl, _info): |
| 37 | + return str(uri) |
25 | 38 |
|
26 | 39 | class SipEndpoint(Endpoint): |
27 | | - type = Field('sip', const=True) |
| 40 | + type: Literal['sip'] = 'sip' |
28 | 41 | uri: str |
29 | | - headers: Optional[dict] |
| 42 | + headers: dict = None |
30 | 43 |
|
31 | 44 | class VbcEndpoint(Endpoint): |
32 | | - type = Field('vbc', const=True) |
| 45 | + type: Literal['vbc'] = 'vbc' |
33 | 46 | extension: str |
34 | 47 |
|
35 | 48 | @classmethod |
36 | 49 | def create_endpoint_model_from_dict(cls, d) -> Endpoint: |
37 | 50 | if d['type'] == 'phone': |
38 | | - return cls.PhoneEndpoint.parse_obj(d) |
| 51 | + return cls.PhoneEndpoint.model_validate(d) |
39 | 52 | elif d['type'] == 'app': |
40 | | - return cls.AppEndpoint.parse_obj(d) |
| 53 | + return cls.AppEndpoint.model_validate(d) |
41 | 54 | elif d['type'] == 'websocket': |
42 | | - return cls.WebsocketEndpoint.parse_obj(d) |
| 55 | + return cls.WebsocketEndpoint.model_validate(d) |
43 | 56 | elif d['type'] == 'sip': |
44 | | - return cls.WebsocketEndpoint.parse_obj(d) |
| 57 | + return cls.WebsocketEndpoint.model_validate(d) |
45 | 58 | elif d['type'] == 'vbc': |
46 | | - return cls.WebsocketEndpoint.parse_obj(d) |
| 59 | + return cls.WebsocketEndpoint.model_validate(d) |
47 | 60 | else: |
48 | 61 | raise ValueError( |
49 | 62 | 'Invalid "type" specified for endpoint object. Cannot create a ConnectEndpoints.Endpoint model.' |
|
0 commit comments