Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codegen output 719ea87132d24b9ebf7829f32ae36f40 #34

Merged
merged 6 commits into from
Sep 1, 2023
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ Embrace the new generation of Box SDKs and unlock the full potential of the Box
pip install box-sdk-gen
```

Use your GitHub account login and GitHub Personal access token as a password when prompt.
Guide how to obtain GitHub Personal access token can be found [here](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token#creating-a-fine-grained-personal-access-token).

This is autogenerated Box SDK Beta version. The engine used for generating this SDK can be found [here](https://github.com/box/box-codegen).
Supported Python versions are Python 3.8 and above.

Expand Down
9 changes: 7 additions & 2 deletions box_sdk_gen/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
from typing import Optional

from .network import NetworkSession
from .schemas import AccessToken


class Authentication:
@abstractmethod
def retrieve_token(self, network_session: Optional[NetworkSession] = None) -> str:
def retrieve_token(
self, network_session: Optional[NetworkSession] = None
) -> AccessToken:
pass

@abstractmethod
def refresh(self, network_session: Optional[NetworkSession] = None) -> str:
def refresh_token(
self, network_session: Optional[NetworkSession] = None
) -> AccessToken:
pass
39 changes: 25 additions & 14 deletions box_sdk_gen/auth_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ class TokenRequestGrantType(str, Enum):
AUTHORIZATION_CODE = 'authorization_code'
REFRESH_TOKEN = 'refresh_token'
CLIENT_CREDENTIALS = 'client_credentials'
URN_IETF_PARAMS_OAUTH_GRANT_TYPE_JWT_BEARER = 'urn:ietf:params:oauth:grant-type:jwt-bearer'
URN_IETF_PARAMS_OAUTH_GRANT_TYPE_TOKEN_EXCHANGE = 'urn:ietf:params:oauth:grant-type:token-exchange'
URN_IETF_PARAMS_OAUTH_GRANT_TYPE_JWT_BEARER = (
'urn:ietf:params:oauth:grant-type:jwt-bearer'
)
URN_IETF_PARAMS_OAUTH_GRANT_TYPE_TOKEN_EXCHANGE = (
'urn:ietf:params:oauth:grant-type:token-exchange'
)


class TokenRequestBoxSubjectType(str, Enum):
Expand All @@ -18,7 +22,25 @@ class TokenRequestBoxSubjectType(str, Enum):


class TokenRequest(BaseObject):
def __init__(self, grant_type: TokenRequestGrantType, client_id: Union[None, str] = None, client_secret: Union[None, str] = None, code: Union[None, str] = None, refresh_token: Union[None, str] = None, assertion: Union[None, str] = None, subject_token: Union[None, str] = None, subject_token_type: Union[None, str] = None, actor_token: Union[None, str] = None, actor_token_type: Union[None, str] = None, scope: Union[None, str] = None, resource: Union[None, str] = None, box_subject_type: Union[None, TokenRequestBoxSubjectType] = None, box_subject_id: Union[None, str] = None, box_shared_link: Union[None, str] = None, **kwargs):
def __init__(
self,
grant_type: TokenRequestGrantType,
client_id: Union[None, str] = None,
client_secret: Union[None, str] = None,
code: Union[None, str] = None,
refresh_token: Union[None, str] = None,
assertion: Union[None, str] = None,
subject_token: Union[None, str] = None,
subject_token_type: Union[None, str] = None,
actor_token: Union[None, str] = None,
actor_token_type: Union[None, str] = None,
scope: Union[None, str] = None,
resource: Union[None, str] = None,
box_subject_type: Union[None, TokenRequestBoxSubjectType] = None,
box_subject_id: Union[None, str] = None,
box_shared_link: Union[None, str] = None,
**kwargs
):
super().__init__(**kwargs)
self.grant_type = grant_type
self.client_id = client_id
Expand Down Expand Up @@ -50,14 +72,3 @@ class FileScope(str, Enum):
ITEM_PREVIEW = 'item_preview'
ITEM_RENAME = 'item_rename'
ITEM_SHARE = 'item_share'


class AccessToken(BaseObject):
def __init__(self, access_token: Union[None, str] = None, expires_in: Union[None, int] = None, token_type: Union[None, str] = None, restricted_to: Union[None, List[FileScope]] = None, refresh_token: Union[None, str] = None, issued_token_type: Union[None, str] = None, **kwargs):
super().__init__(**kwargs)
self.access_token = access_token
self.expires_in = expires_in
self.token_type = token_type
self.restricted_to = restricted_to
self.refresh_token = refresh_token
self.issued_token_type = issued_token_type
19 changes: 14 additions & 5 deletions box_sdk_gen/base_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ 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(key, value, annotation)
unpacked_attributes[mapping_field_name] = cls.__deserialize(
key, value, annotation
)
return cls(**unpacked_attributes)

def to_dict(self) -> dict:
Expand All @@ -24,7 +26,10 @@ def to_dict(self) -> dict:
if v is None:
continue
if type(v) is list:
value = [item.to_dict() if isinstance(item, BaseObject) else item for item in v]
value = [
item.to_dict() if isinstance(item, BaseObject) else item
for item in v
]
elif isinstance(v, BaseObject):
value = v.to_dict()
elif isinstance(v, Enum):
Expand All @@ -42,7 +47,9 @@ def __deserialize(cls, key, value, annotation=None):
if get_origin(annotation) == Optional:
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)]
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])

Expand All @@ -59,7 +66,9 @@ def __deserialize(cls, key, value, annotation=None):
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]
return [
cls.__deserialize(key, list_entry, list_type) for list_entry in value
]
except Exception:
return value

Expand All @@ -70,7 +79,7 @@ def __deserialize_union(cls, key, value, annotation):

type = None
for i, possible_type in enumerate(possible_types):
#remove special characters
# remove special characters
if type_field_value.replace("_", "") in possible_types[i].__name__.lower():
type = possible_types[i]
break
Expand Down
8 changes: 6 additions & 2 deletions box_sdk_gen/box_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@


class APIResponse:
def __init__(self, network_response: Response, reauthentication_needed, raised_exception):
def __init__(
self, network_response: Response, reauthentication_needed, raised_exception
):
self.network_response = network_response
self.reauthentication_needed = reauthentication_needed
self.raised_exception = raised_exception
Expand All @@ -24,7 +26,9 @@ def content(self) -> bytes:
def text(self) -> str:
return self.network_response.text

def get_header(self, header_name: str, default_value: Optional[str] = None) -> Optional[str]:
def get_header(
self, header_name: str, default_value: Optional[str] = None
) -> Optional[str]:
try:
return self.network_response.headers[header_name]
except (ValueError, KeyError):
Expand Down
41 changes: 25 additions & 16 deletions box_sdk_gen/ccg_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
from typing import Union, Optional

from .auth import Authentication
from .auth_schemas import TokenRequestBoxSubjectType, TokenRequest, TokenRequestGrantType, AccessToken
from .auth_schemas import (
TokenRequestBoxSubjectType,
TokenRequest,
TokenRequestGrantType,
)
from .fetch import fetch, FetchResponse, FetchOptions
from .network import NetworkSession
from .schemas import AccessToken


class CCGConfig:
def __init__(
self,
client_id: str,
client_secret: str,
enterprise_id: Union[None, str] = None,
user_id: Union[None, str] = None
self,
client_id: str,
client_secret: str,
enterprise_id: Union[None, str] = None,
user_id: Union[None, str] = None,
):
"""
:param client_id:
Expand Down Expand Up @@ -51,13 +56,13 @@ def __init__(


class CCGAuth(Authentication):
def __init__(self, config: CCGConfig):
def __init__(self, config: CCGConfig):
"""
:param config:
Configuration object of Client Credentials Grant auth.
"""
self.config = config
self.token: Union[None, str] = None
self.token: Union[None, AccessToken] = None

if config.user_id:
self.subject_id = self.config.user_id
Expand All @@ -66,17 +71,21 @@ def __init__(self, config: CCGConfig):
self.subject_type = TokenRequestBoxSubjectType.ENTERPRISE
self.subject_id = self.config.enterprise_id

def retrieve_token(self, network_session: Optional[NetworkSession] = None) -> str:
def retrieve_token(
self, network_session: Optional[NetworkSession] = None
) -> AccessToken:
"""
Return a current token or get a new one when not available.
:return:
Access token
"""
if self.token is None:
return self.refresh(network_session=network_session)
self.refresh_token(network_session=network_session)
return self.token

def refresh(self, network_session: Optional[NetworkSession] = None) -> str:
def refresh_token(
self, network_session: Optional[NetworkSession] = None
) -> AccessToken:
"""
Fetch a new access token
:return:
Expand All @@ -87,7 +96,7 @@ def refresh(self, network_session: Optional[NetworkSession] = None) -> str:
client_id=self.config.client_id,
client_secret=self.config.client_secret,
box_subject_id=self.subject_id,
box_subject_type=self.subject_type
box_subject_type=self.subject_type,
)

response: FetchResponse = fetch(
Expand All @@ -96,13 +105,13 @@ def refresh(self, network_session: Optional[NetworkSession] = None) -> str:
method='POST',
body=urlencode(request_body.to_dict()),
headers={'content-type': 'application/x-www-form-urlencoded'},
network_session=network_session
)
network_session=network_session,
),
)

token_response = AccessToken.from_dict(json.loads(response.text))
self.token = token_response.access_token
return self.token
self.token = token_response
return token_response

def as_user(self, user_id: str):
"""
Expand Down
Loading
Loading