def validate_response(self, context): """Validates an HTTP response by checking for global errors. Args: context (HttpContext): The HttpContext of the API call. """ #CohesityPatch if (context.response.status_code < 200) or (context.response.status_code > 208): # [200,208] = HTTP OK status = context.response.status_code message = jsonpickle.decode(context.response.raw_body)['message'] raise_except = APIException('Response status code: %s, Response message: %s' % (status, message), context) raise raise_except
def validate_response(self, context): """Validates an HTTP response by checking for global errors. Args: context (HttpContext): The HttpContext of the API call. """ #CohesityPatch if (context.response.status_code < 200) or ( context.response.status_code > 208): # [200,208] = HTTP OK status = context.response.status_code message = jsonpickle.decode(context.response.raw_body)['message'] raise_except = APIException( 'Response status code: %s, Response message: %s' % (status, message), context) if context.response.status_code == 401 and \ json.loads(context.response.raw_body)['errorCode'] == 'KStatusUnauthorized' and \ json.loads(context.response.raw_body)['message'] == "The access token is invalid.": raise_except = ExpiredTokenException( 'Response status code: %s, Response message: %s' % (status, message), context) raise raise_except
def get_cluster_partition_by_id(self, id): """Does a GET request to /public/clusterPartitions/{id}. Returns the Cluster Partition corresponding to the specified Cluster Partition Id. Args: id (long|int): Specifies a unique id of the Cluster Partition to return. Returns: ClusterPartition: Response from the API. Success Raises: APIException: When an error occurs while fetching the data from the remote API. This exception includes the HTTP Response code, an error message, and the HTTP body that was received in the request. """ try: self.logger.info('get_cluster_partition_by_id called.') # Validate required parameters self.logger.info( 'Validating required parameters for get_cluster_partition_by_id.' ) self.validate_parameters(id=id) # Prepare query URL self.logger.info( 'Preparing query URL for get_cluster_partition_by_id.') _url_path = '/public/clusterPartitions/{id}' _url_path = APIHelper.append_url_with_template_parameters( _url_path, {'id': id}) _query_builder = Configuration.get_base_uri() _query_builder += _url_path _query_url = APIHelper.clean_url(_query_builder) # Prepare headers self.logger.info( 'Preparing headers for get_cluster_partition_by_id.') _headers = {'accept': 'application/json'} # Prepare and execute request self.logger.info( 'Preparing and executing request for get_cluster_partition_by_id.' ) _request = self.http_client.get(_query_url, headers=_headers) AuthManager.apply(_request) _context = self.execute_request(_request, name='get_cluster_partition_by_id') # Endpoint and global error handling using HTTP status codes. self.logger.info( 'Validating response for get_cluster_partition_by_id.') if _context.response.status_code == 404: raise APIException('Not Found', _context) elif (_context.response.status_code < 200) or (_context.response.status_code > 208): raise ErrorErrorException('Error', _context) self.validate_response(_context) # Return appropriate type return APIHelper.json_deserialize(_context.response.raw_body, ClusterPartition.from_dictionary) except Exception as e: self.logger.error(e, exc_info=True) raise
def get_view_box_by_id(self, id, fetch_stats=None): """Does a GET request to /public/viewBoxes/{id}. Returns the Domain (View Box) corresponding to the specified Domain (View Box) Id. Args: id (long|int): Id of the Storage Domain (View Box) fetch_stats (bool, optional): Specifies whether to include usage and performance statistics. Returns: ViewBox: Response from the API. Success Raises: APIException: When an error occurs while fetching the data from the remote API. This exception includes the HTTP Response code, an error message, and the HTTP body that was received in the request. """ try: self.logger.info('get_view_box_by_id called.') # Validate required parameters self.logger.info( 'Validating required parameters for get_view_box_by_id.') self.validate_parameters(id=id) # Prepare query URL self.logger.info('Preparing query URL for get_view_box_by_id.') _url_path = '/public/viewBoxes/{id}' _url_path = APIHelper.append_url_with_template_parameters( _url_path, {'id': id}) _query_builder = self.config.get_base_uri() _query_builder += _url_path _query_parameters = {'fetchStats': fetch_stats} _query_builder = APIHelper.append_url_with_query_parameters( _query_builder, _query_parameters, Configuration.array_serialization) _query_url = APIHelper.clean_url(_query_builder) # Prepare headers self.logger.info('Preparing headers for get_view_box_by_id.') _headers = {'accept': 'application/json'} # Prepare and execute request self.logger.info( 'Preparing and executing request for get_view_box_by_id.') _request = self.http_client.get(_query_url, headers=_headers) AuthManager.apply(_request, self.config) _context = self.execute_request(_request, name='get_view_box_by_id') # Endpoint and global error handling using HTTP status codes. self.logger.info('Validating response for get_view_box_by_id.') if _context.response.status_code == 404: raise APIException('Not Found', _context) elif (_context.response.status_code < 200) or (_context.response.status_code > 208): raise RequestErrorErrorException('Error', _context) self.validate_response(_context) # Return appropriate type return APIHelper.json_deserialize(_context.response.raw_body, ViewBox.from_dictionary) except Exception as e: self.logger.error(e, exc_info=True) raise