Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 15 additions & 15 deletions linkedin_api/clients/restli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -834,7 +834,7 @@ def action(
formatter=ActionResponseFormatter,
)

def __send_and_format_response(
def _send_and_format_response(
self,
*,
restli_method: RESTLI_METHODS,
Expand Down
8 changes: 5 additions & 3 deletions linkedin_api/clients/restli/response_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
40 changes: 20 additions & 20 deletions linkedin_api/clients/restli/utils/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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
"""
Expand All @@ -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):
Expand All @@ -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))".
Expand Down Expand Up @@ -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

Expand All @@ -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
)
)
Expand All @@ -162,15 +162,15 @@ 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
idx = end_idx + 1
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.

Expand All @@ -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
Expand All @@ -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
)

Expand All @@ -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
Expand Down
18 changes: 9 additions & 9 deletions linkedin_api/clients/restli/utils/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())]
)
Expand All @@ -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())
Expand Down