Skip to content

Commit

Permalink
feat: Introduce raw_data field for storing raw json response (box…
Browse files Browse the repository at this point in the history
  • Loading branch information
box-sdk-build authored Sep 18, 2024
1 parent b3d8da4 commit 3776dc3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .codegen.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "engineHash": "0338480", "specHash": "d9f90b8", "version": "1.4.1" }
{ "engineHash": "5ceb679", "specHash": "d9f90b8", "version": "1.4.1" }
12 changes: 12 additions & 0 deletions box_sdk_gen/internal/base_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class BaseObject:
_fields_to_json_mapping = {}

def __init__(self, **kwargs):
self._raw_data: dict = {}
self.__dict__.update(kwargs)

@classmethod
Expand All @@ -23,9 +24,20 @@ def from_dict(cls, data: dict):
)
return cls(**unpacked_attributes)

@property
def raw_data(self):
"""
Returns the raw json representation returned by the API
:return: dict with the raw json data
"""
return self._raw_data

def to_dict(self) -> dict:
result_dict = {}
for k, v in vars(self).items():
# Skip private and protected attributes
if k.startswith("_"):
continue
if v is None:
continue
if isinstance(v, NullValue):
Expand Down
4 changes: 4 additions & 0 deletions box_sdk_gen/internal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ def get_epoch_time_in_seconds() -> int:
return int(time.time())


def get_value_from_object_raw_data(obj: BaseObject, key: str) -> Any:
return obj.raw_data.get(key, None)


class JwtAlgorithm(str, Enum):
HS256 = 'HS256'
HS384 = 'HS384'
Expand Down
5 changes: 3 additions & 2 deletions box_sdk_gen/serialization/json/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ def serialize(obj: Union[BaseObject, dict, list]) -> SerializedData:
def deserialize(value: SerializedData, type: Type[BaseObject]):
if get_origin(type) == Union:
type = BaseObject._deserialize_union('', value, type)

return type.from_dict(value)
obj = type.from_dict(value)
obj._raw_data = value
return obj
26 changes: 14 additions & 12 deletions test/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@

from box_sdk_gen.internal.utils import date_time_to_string

from box_sdk_gen.internal.utils import get_value_from_object_raw_data

from test.commons import upload_new_file

from box_sdk_gen.schemas.ai_agent_ask import AiAgentAsk
Expand Down Expand Up @@ -210,7 +212,7 @@ def testAIExtract():
),
)
file: FileFull = uploaded_files.entries[0]
delay_in_seconds(1)
delay_in_seconds(2)
response: AiResponse = client.ai.create_ai_extract(
'firstName, lastName, location, yearOfBirth, company',
[AiItemBase(id=file.id)],
Expand Down Expand Up @@ -240,7 +242,7 @@ def testAIExtractStructuredWithFields():
),
)
file: FileFull = uploaded_files.entries[0]
delay_in_seconds(1)
delay_in_seconds(2)
response: AiExtractResponse = client.ai.create_ai_extract_structured(
[AiItemBase(id=file.id)],
fields=[
Expand Down Expand Up @@ -282,11 +284,11 @@ def testAIExtractStructuredWithFields():
],
ai_agent=ai_extract_structured_agent_config,
)
assert response.firstName == 'John'
assert response.lastName == 'Doe'
assert response.dateOfBirth == '1990-07-04'
assert response.age == 34
assert response.hobby == ['guitar', 'books']
assert get_value_from_object_raw_data(response, 'firstName') == 'John'
assert get_value_from_object_raw_data(response, 'lastName') == 'Doe'
assert get_value_from_object_raw_data(response, 'dateOfBirth') == '1990-07-04'
assert get_value_from_object_raw_data(response, 'age') == 34
assert get_value_from_object_raw_data(response, 'hobby') == ['guitar', 'books']
client.files.delete_file_by_id(file.id)


Expand Down Expand Up @@ -345,11 +347,11 @@ def testAIExtractStructuredWithMetadataTemplate():
template_key=template_key, scope='enterprise'
),
)
assert response.firstName == 'John'
assert response.lastName == 'Doe'
assert response.dateOfBirth == '1990-07-04'
assert response.age == 34
assert response.hobby == ['guitar', 'books']
assert get_value_from_object_raw_data(response, 'firstName') == 'John'
assert get_value_from_object_raw_data(response, 'lastName') == 'Doe'
assert get_value_from_object_raw_data(response, 'dateOfBirth') == '1990-07-04'
assert get_value_from_object_raw_data(response, 'age') == 34
assert get_value_from_object_raw_data(response, 'hobby') == ['guitar', 'books']
client.metadata_templates.delete_metadata_template(
DeleteMetadataTemplateScope.ENTERPRISE.value, template.template_key
)
Expand Down

0 comments on commit 3776dc3

Please sign in to comment.