diff --git a/README.md b/README.md index 9178bf6..3f13a06 100644 --- a/README.md +++ b/README.md @@ -457,6 +457,30 @@ with SteamClient('MY_API_KEY', 'MY_USERNAME', 'MY_PASSWORD', 'PATH_TO_STEAMGUARD assert type(on_hold_wallet_balance) == Decimal ``` + +**add_friend(steam_id: str) -> bool** + +**accept_friend(steam_id: str) -> bool** + +**remove_friend(steam_id: str) -> bool** + +Using `SteamClient.login` method is required before usage. +These methods perform the operations of adding, accepting and deleting friends using the provided `steam_id` of the friend. +If the request was successful, True is returned, otherwise False. +`remove_friend` method returns True even if `steam_id` has never been a friend + +Example: +```python +from steampy.client import SteamClient + + +steam_client = SteamClient('MY_API_KEY') +steam_client.login('MY_USERNAME', 'MY_PASSWORD', 'PATH_TO_STEAMGUARD_FILE') + +# if response True - was success +response = steam_client.add_friend("FRIEND_STEAM_ID") +``` + market methods ============== diff --git a/steampy/client.py b/steampy/client.py index ece1057..ebfb300 100644 --- a/steampy/client.py +++ b/steampy/client.py @@ -416,3 +416,50 @@ def get_wallet_balance(self, convert_to_decimal: bool = True, on_hold: bool = Fa return Decimal(balance_dict[balance_dict_key]) / 100 else: return balance_dict[balance_dict_key] + + @login_required + def add_friend(self, steam_id: str) -> bool: + response = self._friend_ajax_request(steam_id) + return True if response else False + + @login_required + def accept_friend(self, steam_id: str) -> bool: + return self._friend_ajax_request(steam_id, accept=1) + + _headers = { + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' + } + + def _friend_ajax_request(self, steam_id: str, accept: int = 0) -> dict: + url = f'{SteamUrl.COMMUNITY_URL}/actions/AddFriendAjax' + data = { + 'sessionID': self._get_session_id(), + 'steamid': steam_id, + 'accept_invite': accept + } + response = self._session.post(url, data=data, headers=self._headers) + + if response.status_code != 200: + raise ApiException( + f'Request failed with status code {response.status_code}') + + # returns True or json object if request was succes, otherwise False + return response.json() + + @login_required + def remove_friend(self, steam_id: str) -> bool: + url = f'{SteamUrl.COMMUNITY_URL}/actions/RemoveFriendAjax' + data = { + 'sessionID': self._get_session_id(), + 'steamid': steam_id + } + response = self._session.post(url, data=data, headers=self._headers) + + if response.status_code != 200: + raise ApiException( + f'Request failed with status code {response.status_code}') + + # returns True if request was succes, otherwise False + # returns True even if `steam_id` has never been a friend + return response.json() +