pumpwood_communication.microservice_abc.simple
Facilitate communication with Pumpowood backend.
This packages facilitates the communication with end-points with Pumpwood pattern and helps with authentication.
Source-code at Github:
https://github.com/Murabei-OpenSource-Codes/pumpwood-communication
1""" 2Facilitate communication with Pumpowood backend. 3 4This packages facilitates the communication with end-points with Pumpwood 5pattern and helps with authentication. 6 7Source-code at Github:<br> 8https://github.com/Murabei-OpenSource-Codes/pumpwood-communication 9""" 10 11__docformat__ = "google" 12from .batch import ABCSimpleBatchMicroservice 13from .permission import ABCPermissionMicroservice 14 15__all__ = [ 16 ABCSimpleBatchMicroservice, ABCPermissionMicroservice]
class
ABCSimpleBatchMicroservice(abc.ABC):
8class ABCSimpleBatchMicroservice(ABC): 9 """Abstract class for batch end-points.""" 10 11 @staticmethod 12 def _build_aggregate_url(model_class: str): 13 return "rest/%s/aggregate/" % (model_class.lower(),) 14 15 def aggregate(self, model_class: str, group_by: List[str], agg: dict, 16 filter_dict: dict = {}, exclude_dict: dict = {}, 17 order_by: List[str] = [], auth_header: dict = None, 18 limit: int = None) -> pd.DataFrame: 19 """Save a list of objects with one request. 20 21 Args: 22 model_class (str): 23 Model class of the end-point that will be aggregated. 24 group_by (List[str]): 25 List of the fields that will be used on aggregation as 26 group by. 27 agg (dict): 28 A dictionary with dictionary itens as `field` and `function` 29 specifing the field that will be aggregated using a function. 30 filter_dict (dict): 31 Filter that will be applied before the aggregation. 32 exclude_dict (dict): 33 Exclude clause that will be applied before the aggregation. 34 order_by (list): 35 Ordenation acording to grouping elements. It can be used 36 fields created as keys of agg dictinary. 37 auth_header (dict): 38 Authentication header used to impersonation of user. 39 limit (int): 40 Limit number of returned row at aggregation query. 41 42 Returns: 43 Return a DataFrame with aggregation results. 44 """ 45 url_str = self._build_aggregate_url(model_class=model_class) 46 data = { 47 'agg': agg, 'group_by': group_by, 'filter_dict': filter_dict, 48 'exclude_dict': exclude_dict, 'order_by': order_by, 49 'limit': limit} 50 return self.request_post( 51 url=url_str, data=data, auth_header=auth_header)
Abstract class for batch end-points.
def
aggregate( self, model_class: str, group_by: List[str], agg: dict, filter_dict: dict = {}, exclude_dict: dict = {}, order_by: List[str] = [], auth_header: dict = None, limit: int = None) -> pandas.core.frame.DataFrame:
15 def aggregate(self, model_class: str, group_by: List[str], agg: dict, 16 filter_dict: dict = {}, exclude_dict: dict = {}, 17 order_by: List[str] = [], auth_header: dict = None, 18 limit: int = None) -> pd.DataFrame: 19 """Save a list of objects with one request. 20 21 Args: 22 model_class (str): 23 Model class of the end-point that will be aggregated. 24 group_by (List[str]): 25 List of the fields that will be used on aggregation as 26 group by. 27 agg (dict): 28 A dictionary with dictionary itens as `field` and `function` 29 specifing the field that will be aggregated using a function. 30 filter_dict (dict): 31 Filter that will be applied before the aggregation. 32 exclude_dict (dict): 33 Exclude clause that will be applied before the aggregation. 34 order_by (list): 35 Ordenation acording to grouping elements. It can be used 36 fields created as keys of agg dictinary. 37 auth_header (dict): 38 Authentication header used to impersonation of user. 39 limit (int): 40 Limit number of returned row at aggregation query. 41 42 Returns: 43 Return a DataFrame with aggregation results. 44 """ 45 url_str = self._build_aggregate_url(model_class=model_class) 46 data = { 47 'agg': agg, 'group_by': group_by, 'filter_dict': filter_dict, 48 'exclude_dict': exclude_dict, 'order_by': order_by, 49 'limit': limit} 50 return self.request_post( 51 url=url_str, data=data, auth_header=auth_header)
Save a list of objects with one request.
Arguments:
- model_class (str): Model class of the end-point that will be aggregated.
- group_by (List[str]): List of the fields that will be used on aggregation as group by.
- agg (dict): A dictionary with dictionary itens as
field
andfunction
specifing the field that will be aggregated using a function. - filter_dict (dict): Filter that will be applied before the aggregation.
- exclude_dict (dict): Exclude clause that will be applied before the aggregation.
- order_by (list): Ordenation acording to grouping elements. It can be used fields created as keys of agg dictinary.
- auth_header (dict): Authentication header used to impersonation of user.
- limit (int): Limit number of returned row at aggregation query.
Returns:
Return a DataFrame with aggregation results.
class
ABCPermissionMicroservice(abc.ABC):
7class ABCPermissionMicroservice(ABC): 8 """Abstract class for permission checking at pumpwood.""" 9 10 def check_if_logged(self, auth_header: dict = None) -> bool: 11 """Check if user is logged. 12 13 Args: 14 auth_header (dict): = None 15 AuthHeader to substitute the microservice original at 16 request. If not passed, microservice object auth_header 17 will be used. 18 19 Returns: 20 Return True if auth_header is looged and False if not 21 """ 22 try: 23 check = self.request_get( 24 url="rest/registration/check/", 25 auth_header=auth_header) 26 except PumpWoodUnauthorized: 27 return False 28 return check 29 30 def get_user_info(self, auth_header: dict = None) -> dict: 31 """Get user info. 32 33 Args: 34 auth_header (dict): = None 35 AuthHeader to substitute the microservice original at 36 request. If not passed, microservice object auth_header 37 will be used. 38 39 Returns: 40 A serialized user object with information of the logged user. 41 """ 42 user_info = self.request_get( 43 url="rest/registration/retrieveauthenticateduser/", 44 auth_header=auth_header, timeout=self.default_timeout) 45 return user_info 46 47 def check_permission(self, model_class: str, end_point: str, 48 extra_arg: str = None, allow_service_user: str = None, 49 allow_external: str = None, auth_header: dict = None, 50 ) -> dict: 51 """Get user info. 52 53 Args: 54 model_class (str): 55 Model class associated to be checked for access. 56 end_point (str): 57 Name of the end-point that will be checked for permission. Ex.: 58 retrieve, save, list, list-without-pag, ... 59 extra_arg (str): 60 Used on some end-points. On action end-point it is reponsible 61 for setting the action associated with the call. 62 allow_service_user: str = None: 63 64 allow_external: str = None: 65 66 auth_header (dict): 67 AuthHeader to substitute the microservice original at 68 request. If not passed, microservice object auth_header 69 will be used. 70 71 Returns: 72 A serialized user object with information of the logged user. 73 """ 74 # user_info = self.request_post( 75 # url="rest/registration/check/", 76 # payload={ 77 # 'end_point': end_point, 78 # 'first_arg': first_arg, 79 # 'second_arg': second_arg, 80 # 'api_config_allow': api_config_allow, 81 # 'api_config_deny': api_config_deny}, 82 # auth_header=auth_header, timeout=self.default_timeout) 83 # return user_info 84 return True
Abstract class for permission checking at pumpwood.
def
check_if_logged(self, auth_header: dict = None) -> bool:
10 def check_if_logged(self, auth_header: dict = None) -> bool: 11 """Check if user is logged. 12 13 Args: 14 auth_header (dict): = None 15 AuthHeader to substitute the microservice original at 16 request. If not passed, microservice object auth_header 17 will be used. 18 19 Returns: 20 Return True if auth_header is looged and False if not 21 """ 22 try: 23 check = self.request_get( 24 url="rest/registration/check/", 25 auth_header=auth_header) 26 except PumpWoodUnauthorized: 27 return False 28 return check
Check if user is logged.
Arguments:
- auth_header (dict): = None AuthHeader to substitute the microservice original at request. If not passed, microservice object auth_header will be used.
Returns:
Return True if auth_header is looged and False if not
def
get_user_info(self, auth_header: dict = None) -> dict:
30 def get_user_info(self, auth_header: dict = None) -> dict: 31 """Get user info. 32 33 Args: 34 auth_header (dict): = None 35 AuthHeader to substitute the microservice original at 36 request. If not passed, microservice object auth_header 37 will be used. 38 39 Returns: 40 A serialized user object with information of the logged user. 41 """ 42 user_info = self.request_get( 43 url="rest/registration/retrieveauthenticateduser/", 44 auth_header=auth_header, timeout=self.default_timeout) 45 return user_info
Get user info.
Arguments:
- auth_header (dict): = None AuthHeader to substitute the microservice original at request. If not passed, microservice object auth_header will be used.
Returns:
A serialized user object with information of the logged user.
def
check_permission( self, model_class: str, end_point: str, extra_arg: str = None, allow_service_user: str = None, allow_external: str = None, auth_header: dict = None) -> dict:
47 def check_permission(self, model_class: str, end_point: str, 48 extra_arg: str = None, allow_service_user: str = None, 49 allow_external: str = None, auth_header: dict = None, 50 ) -> dict: 51 """Get user info. 52 53 Args: 54 model_class (str): 55 Model class associated to be checked for access. 56 end_point (str): 57 Name of the end-point that will be checked for permission. Ex.: 58 retrieve, save, list, list-without-pag, ... 59 extra_arg (str): 60 Used on some end-points. On action end-point it is reponsible 61 for setting the action associated with the call. 62 allow_service_user: str = None: 63 64 allow_external: str = None: 65 66 auth_header (dict): 67 AuthHeader to substitute the microservice original at 68 request. If not passed, microservice object auth_header 69 will be used. 70 71 Returns: 72 A serialized user object with information of the logged user. 73 """ 74 # user_info = self.request_post( 75 # url="rest/registration/check/", 76 # payload={ 77 # 'end_point': end_point, 78 # 'first_arg': first_arg, 79 # 'second_arg': second_arg, 80 # 'api_config_allow': api_config_allow, 81 # 'api_config_deny': api_config_deny}, 82 # auth_header=auth_header, timeout=self.default_timeout) 83 # return user_info 84 return True
Get user info.
Arguments:
- model_class (str): Model class associated to be checked for access.
- end_point (str): Name of the end-point that will be checked for permission. Ex.: retrieve, save, list, list-without-pag, ...
- extra_arg (str): Used on some end-points. On action end-point it is reponsible for setting the action associated with the call.
- allow_service_user: str = None:
- allow_external: str = None:
- auth_header (dict): AuthHeader to substitute the microservice original at request. If not passed, microservice object auth_header will be used.
Returns:
A serialized user object with information of the logged user.