Skip to content

Commit 3776dc3

Browse files
feat: Introduce raw_data field for storing raw json response (box/box-codegen#566) (#319)
1 parent b3d8da4 commit 3776dc3

File tree

5 files changed

+34
-15
lines changed

5 files changed

+34
-15
lines changed

.codegen.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "engineHash": "0338480", "specHash": "d9f90b8", "version": "1.4.1" }
1+
{ "engineHash": "5ceb679", "specHash": "d9f90b8", "version": "1.4.1" }

box_sdk_gen/internal/base_object.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class BaseObject:
1010
_fields_to_json_mapping = {}
1111

1212
def __init__(self, **kwargs):
13+
self._raw_data: dict = {}
1314
self.__dict__.update(kwargs)
1415

1516
@classmethod
@@ -23,9 +24,20 @@ def from_dict(cls, data: dict):
2324
)
2425
return cls(**unpacked_attributes)
2526

27+
@property
28+
def raw_data(self):
29+
"""
30+
Returns the raw json representation returned by the API
31+
:return: dict with the raw json data
32+
"""
33+
return self._raw_data
34+
2635
def to_dict(self) -> dict:
2736
result_dict = {}
2837
for k, v in vars(self).items():
38+
# Skip private and protected attributes
39+
if k.startswith("_"):
40+
continue
2941
if v is None:
3042
continue
3143
if isinstance(v, NullValue):

box_sdk_gen/internal/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ def get_epoch_time_in_seconds() -> int:
237237
return int(time.time())
238238

239239

240+
def get_value_from_object_raw_data(obj: BaseObject, key: str) -> Any:
241+
return obj.raw_data.get(key, None)
242+
243+
240244
class JwtAlgorithm(str, Enum):
241245
HS256 = 'HS256'
242246
HS384 = 'HS384'

box_sdk_gen/serialization/json/serializer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ def serialize(obj: Union[BaseObject, dict, list]) -> SerializedData:
2020
def deserialize(value: SerializedData, type: Type[BaseObject]):
2121
if get_origin(type) == Union:
2222
type = BaseObject._deserialize_union('', value, type)
23-
24-
return type.from_dict(value)
23+
obj = type.from_dict(value)
24+
obj._raw_data = value
25+
return obj

test/ai.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858

5959
from box_sdk_gen.internal.utils import date_time_to_string
6060

61+
from box_sdk_gen.internal.utils import get_value_from_object_raw_data
62+
6163
from test.commons import upload_new_file
6264

6365
from box_sdk_gen.schemas.ai_agent_ask import AiAgentAsk
@@ -210,7 +212,7 @@ def testAIExtract():
210212
),
211213
)
212214
file: FileFull = uploaded_files.entries[0]
213-
delay_in_seconds(1)
215+
delay_in_seconds(2)
214216
response: AiResponse = client.ai.create_ai_extract(
215217
'firstName, lastName, location, yearOfBirth, company',
216218
[AiItemBase(id=file.id)],
@@ -240,7 +242,7 @@ def testAIExtractStructuredWithFields():
240242
),
241243
)
242244
file: FileFull = uploaded_files.entries[0]
243-
delay_in_seconds(1)
245+
delay_in_seconds(2)
244246
response: AiExtractResponse = client.ai.create_ai_extract_structured(
245247
[AiItemBase(id=file.id)],
246248
fields=[
@@ -282,11 +284,11 @@ def testAIExtractStructuredWithFields():
282284
],
283285
ai_agent=ai_extract_structured_agent_config,
284286
)
285-
assert response.firstName == 'John'
286-
assert response.lastName == 'Doe'
287-
assert response.dateOfBirth == '1990-07-04'
288-
assert response.age == 34
289-
assert response.hobby == ['guitar', 'books']
287+
assert get_value_from_object_raw_data(response, 'firstName') == 'John'
288+
assert get_value_from_object_raw_data(response, 'lastName') == 'Doe'
289+
assert get_value_from_object_raw_data(response, 'dateOfBirth') == '1990-07-04'
290+
assert get_value_from_object_raw_data(response, 'age') == 34
291+
assert get_value_from_object_raw_data(response, 'hobby') == ['guitar', 'books']
290292
client.files.delete_file_by_id(file.id)
291293

292294

@@ -345,11 +347,11 @@ def testAIExtractStructuredWithMetadataTemplate():
345347
template_key=template_key, scope='enterprise'
346348
),
347349
)
348-
assert response.firstName == 'John'
349-
assert response.lastName == 'Doe'
350-
assert response.dateOfBirth == '1990-07-04'
351-
assert response.age == 34
352-
assert response.hobby == ['guitar', 'books']
350+
assert get_value_from_object_raw_data(response, 'firstName') == 'John'
351+
assert get_value_from_object_raw_data(response, 'lastName') == 'Doe'
352+
assert get_value_from_object_raw_data(response, 'dateOfBirth') == '1990-07-04'
353+
assert get_value_from_object_raw_data(response, 'age') == 34
354+
assert get_value_from_object_raw_data(response, 'hobby') == ['guitar', 'books']
353355
client.metadata_templates.delete_metadata_template(
354356
DeleteMetadataTemplateScope.ENTERPRISE.value, template.template_key
355357
)

0 commit comments

Comments
 (0)