diff --git a/linkedin_api/clients/restli/client.py b/linkedin_api/clients/restli/client.py index 5020032..b622c37 100644 --- a/linkedin_api/clients/restli/client.py +++ b/linkedin_api/clients/restli/client.py @@ -96,7 +96,7 @@ def get( encoded_query_param_string = encode_query_params_for_get_requests(query_params) - return self.__send_and_format_response( + return self._send_and_format_response( restli_method=RESTLI_METHODS.GET, resource_path=resource_path, path_keys=path_keys, @@ -147,7 +147,7 @@ def batch_get( query_params_final ) - return self.__send_and_format_response( + return self._send_and_format_response( restli_method=RESTLI_METHODS.BATCH_GET, resource_path=resource_path, path_keys=path_keys, @@ -194,7 +194,7 @@ def get_all( """ encoded_query_param_string = encode_query_params_for_get_requests(query_params) - return self.__send_and_format_response( + return self._send_and_format_response( restli_method=RESTLI_METHODS.GET_ALL, resource_path=resource_path, path_keys=path_keys, @@ -255,7 +255,7 @@ def finder( final_query_params ) - return self.__send_and_format_response( + return self._send_and_format_response( restli_method=RESTLI_METHODS.FINDER, resource_path=resource_path, path_keys=path_keys, @@ -322,7 +322,7 @@ def batch_finder( final_query_params ) - return self.__send_and_format_response( + return self._send_and_format_response( restli_method=RESTLI_METHODS.BATCH_FINDER, resource_path=resource_path, path_keys=path_keys, @@ -371,7 +371,7 @@ def create( encoded_query_param_string = encoder.param_encode(query_params) - return self.__send_and_format_response( + return self._send_and_format_response( restli_method=RESTLI_METHODS.CREATE, resource_path=resource_path, path_keys=path_keys, @@ -431,7 +431,7 @@ def batch_create( encoded_query_param_string = encoder.param_encode(query_params) request_body = {"elements": entities} - return self.__send_and_format_response( + return self._send_and_format_response( restli_method=RESTLI_METHODS.BATCH_CREATE, resource_path=resource_path, path_keys=path_keys, @@ -488,7 +488,7 @@ def update( encoded_query_param_string = encoder.param_encode(query_params) - return self.__send_and_format_response( + return self._send_and_format_response( restli_method=RESTLI_METHODS.UPDATE, resource_path=resource_path, path_keys=path_keys, @@ -550,7 +550,7 @@ def batch_update( entities_map = dict(zip(encoded_ids, entities)) request_body = {"entities": entities_map} - return self.__send_and_format_response( + return self._send_and_format_response( restli_method=RESTLI_METHODS.BATCH_UPDATE, resource_path=resource_path, path_keys=path_keys, @@ -604,7 +604,7 @@ def partial_update( request_body = {"patch": {"$set": patch_set_object}} - return self.__send_and_format_response( + return self._send_and_format_response( restli_method=RESTLI_METHODS.PARTIAL_UPDATE, resource_path=resource_path, path_keys=path_keys, @@ -671,7 +671,7 @@ def batch_partial_update( } request_body = {"entities": entities_map} - return self.__send_and_format_response( + return self._send_and_format_response( restli_method=RESTLI_METHODS.BATCH_PARTIAL_UPDATE, resource_path=resource_path, path_keys=path_keys, @@ -716,7 +716,7 @@ def delete( encoded_query_param_string = encoder.param_encode(query_params) - return self.__send_and_format_response( + return self._send_and_format_response( restli_method=RESTLI_METHODS.DELETE, resource_path=resource_path, path_keys=path_keys, @@ -764,7 +764,7 @@ def batch_delete( final_query_params.update({"ids": ids}) encoded_query_param_string = encoder.param_encode(final_query_params) - return self.__send_and_format_response( + return self._send_and_format_response( resource_path=resource_path, path_keys=path_keys, encoded_query_param_string=encoded_query_param_string, @@ -823,7 +823,7 @@ def action( request_body = action_params if action_params else {} - return self.__send_and_format_response( + return self._send_and_format_response( restli_method=RESTLI_METHODS.ACTION, resource_path=resource_path, path_keys=path_keys, @@ -834,7 +834,7 @@ def action( formatter=ActionResponseFormatter, ) - def __send_and_format_response( + def _send_and_format_response( self, *, restli_method: RESTLI_METHODS, diff --git a/linkedin_api/clients/restli/response_formatter.py b/linkedin_api/clients/restli/response_formatter.py index 0f7da7b..a6c0c52 100644 --- a/linkedin_api/clients/restli/response_formatter.py +++ b/linkedin_api/clients/restli/response_formatter.py @@ -140,9 +140,11 @@ class BatchCreateResponseFormatter(BaseResponseFormatter[BatchCreateResponse]): def format_response(cls, response: Response) -> BatchCreateResponse: json_data = response.json() elements = json_data.get("elements", None) - batch_create_results = [ - cls.format_batch_create_result(result) for result in elements - ] + batch_create_results = ( + [cls.format_batch_create_result(result) for result in elements] + if elements + else [] + ) return BatchCreateResponse( status_code=response.status_code, diff --git a/linkedin_api/clients/restli/utils/decoder.py b/linkedin_api/clients/restli/utils/decoder.py index 024e5d5..1a175d3 100644 --- a/linkedin_api/clients/restli/utils/decoder.py +++ b/linkedin_api/clients/restli/utils/decoder.py @@ -30,7 +30,7 @@ def decode(value: str) -> Union[Dict[str, Any], List[Any], str]: Returns: Union[Dict[str,Any], List[Any], str]: The decoded value """ - return __internal_decode(value, False) + return _internal_decode(value, False) def reduced_decode(value: str) -> Union[Dict[str, Any], List[Any], str]: @@ -43,10 +43,10 @@ def reduced_decode(value: str) -> Union[Dict[str, Any], List[Any], str]: Returns: Union[Dict[str,Any], List[Any], str]: The decoded value """ - return __internal_decode(value, True) + return _internal_decode(value, True) -def __validateSuffix(restli_encoded_str: str, suffix: str): +def _validateSuffix(restli_encoded_str: str, suffix: str): """ Validates that the input restli_encoded_str has the expected suffix at the end """ @@ -56,7 +56,7 @@ def __validateSuffix(restli_encoded_str: str, suffix: str): ) -def __restli_unescape(value: str, reduced: bool): +def _restli_unescape(value: str, reduced: bool): if not reduced: value = unquote(value) elif re.search(reduced_decode_special_chars_pattern, value): @@ -68,7 +68,7 @@ def __restli_unescape(value: str, reduced: bool): return value -def __find_last_right_bracket(value: str, pos: int) -> int: +def _find_last_right_bracket(value: str, pos: int) -> int: """ Returns the index of the last right, matching bracket, starting from specified index. For example, consider value = "List(1,(k:v))". @@ -111,21 +111,21 @@ def __find_last_right_bracket(value: str, pos: int) -> int: return idx -def __internal_decode(restli_encoded_str: str, reduced: bool): +def _internal_decode(restli_encoded_str: str, reduced: bool): if (restli_encoded_str is None) or (restli_encoded_str == "''"): restli_encoded_str = "" if restli_encoded_str.startswith(LIST_PREFIX): - __validateSuffix(restli_encoded_str, LIST_SUFFIX) - return __decode_list(restli_encoded_str[5:-1], reduced) + _validateSuffix(restli_encoded_str, LIST_SUFFIX) + return _decode_list(restli_encoded_str[5:-1], reduced) elif restli_encoded_str.startswith(OBJ_PREFIX): - __validateSuffix(restli_encoded_str, OBJ_SUFFIX) - return __decode_object(restli_encoded_str[1:-1], reduced) + _validateSuffix(restli_encoded_str, OBJ_SUFFIX) + return _decode_object(restli_encoded_str[1:-1], reduced) else: - return __restli_unescape(restli_encoded_str, reduced) + return _restli_unescape(restli_encoded_str, reduced) -def __decode_list(restli_encoded_str: str, reduced: bool) -> List[Any]: +def _decode_list(restli_encoded_str: str, reduced: bool) -> List[Any]: """ Decodes a Rest.li-encoded string to a list @@ -147,9 +147,9 @@ def __decode_list(restli_encoded_str: str, reduced: bool) -> List[Any]: restli_encoded_str[idx:].startswith(OBJ_PREFIX) ): # If we encounter a List or Object as one of the current list's entries, append the decoded value - right_bracket_idx = __find_last_right_bracket(restli_encoded_str, idx) + right_bracket_idx = _find_last_right_bracket(restli_encoded_str, idx) decoded_list.append( - __internal_decode( + _internal_decode( restli_encoded_str[idx : right_bracket_idx + 1], reduced ) ) @@ -162,7 +162,7 @@ def __decode_list(restli_encoded_str: str, reduced: bool) -> List[Any]: if end_idx < 0: end_idx = len(restli_encoded_str) decoded_list.append( - __restli_unescape(restli_encoded_str[idx:end_idx], reduced) + _restli_unescape(restli_encoded_str[idx:end_idx], reduced) ) # Move past the comma @@ -170,7 +170,7 @@ def __decode_list(restli_encoded_str: str, reduced: bool) -> List[Any]: return decoded_list -def __decode_object(restli_encoded_str: str, reduced: bool) -> Dict[str, Any]: +def _decode_object(restli_encoded_str: str, reduced: bool) -> Dict[str, Any]: """ Decodes a Rest.li-encoded string to an object. @@ -189,7 +189,7 @@ def __decode_object(restli_encoded_str: str, reduced: bool) -> Dict[str, Any]: while idx < len(restli_encoded_str): # Get the key value between the start index and key-val separator (:) colon_idx = restli_encoded_str.find(OBJ_KEY_VAL_SEP, idx) - key = __restli_unescape(restli_encoded_str[idx:colon_idx], reduced) + key = _restli_unescape(restli_encoded_str[idx:colon_idx], reduced) # Move to the next character after the colon idx = colon_idx + 1 @@ -198,8 +198,8 @@ def __decode_object(restli_encoded_str: str, reduced: bool) -> Dict[str, Any]: restli_encoded_str[idx:].startswith(OBJ_PREFIX) ): # If we encounter a List or Object as the key's value, decode it - right_bracket_idx = __find_last_right_bracket(restli_encoded_str, idx) - decoded_object[key] = __internal_decode( + right_bracket_idx = _find_last_right_bracket(restli_encoded_str, idx) + decoded_object[key] = _internal_decode( restli_encoded_str[idx : right_bracket_idx + 1], reduced ) @@ -211,7 +211,7 @@ def __decode_object(restli_encoded_str: str, reduced: bool) -> Dict[str, Any]: if end_idx < 0: end_idx = len(restli_encoded_str) - decoded_object[key] = __restli_unescape( + decoded_object[key] = _restli_unescape( restli_encoded_str[idx:end_idx], reduced ) # end_idx is the comma index, so move 1 past it diff --git a/linkedin_api/clients/restli/utils/encoder.py b/linkedin_api/clients/restli/utils/encoder.py index c9ccce8..3151f58 100644 --- a/linkedin_api/clients/restli/utils/encoder.py +++ b/linkedin_api/clients/restli/utils/encoder.py @@ -27,7 +27,7 @@ def param_encode(raw_query_params_map: Optional[Dict[str, Any]]) -> str: if raw_query_params_map is None: return "" - query_params_map = __encode_query_param_map(raw_query_params_map) + query_params_map = _encode_query_param_map(raw_query_params_map) return "&".join( [f"{key}={query_params_map[key]}" for key in sorted(query_params_map.keys())] ) @@ -48,32 +48,32 @@ def encode(value: Union[bool, str, int, float, List, Dict]) -> str: elif isinstance(value, bool): return "true" if value else "false" elif isinstance(value, str): - return __encode_string(value) + return _encode_string(value) elif isinstance(value, list): - return __encode_list(value) + return _encode_list(value) elif isinstance(value, dict): - return __encode_dict(value) + return _encode_dict(value) else: # Everything else (e.g. int, float) return str(value) -def __encode_query_param_map(raw_query_params_map: Dict[str, Any]) -> Dict: +def _encode_query_param_map(raw_query_params_map: Dict[str, Any]) -> Dict: # Return a Dict with the input keys and values encoded - return {__encode_string(k): encode(v) for (k, v) in raw_query_params_map.items()} + return {_encode_string(k): encode(v) for (k, v) in raw_query_params_map.items()} -def __encode_string(value: str) -> str: +def _encode_string(value: str) -> str: # Perform standard URL-encoding on strings return quote(value, safe="") -def __encode_list(value: List[Any]) -> str: +def _encode_list(value: List[Any]) -> str: # Encode a list return f"{LIST_PREFIX}{LIST_ITEM_SEP.join(encode(el) for el in value)}{LIST_SUFFIX}" -def __encode_dict(value: Dict[str, Any]) -> str: +def _encode_dict(value: Dict[str, Any]) -> str: # Encode a dict by encoding both key and value, both of which can be complex key_values = OBJ_KEY_VAL_PAIR_SEP.join( f"{encode(k)}{OBJ_KEY_VAL_SEP}{encode(v)}" for (k, v) in sorted(value.items())