Skip to content

Commit

Permalink
generated with codegen at box/box-codegen@a1233bf and spec at box/box…
Browse files Browse the repository at this point in the history
  • Loading branch information
box-sdk-build committed Sep 26, 2023
1 parent b59c14c commit 2430fd9
Show file tree
Hide file tree
Showing 97 changed files with 898 additions and 451 deletions.
50 changes: 31 additions & 19 deletions box_sdk_gen/base_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def from_dict(cls, data: dict):
for key, value in data.items():
mapping_field_name = cls._json_to_fields_mapping.get(key, key)
annotation = cls.__init__.__annotations__.get(mapping_field_name, None)
unpacked_attributes[mapping_field_name] = cls.__deserialize(
unpacked_attributes[mapping_field_name] = cls._deserialize(
key, value, annotation
)
return cls(**unpacked_attributes)
Expand All @@ -41,66 +41,78 @@ def to_dict(self) -> dict:
return result_dict

@classmethod
def __deserialize(cls, key, value, annotation=None):
def _deserialize(cls, key, value, annotation=None):
if annotation is None:
return value
if get_origin(annotation) == Optional:
return cls.__deserialize(key, value, get_args(annotation))
return cls._deserialize(key, value, get_args(annotation))
if get_origin(annotation) == Union:
union_without_none_type = [
arg for arg in get_args(annotation) if arg is not type(None)
]
if len(union_without_none_type) == 1:
return cls.__deserialize(key, value, union_without_none_type[0])
return cls._deserialize(key, value, union_without_none_type[0])

if get_origin(annotation) == list:
return cls.__deserialize_list(key, value, annotation)
return cls._deserialize_list(key, value, annotation)
elif get_origin(annotation) == Union:
return cls.__deserialize_union(key, value, annotation)
return cls._deserialize_union(key, value, annotation)
elif isinstance(annotation, EnumMeta):
return cls.__deserialize_enum(key, value, annotation)
return cls._deserialize_enum(key, value, annotation)
else:
return cls.__deserialize_nested_type(key, value, annotation)
return cls._deserialize_nested_type(key, value, annotation)

@classmethod
def __deserialize_list(cls, key, value, annotation: list):
def _deserialize_list(cls, key, value, annotation: list):
list_type = get_args(annotation)[0]
try:
return [
cls.__deserialize(key, list_entry, list_type) for list_entry in value
cls._deserialize(key, list_entry, list_type) for list_entry in value
]
except Exception:
return value

@classmethod
def __deserialize_union(cls, key, value, annotation):
def _deserialize_union(cls, key, value, annotation):
possible_types = get_args(annotation)
type_field_value = value.get('type', None) or value.get('skillCardType', None)
if 'type' in value:
type_field = 'type'
else:
type_field = 'skillCardType'
type_field_value = value.get(type_field, None)

type = None
for i, possible_type in enumerate(possible_types):
# remove special characters
if type_field_value.replace("_", "") in possible_types[i].__name__.lower():
type = possible_types[i]
break
try:
if (
type_field_value.replace("_", "")
in possible_types[i].__name__.lower()
or possible_types[i].__init__.__annotations__[type_field][
type_field_value.upper()
]
):
type = possible_types[i]
break
except Exception:
continue

if not type:
print('Could not deserialize Union: ', annotation, 'of value:', value)

try:
return cls.__deserialize(key, value, type)
return cls._deserialize(key, value, type)
except Exception:
return value

@classmethod
def __deserialize_enum(cls, key, value, annotation):
def _deserialize_enum(cls, key, value, annotation):
try:
return getattr(annotation, value.upper().replace(' ', '_'))
except Exception:
return value

@classmethod
def __deserialize_nested_type(cls, key, value, annotation):
def _deserialize_nested_type(cls, key, value, annotation):
try:
return annotation.from_dict(value)
except Exception:
Expand Down
6 changes: 3 additions & 3 deletions box_sdk_gen/managers/avatars.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def get_user_avatar(
extra_headers = {}
headers_map: Dict[str, str] = prepare_params({**extra_headers})
response: FetchResponse = fetch(
''.join(['https://api.box.com/2.0/users/', user_id, '/avatar']),
''.join(['https://api.box.com/2.0/users/', to_string(user_id), '/avatar']),
FetchOptions(
method='GET',
headers=headers_map,
Expand Down Expand Up @@ -91,7 +91,7 @@ def create_user_avatar(
)
headers_map: Dict[str, str] = prepare_params({**extra_headers})
response: FetchResponse = fetch(
''.join(['https://api.box.com/2.0/users/', user_id, '/avatar']),
''.join(['https://api.box.com/2.0/users/', to_string(user_id), '/avatar']),
FetchOptions(
method='POST',
headers=headers_map,
Expand Down Expand Up @@ -129,7 +129,7 @@ def delete_user_avatar(
extra_headers = {}
headers_map: Dict[str, str] = prepare_params({**extra_headers})
response: FetchResponse = fetch(
''.join(['https://api.box.com/2.0/users/', user_id, '/avatar']),
''.join(['https://api.box.com/2.0/users/', to_string(user_id), '/avatar']),
FetchOptions(
method='DELETE',
headers=headers_map,
Expand Down
16 changes: 10 additions & 6 deletions box_sdk_gen/managers/chunked_uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ def create_file_upload_session_for_existing_file(
headers_map: Dict[str, str] = prepare_params({**extra_headers})
response: FetchResponse = fetch(
''.join(
['https://upload.box.com/api/2.0/files/', file_id, '/upload_sessions']
[
'https://upload.box.com/api/2.0/files/',
to_string(file_id),
'/upload_sessions',
]
),
FetchOptions(
method='POST',
Expand Down Expand Up @@ -188,7 +192,7 @@ def get_file_upload_session_by_id(
''.join(
[
'https://upload.box.com/api/2.0/files/upload_sessions/',
upload_session_id,
to_string(upload_session_id),
]
),
FetchOptions(
Expand Down Expand Up @@ -252,7 +256,7 @@ def upload_file_part(
''.join(
[
'https://upload.box.com/api/2.0/files/upload_sessions/',
upload_session_id,
to_string(upload_session_id),
]
),
FetchOptions(
Expand Down Expand Up @@ -290,7 +294,7 @@ def delete_file_upload_session_by_id(
''.join(
[
'https://upload.box.com/api/2.0/files/upload_sessions/',
upload_session_id,
to_string(upload_session_id),
]
),
FetchOptions(
Expand Down Expand Up @@ -338,7 +342,7 @@ def get_file_upload_session_parts(
''.join(
[
'https://upload.box.com/api/2.0/files/upload_sessions/',
upload_session_id,
to_string(upload_session_id),
'/parts',
]
),
Expand Down Expand Up @@ -409,7 +413,7 @@ def create_file_upload_session_commit(
''.join(
[
'https://upload.box.com/api/2.0/files/upload_sessions/',
upload_session_id,
to_string(upload_session_id),
'/commit',
]
),
Expand Down
4 changes: 2 additions & 2 deletions box_sdk_gen/managers/collaboration_allowlist_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def get_collaboration_whitelist_entry_by_id(
''.join(
[
'https://api.box.com/2.0/collaboration_whitelist_entries/',
collaboration_whitelist_entry_id,
to_string(collaboration_whitelist_entry_id),
]
),
FetchOptions(
Expand Down Expand Up @@ -182,7 +182,7 @@ def delete_collaboration_whitelist_entry_by_id(
''.join(
[
'https://api.box.com/2.0/collaboration_whitelist_entries/',
collaboration_whitelist_entry_id,
to_string(collaboration_whitelist_entry_id),
]
),
FetchOptions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def get_collaboration_whitelist_exempt_target_by_id(
''.join(
[
'https://api.box.com/2.0/collaboration_whitelist_exempt_targets/',
collaboration_whitelist_exempt_target_id,
to_string(collaboration_whitelist_exempt_target_id),
]
),
FetchOptions(
Expand Down Expand Up @@ -183,7 +183,7 @@ def delete_collaboration_whitelist_exempt_target_by_id(
''.join(
[
'https://api.box.com/2.0/collaboration_whitelist_exempt_targets/',
collaboration_whitelist_exempt_target_id,
to_string(collaboration_whitelist_exempt_target_id),
]
),
FetchOptions(
Expand Down
18 changes: 13 additions & 5 deletions box_sdk_gen/managers/collections.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Optional

from typing import List

from typing import Dict

from box_sdk_gen.serialization import deserialize
Expand Down Expand Up @@ -38,7 +40,7 @@ def __init__(

def get_collections(
self,
fields: Optional[str] = None,
fields: Optional[List[str]] = None,
offset: Optional[int] = None,
limit: Optional[int] = None,
extra_headers: Optional[Dict[str, Optional[str]]] = None,
Expand All @@ -59,7 +61,7 @@ def get_collections(
the response unless explicitly specified, instead only
fields for the mini representation are returned, additional
to the fields requested.
:type fields: Optional[str], optional
:type fields: Optional[List[str]], optional
:param offset: The offset of the item at which to begin the response.
Queries with offset parameter value
exceeding 10000 will be rejected
Expand Down Expand Up @@ -96,7 +98,7 @@ def get_collections(
def get_collection_items(
self,
collection_id: str,
fields: Optional[str] = None,
fields: Optional[List[str]] = None,
offset: Optional[int] = None,
limit: Optional[int] = None,
extra_headers: Optional[Dict[str, Optional[str]]] = None,
Expand All @@ -117,7 +119,7 @@ def get_collection_items(
the response unless explicitly specified, instead only
fields for the mini representation are returned, additional
to the fields requested.
:type fields: Optional[str], optional
:type fields: Optional[List[str]], optional
:param offset: The offset of the item at which to begin the response.
Queries with offset parameter value
exceeding 10000 will be rejected
Expand All @@ -139,7 +141,13 @@ def get_collection_items(
)
headers_map: Dict[str, str] = prepare_params({**extra_headers})
response: FetchResponse = fetch(
''.join(['https://api.box.com/2.0/collections/', collection_id, '/items']),
''.join(
[
'https://api.box.com/2.0/collections/',
to_string(collection_id),
'/items',
]
),
FetchOptions(
method='GET',
params=query_params_map,
Expand Down
Loading

0 comments on commit 2430fd9

Please sign in to comment.