Skip to content

Commit 835d39f

Browse files
authored
Merge pull request #83 from OpenXbox/fix/py3.7+_compat
Feat: Python 3.7+ compatibility
2 parents e4703fe + c9e10da commit 835d39f

13 files changed

Lines changed: 61 additions & 64 deletions

File tree

.pre-commit-config.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
repos:
22
- repo: https://github.com/asottile/pyupgrade
3-
rev: v2.7.2
3+
rev: v2.20.0
44
hooks:
55
- id: pyupgrade
66
args: [--py36-plus]
77
- repo: https://github.com/psf/black
8-
rev: 20.8b1
8+
rev: 22.10.0
99
hooks:
1010
- id: black
1111
args:
1212
- --safe
1313
- --quiet
1414
files: ^((xbox|tests)/.+)?[^/]+\.py$
1515
- repo: https://gitlab.com/pycqa/flake8
16-
rev: 3.8.3
16+
rev: 5.0.4
1717
hooks:
1818
- id: flake8
1919
additional_dependencies:
2020
# - flake8-docstrings==1.5.0
2121
- pydocstyle==5.1.1
2222
files: ^(xbox)/.+\.py$
2323
- repo: https://github.com/PyCQA/bandit
24-
rev: 1.6.2
24+
rev: 1.7.4
2525
hooks:
2626
- id: bandit
2727
args:
@@ -30,7 +30,7 @@ repos:
3030
- --configfile=bandit.yaml
3131
files: ^(xbox|tests)/.+\.py$
3232
- repo: https://github.com/PyCQA/isort
33-
rev: 5.5.3
33+
rev: 5.10.1
3434
hooks:
3535
- id: isort
3636
- repo: https://github.com/pre-commit/pre-commit-hooks

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#########################################
44
# Image WITH C compiler, building wheels for next stage
5-
FROM python:3.6-alpine as bigimage
5+
FROM python:3.10-alpine as bigimage
66

77
ENV LANG C.UTF-8
88

@@ -17,7 +17,7 @@ RUN pip wheel --wheel-dir=/root/wheels /src/xbox-webapi
1717

1818
#########################################
1919
# Image WITHOUT C compiler, installing the component from wheel
20-
FROM python:3.6-alpine as smallimage
20+
FROM python:3.10-alpine as smallimage
2121

2222
RUN apk add --no-cache openssl
2323

README.md

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Authentication is supported via OAuth2.
1919

2020
## Dependencies
2121

22-
- Python >= 3.6
22+
- Python >= 3.7
2323
- Libraries: aiohttp, appdirs, ms_cv, pydantic, urwid, yarl, ecdsa
2424

2525
## How to use
@@ -32,19 +32,18 @@ pip install xbox-webapi
3232

3333
Authentication
3434

