Skip to content

Commit

Permalink
Add MyPlexAccount.ping() to refresh authentication token (#1271)
Browse files Browse the repository at this point in the history
* Add `MyPlexAccount.ping()` to refresh auth token

* Test account ping
  • Loading branch information
JonnyWong16 committed Nov 3, 2023
1 parent 7f5cf76 commit 253949c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
10 changes: 10 additions & 0 deletions plexapi/myplex.py
Expand Up @@ -108,6 +108,7 @@ class MyPlexAccount(PlexObject):
OPTOUTS = 'https://plex.tv/api/v2/user/{userUUID}/settings/opt_outs' # get
LINK = 'https://plex.tv/api/v2/pins/link' # put
VIEWSTATESYNC = 'https://plex.tv/api/v2/user/view_state_sync' # put
PING = 'https://plex.tv/api/v2/ping'
# Hub sections
VOD = 'https://vod.provider.plex.tv' # get
MUSIC = 'https://music.provider.plex.tv' # get
Expand Down Expand Up @@ -250,6 +251,15 @@ def query(self, url, method=None, headers=None, timeout=None, **kwargs):
data = response.text.encode('utf8')
return ElementTree.fromstring(data) if data.strip() else None

def ping(self):
""" Ping the Plex.tv API.
This will refresh the authentication token to prevent it from expiring.
"""
pong = self.query(self.PING)
if pong is not None:
return utils.cast(bool, pong.text)
return False

def device(self, name=None, clientId=None):
""" Returns the :class:`~plexapi.myplex.MyPlexDevice` that matches the name specified.
Expand Down
4 changes: 4 additions & 0 deletions tests/test_myplex.py
Expand Up @@ -361,3 +361,7 @@ def test_myplex_pin(account, plex):

def test_myplex_geoip(account):
assert account.geoip(account.publicIP())


def test_myplex_ping(account):
assert account.ping()

0 comments on commit 253949c

Please sign in to comment.