From 95877dd1eea70c5613375ef23a77c2054db91329 Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Sat, 22 May 2021 14:44:16 -0700 Subject: [PATCH 1/2] Add methods to claim and unclaim a server --- plexapi/server.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/plexapi/server.py b/plexapi/server.py index 4b9c7e886..cd94405a1 100644 --- a/plexapi/server.py +++ b/plexapi/server.py @@ -193,6 +193,26 @@ def account(self): data = self.query(Account.key) return Account(self, data) + def claim(self, account): + """ Claim the Plex server using a :class:`~plexapi.myplex.MyPlexAccount`. + This will only work with an unclaimed server on localhost or the same subnet. + + Parameters: + account (:class:`~plexapi.myplex.MyPlexAccount`): The account used to + claim the server. + """ + key = '/myplex/claim' + params = {'token': account.claimToken()} + data = self.query(key, method=self._session.post, params=params) + return Account(self, data) + + def unclaim(self): + """ Unclaim the Plex server. This will remove the server from your + :class:`~plexapi.myplex.MyPlexAccount`. + """ + data = self.query(Account.key, method=self._session.delete) + return Account(self, data) + @property def activities(self): """Returns all current PMS activities.""" @@ -209,7 +229,7 @@ def agents(self, mediaType=None): return self.fetchItems(key) def createToken(self, type='delegation', scope='all'): - """Create a temp access token for the server.""" + """ Create a temp access token for the server. """ if not self._token: # Handle unclaimed servers return None From 2ef3f595f0e0f1580a425d8cb682f1b5028094a3 Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Sat, 22 May 2021 14:36:47 -0700 Subject: [PATCH 2/2] Add test for claiming and unclaiming server --- tests/test_server.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_server.py b/tests/test_server.py index 0b68d0dd3..f252bfda3 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -308,6 +308,16 @@ def test_server_account(plex): assert re.match(utils.REGEX_EMAIL, account.username) +@pytest.mark.authenticated +def test_server_claim_unclaim(plex, account): + server_account = plex.account() + assert server_account.signInState == 'ok' + result = plex.unclaim() + assert result.signInState == 'none' + result = plex.claim(account) + assert result.signInState == 'ok' + + def test_server_downloadLogs(tmpdir, plex): plex.downloadLogs(savepath=str(tmpdir), unpack=True) assert len(tmpdir.listdir()) > 1