From 7b7b3f511216ce80fffebea5c975c5975aa47618 Mon Sep 17 00:00:00 2001 From: Dr-Blank <64108942+Dr-Blank@users.noreply.github.com> Date: Fri, 29 Dec 2023 02:45:30 -0500 Subject: [PATCH 1/5] Sort Imports --- plexauth.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/plexauth.py b/plexauth.py index f09e8e3..447b84b 100755 --- a/plexauth.py +++ b/plexauth.py @@ -1,9 +1,12 @@ """Handle a Plex.tv authorization flow to obtain an access token.""" -import aiohttp -from asyncio import sleep -from datetime import datetime, timedelta + import urllib.parse import uuid +from asyncio import sleep +from datetime import datetime, timedelta +from typing import Any, Dict, Optional + +import aiohttp __version__ = '0.0.5' From d6550e34c102a854486bd2c91b2a5fb88d853f0a Mon Sep 17 00:00:00 2001 From: Dr-Blank <64108942+Dr-Blank@users.noreply.github.com> Date: Fri, 29 Dec 2023 02:49:06 -0500 Subject: [PATCH 2/5] make it pass for pyright and mypy --- plexauth.py | 64 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/plexauth.py b/plexauth.py index 447b84b..2e5ddb9 100755 --- a/plexauth.py +++ b/plexauth.py @@ -8,60 +8,70 @@ import aiohttp -__version__ = '0.0.5' +__version__ = "0.0.5" -CODES_URL = 'https://plex.tv/api/v2/pins.json?strong=true' -AUTH_URL = 'https://app.plex.tv/auth#!?{}' -TOKEN_URL = 'https://plex.tv/api/v2/pins/{}' +CODES_URL = "https://plex.tv/api/v2/pins.json?strong=true" +AUTH_URL = "https://app.plex.tv/auth#!?{}" +TOKEN_URL = "https://plex.tv/api/v2/pins/{}" -class PlexAuth(): - def __init__(self, payload, session=None, headers=None): - '''Create PlexAuth instance.''' +class PlexAuth: + def __init__( + self, + payload: Dict[str, str], + session: Optional[aiohttp.ClientSession] = None, + headers: Any = None, + ): + """Create PlexAuth instance.""" self.client_identifier = str(uuid.uuid4()) self._code = None self._headers = headers self._identifier = None self._payload = payload - self._payload['X-Plex-Client-Identifier'] = self.client_identifier + self._payload["X-Plex-Client-Identifier"] = self.client_identifier self._local_session = False - self._session = session if session is None: - self._session = aiohttp.ClientSession() + session = aiohttp.ClientSession() self._local_session = True + self._session = session async def initiate_auth(self): - '''Request codes needed to create an auth URL. Starts external timeout.''' - async with self._session.post(CODES_URL, data=self._payload, headers=self._headers) as resp: + """Request codes needed to create an auth URL. + Starts external timeout. + """ + async with self._session.post( + CODES_URL, data=self._payload, headers=self._headers + ) as resp: response = await resp.json() - self._code = response['code'] - self._identifier = response['id'] + self._code = response["code"] + self._identifier = response["id"] - def auth_url(self, forward_url=None): - '''Return an auth URL for the user to follow.''' + def auth_url(self, forward_url: Optional[str] = None): + """Return an auth URL for the user to follow.""" parameters = { - 'clientID': self.client_identifier, - 'code': self._code, + "clientID": self.client_identifier, + "code": self._code, } if forward_url: - parameters['forwardUrl'] = forward_url + parameters["forwardUrl"] = forward_url url = AUTH_URL.format(urllib.parse.urlencode(parameters)) return url async def request_auth_token(self): - '''Request an auth token from Plex.''' - token_url = TOKEN_URL.format(self._code) + """Request an auth token from Plex.""" payload = dict(self._payload) - payload['Accept'] = 'application/json' - async with self._session.get(TOKEN_URL.format(self._identifier), headers=payload) as resp: + payload["Accept"] = "application/json" + async with self._session.get( + TOKEN_URL.format(self._identifier), headers=payload + ) as resp: response = await resp.json() - token = response['authToken'] + token = response["authToken"] return token - async def token(self, timeout=60): - '''Poll Plex endpoint until a token is retrieved or times out.''' + async def token(self, timeout: int = 60): + """Poll Plex endpoint until a token is retrieved or times out.""" token = None wait_until = datetime.now() + timedelta(seconds=timeout) break_loop = False @@ -82,6 +92,6 @@ async def __aenter__(self): """Async enter.""" return self - async def __aexit__(self, *exc_info): + async def __aexit__(self, *_): """Async exit.""" await self.close() From 72d7e45b96f5072ecf089c1058cd0585b5dabb20 Mon Sep 17 00:00:00 2001 From: Dr-Blank <64108942+Dr-Blank@users.noreply.github.com> Date: Fri, 29 Dec 2023 02:55:46 -0500 Subject: [PATCH 3/5] make packege typed --- py.typed | 0 setup.py | 4 +++- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 py.typed diff --git a/py.typed b/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/setup.py b/setup.py index c37ddde..0315ba4 100644 --- a/setup.py +++ b/setup.py @@ -24,5 +24,7 @@ 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', - ] + ], + package_data={'plexauth': ['LICENSE', 'README.md', 'py.typed']}, + ) From c39b24d5eeaac876e923875c8f84183139ac1b36 Mon Sep 17 00:00:00 2001 From: Dr-Blank <64108942+Dr-Blank@users.noreply.github.com> Date: Fri, 29 Dec 2023 03:02:04 -0500 Subject: [PATCH 4/5] Add type annotation for token variable in PlexAuth class --- plexauth.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plexauth.py b/plexauth.py index 2e5ddb9..7c01e1a 100755 --- a/plexauth.py +++ b/plexauth.py @@ -67,7 +67,7 @@ async def request_auth_token(self): TOKEN_URL.format(self._identifier), headers=payload ) as resp: response = await resp.json() - token = response["authToken"] + token: str = response["authToken"] return token async def token(self, timeout: int = 60): From c607748c158b3eb3bd796d213e726ffc3142f3a3 Mon Sep 17 00:00:00 2001 From: Dr-Blank <64108942+Dr-Blank@users.noreply.github.com> Date: Sat, 30 Dec 2023 08:26:57 -0500 Subject: [PATCH 5/5] make the package installable with py.typed --- plexauth/__init__.py | 3 +++ plexauth.py => plexauth/plexauth.py | 0 py.typed => plexauth/py.typed | 0 setup.py | 41 ++++++++++++++++------------- 4 files changed, 25 insertions(+), 19 deletions(-) create mode 100644 plexauth/__init__.py rename plexauth.py => plexauth/plexauth.py (100%) mode change 100755 => 100644 rename py.typed => plexauth/py.typed (100%) diff --git a/plexauth/__init__.py b/plexauth/__init__.py new file mode 100644 index 0000000..290b052 --- /dev/null +++ b/plexauth/__init__.py @@ -0,0 +1,3 @@ +from .plexauth import PlexAuth + +__all__ = ["PlexAuth"] diff --git a/plexauth.py b/plexauth/plexauth.py old mode 100755 new mode 100644 similarity index 100% rename from plexauth.py rename to plexauth/plexauth.py diff --git a/py.typed b/plexauth/py.typed similarity index 100% rename from py.typed rename to plexauth/py.typed diff --git a/setup.py b/setup.py index 0315ba4..fed9552 100644 --- a/setup.py +++ b/setup.py @@ -1,30 +1,33 @@ from setuptools import setup -with open('README.md') as f: +with open("README.md") as f: long_description = f.read() -VERSION="0.0.6" +VERSION = "0.0.6" setup( - name='plexauth', + name="plexauth", version=VERSION, - description='Handles the authorization flow to obtain tokens from Plex.tv via external redirection.', + description=( + "Handles the authorization flow to obtain tokens from Plex.tv via" + " external redirection." + ), long_description=long_description, - long_description_content_type='text/markdown', - url='https://github.com/jjlawren/python-plexauth/', - license='MIT', - author='Jason Lawrence', - author_email='jjlawren@users.noreply.github.com', - platforms='any', - py_modules=['plexauth'], - install_requires=['aiohttp'], + long_description_content_type="text/markdown", + url="https://github.com/jjlawren/python-plexauth/", + license="MIT", + author="Jason Lawrence", + author_email="jjlawren@users.noreply.github.com", + platforms="any", + py_modules=["plexauth"], + install_requires=["aiohttp"], classifiers=[ - 'License :: OSI Approved :: MIT License', - 'Operating System :: OS Independent', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + "Programming Language :: Python :: 3.5", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", ], - package_data={'plexauth': ['LICENSE', 'README.md', 'py.typed']}, - + package_data={"plexauth": ["py.typed"]}, + packages=["plexauth"], )