35-
```text
36-
# Note: you must use non child account (> 18 years old)
37-
#
38-
# Token save location: If tokenfile is not provided via cmdline, fallback
39-
# of <appdirs.user_data_dir>/tokens.json is used as save-location
40-
#
41-
# Specifically:
42-
# Windows: C:\\Users\\<username>\\AppData\\Local\\OpenXbox\\xbox
43-
# Mac OSX: /Users/<username>/Library/Application Support/xbox/tokens.json
44-
# Linux: /home/<username>/.local/share/xbox
45-
#
46-
# For more information, see: https://pypi.org/project/appdirs and module: xbox.webapi.scripts.constants
35+
**Note: You must use non child account (> 18 years old)**
36+
37+
Token save location: If tokenfile is not provided via cmdline, fallback of `<appdirs.user_data_dir>/tokens.json` is used as save-location
38+
39+
Specifically:
40+
Windows: `C:\\Users\\<username>\\AppData\\Local\\OpenXbox\\xbox`
41+
Mac OSX: `/Users/<username>/Library/Application Support/xbox/tokens.json`
42+
Linux: `/home/<username>/.local/share/xbox`
43+
44+
For more information, see: <https://pypi.org/project/appdirs> and module: `xbox.webapi.scripts.constants`
4745

46+
```
4847
xbox-authenticate --client-id <client-id> --client-secret <client-secret>
4948
```
5049

@@ -60,19 +59,22 @@ API usage
6059
```py
6160
import sys
6261
import asyncio
63-
from aiohttp import ClientSession
62+
from aiohttp import ClientSession, ClientResponseError
6463
from xbox.webapi.api.client import XboxLiveClient
6564
from xbox.webapi.authentication.manager import AuthenticationManager
6665
from xbox.webapi.authentication.models import OAuth2TokenResponse
67-
from xbox.webapi.common.exceptions import AuthenticationException
68-
from xbox import *
69-
client_id = 'YOUR CLIENT ID HERE'
70-
client_secret = 'YOUR CLIENT SECRET HERE'
66+
from xbox.webapi.scripts import CLIENT_ID, CLIENT_SECRET, TOKENS_FILE
67+
68+
# This uses the default client identification by OpenXbox
69+
# Feel free to use your own here
70+
client_id = CLIENT_ID
71+
client_secret = CLIENT_SECRET
72+
tokens_file = TOKENS_FILE
73+
7174
"""
7275
For doing authentication, see xbox/webapi/scripts/authenticate.py
7376
"""
7477
async def async_main():
75-
tokens_file = "./tokens.json" # replace with path in auth scrip or just paste file with tokens here
7678
async with ClientSession() as session:
7779
auth_mgr = AuthenticationManager(
7880
session, client_id, client_secret, ""

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[tool.black]
2-
target-version = ["py36", "py37", "py38"]
2+
target-version = ["py37", "py38", "py39", "py310", "py311"]
33
exclude = 'generated'

readme_example.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
# this is just script from README.md
21
import sys
32
import asyncio
4-
from aiohttp import ClientSession
3+
from aiohttp import ClientSession, ClientResponseError
54
from xbox.webapi.api.client import XboxLiveClient
65
from xbox.webapi.authentication.manager import AuthenticationManager
76
from xbox.webapi.authentication.models import OAuth2TokenResponse
8-
from xbox.webapi.common.exceptions import AuthenticationException
9-
from xbox import *
10-
client_id = 'YOUR CLIENT ID HERE'
11-
client_secret = 'YOUR CLIENT SECRET HERE'
7+
from xbox.webapi.scripts import CLIENT_ID, CLIENT_SECRET, TOKENS_FILE
8+
9+
# This uses the default client identification by OpenXbox
10+
# Feel free to use your own here
11+
client_id = CLIENT_ID
12+
client_secret = CLIENT_SECRET
13+
tokens_file = TOKENS_FILE
14+
1215
"""
1316
For doing authentication, see xbox/webapi/scripts/authenticate.py
1417
"""
1518
async def async_main():
16-
tokens_file = "./tokens.json" # replace with path in auth scrip or just paste file with tokens here
1719
async with ClientSession() as session:
1820
auth_mgr = AuthenticationManager(
1921
session, client_id, client_secret, ""

requirements.txt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ urwid
88
yarl
99

1010
# aiohttp speedups
11-
cchardet
1211
aiodns
1312

1413
# Dev
@@ -18,11 +17,11 @@ pylint
1817
aresponses
1918

2019
# pre-commit
21-
pre-commit==2.7.1
22-
pyupgrade==2.7.2
23-
black==20.8b1
24-
flake8==3.8.3
20+
pre-commit==2.20.0
21+
pyupgrade==3.2.0
22+
black==22.10.0
23+
flake8==5.0.4
2524
# flake8-docstrings==1.5.0
26-
pydocstyle==5.1.1
27-
bandit==1.6.2
28-
isort==5.5.3
25+
pydocstyle==6.1.1
26+
bandit==1.7.4
27+
isort==5.10.1

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
"License :: OSI Approved :: MIT License",
2424
"Topic :: Software Development :: Libraries :: Python Modules",
2525
"Programming Language :: Python :: 3",
26-
"Programming Language :: Python :: 3.6",
2726
"Programming Language :: Python :: 3.7",
2827
"Programming Language :: Python :: 3.8",
28+
"Programming Language :: Python :: 3.9",
29+
"Programming Language :: Python :: 3.10",
30+
"Programming Language :: Python :: 3.11",
2931
],
3032
test_suite="tests",
3133
install_requires=[

tests/conftest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from aiohttp import ClientSession
66
import pytest
7+
import pytest_asyncio
78

89
from xbox.webapi.api.client import XboxLiveClient
910
from xbox.webapi.authentication.manager import AuthenticationManager
@@ -19,7 +20,7 @@
1920
collect_ignore = ["setup.py"]
2021

2122

22-
@pytest.fixture(scope="function")
23+
@pytest_asyncio.fixture(scope="function")
2324
async def auth_mgr(event_loop):
2425
session = ClientSession(loop=event_loop)
2526
mgr = AuthenticationManager(session, "abc", "123", "http://localhost")
@@ -32,4 +33,4 @@ async def auth_mgr(event_loop):
3233

3334
@pytest.fixture(scope="function")
3435
def xbl_client(auth_mgr):
35-
yield XboxLiveClient(auth_mgr)
36+
return XboxLiveClient(auth_mgr)

xbox/webapi/authentication/manager.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ async def _oauth2_token_request(self, data: dict) -> OAuth2TokenResponse:
111111
resp = await self.session.post(
112112
"https://login.live.com/oauth20_token.srf", data=data
113113
)
114-
# print(await resp.content.read())
115114
resp.raise_for_status()
116115
return OAuth2TokenResponse.parse_raw(await resp.text())
117116

xbox/webapi/scripts/authenticate.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ async def auth_callback(request):
2929
)
3030

3131

32-
async def async_main(
32+
async def do_auth(
3333
client_id: str, client_secret: str, redirect_uri: str, token_filepath: str
3434
):
35-
3635
async with ClientSession() as session:
3736
auth_mgr = AuthenticationManager(
3837
session, client_id, client_secret, redirect_uri
@@ -53,10 +52,11 @@ async def async_main(
5352
await auth_mgr.request_tokens(code)
5453

5554
with open(token_filepath, mode="w") as f:
55+
print(f"Finished authentication, writing tokens to {token_filepath}")
5656
f.write(auth_mgr.oauth.json())
5757

5858

59-
def main():
59+
async def async_main():
6060
parser = argparse.ArgumentParser(description="Authenticate with XBL")
6161
parser.add_argument(
6262
"--tokens",
@@ -82,21 +82,16 @@ def main():
8282
default=os.environ.get("REDIRECT_URI", REDIRECT_URI),
8383
help="OAuth2 Redirect URI",
8484
)
85-
8685
args = parser.parse_args()
87-
8886
app = web.Application()
8987
app.add_routes([web.get("/auth/callback", auth_callback)])
9088
runner = web.AppRunner(app)
9189

92-
loop = asyncio.get_event_loop()
93-
loop.run_until_complete(runner.setup())
90+
await runner.setup()
9491
site = web.TCPSite(runner, "localhost", 8080)
95-
loop.run_until_complete(site.start())
96-
loop.run_until_complete(
97-
async_main(args.client_id, args.client_secret, args.redirect_uri, args.tokens)
98-
)
92+
await site.start()
93+
await do_auth(args.client_id, args.client_secret, args.redirect_uri, args.tokens)
9994

10095

10196
if __name__ == "__main__":
102-
main()
97+
asyncio.run(async_main())

0 commit comments

Comments
 (0)