-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
152 lines (121 loc) · 4.59 KB
/
base.py
File metadata and controls
152 lines (121 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from typing import Any, Generic, List, Literal, TypeVar
import yaml
from pydantic import BaseModel, ConfigDict, RootModel
from pydantic.alias_generators import to_camel
from ..http_client import APIHttpClient
from .handler import _APIOperationExecutor
ModelT = TypeVar("ModelT", bound=BaseModel)
class ResourceBase(_APIOperationExecutor):
def __init__(self, http_client: APIHttpClient):
self._http_client = http_client
class CamelModel(BaseModel):
model_config = ConfigDict(
alias_generator=to_camel,
populate_by_name=True,
serialize_by_alias=True,
)
def to_dict(
self, *, by_alias: bool = True, exclude_none: bool = False
) -> dict[str, Any]:
"""Export model as a Python dictionary.
Args:
by_alias: Use camelCase keys (API format) if True, snake_case if False.
exclude_none: Exclude fields with None values if True.
Returns:
Dictionary representation of the model.
"""
return self.model_dump(by_alias=by_alias, exclude_none=exclude_none)
def to_json(
self,
*,
by_alias: bool = True,
exclude_none: bool = False,
indent: int | None = None,
) -> str:
"""Export model as a JSON string.
Args:
by_alias: Use camelCase keys (API format) if True, snake_case if False.
exclude_none: Exclude fields with None values if True.
indent: Number of spaces for indentation. None for compact output.
Returns:
JSON string representation of the model.
"""
return self.model_dump_json(
by_alias=by_alias, exclude_none=exclude_none, indent=indent
)
def to_yaml(self, *, by_alias: bool = True, exclude_none: bool = False) -> str:
"""Export model as a YAML string.
Args:
by_alias: Use camelCase keys (API format) if True, snake_case if False.
exclude_none: Exclude fields with None values if True.
Returns:
YAML string representation of the model.
"""
data = self.model_dump(
by_alias=by_alias, exclude_none=exclude_none, mode="json"
)
return yaml.safe_dump(
data, default_flow_style=False, allow_unicode=True, sort_keys=False
)
class ResourceList(RootModel[List[ModelT]], Generic[ModelT]):
root: List[ModelT]
def __iter__(self):
return iter(self.root)
def __getitem__(self, item):
return self.root[item]
def __len__(self):
return len(self.root)
def to_list(
self,
*,
by_alias: bool = True,
exclude_none: bool = False,
mode: Literal["python", "json"] = "python",
) -> list[dict[str, Any]]:
"""Export all items as a list of dictionaries.
Args:
by_alias: Use camelCase keys (API format) if True, snake_case if False.
exclude_none: Exclude fields with None values if True.
mode: Serialization mode. "python" returns native Python objects,
"json" returns JSON-compatible types (e.g., datetime as ISO string).
Returns:
List of dictionary representations.
"""
return [
item.model_dump(by_alias=by_alias, exclude_none=exclude_none, mode=mode)
for item in self.root
]
def to_json(
self,
*,
by_alias: bool = True,
exclude_none: bool = False,
indent: int | None = None,
) -> str:
"""Export all items as a JSON array string.
Args:
by_alias: Use camelCase keys (API format) if True, snake_case if False.
exclude_none: Exclude fields with None values if True.
indent: Number of spaces for indentation. None for compact output.
Returns:
JSON array string representation.
"""
import json
return json.dumps(
self.to_list(by_alias=by_alias, exclude_none=exclude_none, mode="json"),
indent=indent,
)
def to_yaml(self, *, by_alias: bool = True, exclude_none: bool = False) -> str:
"""Export all items as a YAML string.
Args:
by_alias: Use camelCase keys (API format) if True, snake_case if False.
exclude_none: Exclude fields with None values if True.
Returns:
YAML string representation.
"""
return yaml.safe_dump(
self.to_list(by_alias=by_alias, exclude_none=exclude_none, mode="json"),
default_flow_style=False,
allow_unicode=True,
sort_keys=False,
)