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

Flaw References and Comments support #14

Merged
merged 5 commits into from
Jun 28, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 47 additions & 10 deletions osidb_bindings/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ def __init__(self, base_url, auth=None, verify_ssl=True):
"create",
"search",
),
subresources={
"comments": {"allowed_operations": ["retrieve", "list", "create"]},
"references": {
"allowed_operations": [
"retrieve",
"update",
"list",
"create",
"destroy",
]
},
},
)
self.affects = SessionOperationsGroup(
self.__get_client_with_new_access_token,
Expand Down Expand Up @@ -184,19 +196,36 @@ def __init__(
client: Callable,
resource_name: str,
allowed_operations: List[str] = ALL_SESSION_OPERATIONS,
subresources: Dict[str, dict] = None,
):
self.client = client
self.resource_name = resource_name
self.allowed_operations = allowed_operations

if subresources is None:
subresources = {}

# Create a session operation group for each subresource with respective
# allowed session operations
for subresource_name, subresource_metadata in subresources.items():
setattr(
self,
subresource_name,
SessionOperationsGroup(
client,
resource_name=f"{resource_name}_{subresource_name}",
**subresource_metadata,
),
)

def __get_method_module(self, resource_name: str, method: str) -> ModuleType:
# import endpoint module based on a method
return importlib.import_module(
f"{OSIDB_BINDINGS_API_PATH}.osidb_api_{OSIDB_API_VERSION}_{resource_name}_{method}",
package="osidb_bindings",
)

def __make_iterable(self, response, **kwargs):
def __make_iterable(self, response, *args, **kwargs):
"""
Populate next, prev and iterator helper methods for paginated responses
"""
Expand All @@ -219,46 +248,48 @@ def __make_iterable(self, response, **kwargs):
if offset is not None:
kwargs["offset"] = offset.group(1)

setattr(response, func_name, partial(self.retrieve_list, **kwargs))
setattr(
response, func_name, partial(self.retrieve_list, *args, **kwargs)
)

return response

# CRUD operations

def retrieve(self, id, **kwargs):
def retrieve(self, id, *args, **kwargs):
if "retrieve" in self.allowed_operations:
method_module = self.__get_method_module(
resource_name=self.resource_name, method="retrieve"
)
sync_fn = get_sync_function(method_module)
return sync_fn(id, client=self.client(), **kwargs)
return sync_fn(id, *args, client=self.client(), **kwargs)
else:
raise OperationUnsupported(
'Operation "update" is not supported for the '
f'"{self.resource_name}" resource.'
)

def retrieve_list(self, **kwargs):
def retrieve_list(self, *args, **kwargs):
if "list" in self.allowed_operations:
method_module = self.__get_method_module(
resource_name=self.resource_name, method="list"
)
sync_fn = get_sync_function(method_module)
return self.__make_iterable(
sync_fn(client=self.client(), **kwargs), **kwargs
sync_fn(*args, client=self.client(), **kwargs), *args, **kwargs
)
else:
raise OperationUnsupported(
'Operation "update" is not supported for the '
f'"{self.resource_name}" resource.'
)

def create(self, form_data: Dict[str, Any]):
def create(self, form_data: Dict[str, Any], *args, **kwargs):
if "create" in self.allowed_operations:
method_module = self.__get_method_module(
resource_name=self.resource_name, method="create"
)
model = getattr(method_module, "REQUEST_BODY_TYPEs", None)
model = getattr(method_module, "REQUEST_BODY_TYPE", None)
if model is None:
raise UndefinedRequestBody(
"There is no defined request body "
Expand All @@ -269,18 +300,20 @@ def create(self, form_data: Dict[str, Any]):
transformed_data = model.from_dict(form_data)
sync_fn = get_sync_function(method_module)
return sync_fn(
*args,
client=self.client(),
form_data=transformed_data,
multipart_data=UNSET,
json_body=UNSET,
**kwargs,
)
else:
raise OperationUnsupported(
'Operation "update" is not supported for the '
f'"{self.resource_name}" resource.'
)

def update(self, id, form_data: Dict[str, Any]):
def update(self, id, form_data: Dict[str, Any], *args, **kwargs):
if "update" in self.allowed_operations:
method_module = self.__get_method_module(
resource_name=self.resource_name, method="update"
Expand All @@ -297,26 +330,30 @@ def update(self, id, form_data: Dict[str, Any]):
sync_fn = get_sync_function(method_module)
return sync_fn(
id,
*args,
client=self.client(),
form_data=transformed_data,
multipart_data=UNSET,
json_body=UNSET,
**kwargs,
)
else:
raise OperationUnsupported(
'Operation "update" is not supported for the '
f'"{self.resource_name}" resource.'
)

def delete(self, id):
def delete(self, id, *args, **kwargs):
JakubFrejlach marked this conversation as resolved.
Show resolved Hide resolved
if "destroy" in self.allowed_operations:
method_module = self.__get_method_module(
resource_name=self.resource_name, method="destroy"
)
sync_fn = get_sync_function(method_module)
return sync_fn(
id,
*args,
client=self.client(),
**kwargs,
)
else:
raise OperationUnsupported(
Expand Down