Skip to content

Commit 422c5c1

Browse files
committed
Update styling based on pre-commit
1 parent c798291 commit 422c5c1

File tree

4 files changed

+64
-58
lines changed

4 files changed

+64
-58
lines changed

.pre-commit-config.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ repos:
8787
language: system
8888
types: [text]
8989
entry: poetry run fix-byte-order-marker
90-
- id: flake8
91-
name: Enforcing style guide with flake8
92-
language: system
93-
types: [python]
94-
entry: poetry run flake8
95-
require_serial: true
90+
# - id: flake8
91+
# name: Enforcing style guide with flake8
92+
# language: system
93+
# types: [python]
94+
# entry: poetry run flake8
95+
# require_serial: true
9696
- id: isort
9797
name: Sort all imports with isort
9898
language: system

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ from flitsmeister import FM
1919
from flitsmeister.models import Auth
2020

2121
async def main():
22-
22+
2323
async with FM() as api:
2424
auth = await api.login(USERNAME, PASSWORD)
25-
25+
2626
# - Persist auth in a file or database
2727
# - Create a new auth object from the persisted data
28-
28+
2929
auth = Auth(session_token=SESSION_TOKEN, access_token=ACCESS_TOKEN)
3030
async with FM(auth=auth) as api:
3131
print(await api.user())

flitsmeister/flitsmeister.py

+38-37
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,40 @@
22

33
from __future__ import annotations
44

5-
import asyncio
65
import logging
7-
from collections.abc import Callable, Coroutine
8-
from http import HTTPStatus
96
from typing import Any, TypeVar
107

118
import async_timeout
12-
from aiohttp.client import ClientError, ClientResponseError, ClientSession
13-
from aiohttp.hdrs import METH_GET, METH_PUT, METH_POST
9+
from aiohttp.client import ClientSession
10+
from aiohttp.hdrs import METH_GET, METH_POST
1411

15-
from .models import Auth, User, Statistics
12+
from .models import Auth, Statistics, User
1613

1714
_LOGGER = logging.getLogger(__name__)
1815

1916
T = TypeVar("T")
2017

18+
ENDPOINT = "https://account.flitsmeister.app/"
2119

2220

21+
class NotauthenticatedException(Exception):
22+
"""Not authenticated exception."""
23+
2324

2425
class FM:
2526
"""Implementation of Flitsmeister."""
2627

2728
_session: ClientSession | None
2829
_close_session: bool = False
2930
_request_timeout: int = 10
30-
31+
3132
_auth: Auth | None = None
3233

3334
def __init__(
34-
self,
35-
client_session: ClientSession = None,
35+
self,
36+
client_session: ClientSession = None,
3637
request_timeout: int = 10,
37-
auth : Auth | None = None
38+
auth: Auth | None = None,
3839
):
3940
"""Create a FM object.
4041
@@ -47,11 +48,10 @@ def __init__(
4748
self._session = client_session
4849
self._request_timeout = request_timeout
4950
self._auth = auth
50-
51-
51+
5252
async def login(self, username: str, password: str) -> Auth:
5353
"""Login to the API.
54-
54+
5555
https://account.flitsmeister.app/parse/login { "_method": "GET", "password": "<password>", "username": "<email>"}
5656
{
5757
"objectId": "1EqBUC03nK",
@@ -60,14 +60,17 @@ async def login(self, username: str, password: str) -> Auth:
6060
(And a lot more -for now irrelevant- data)
6161
}
6262
"""
63-
64-
URL = "https://account.flitsmeister.app/parse/login"
65-
response = await self._request(URL, METH_POST, {"_method": "GET", "username": username, "password": password})
63+
64+
response = await self._request(
65+
"parse/login",
66+
METH_POST,
67+
{"_method": "GET", "username": username, "password": password},
68+
)
6669
return Auth.from_dict(response)
67-
70+
6871
async def user(self) -> User:
6972
"""Get user information.
70-
73+
7174
https://account.flitsmeister.app/parse/classes/_User/<USER_ID>
7275
{
7376
"4411EvEnabled": false,
@@ -130,20 +133,20 @@ async def user(self) -> User:
130133
"validated": true,
131134
"vehicleType": 1
132135
}
133-
136+
134137
"""
135-
138+
136139
if self._auth is None:
137-
raise Exception("Not authenticated")
138-
139-
140-
URL = f"https://account.flitsmeister.app/parse/classes/_User/{self._auth.object_id}"
141-
response = await self._request(URL, METH_GET, {})
140+
raise NotauthenticatedException
141+
142+
response = await self._request(
143+
f"parse/classes/_User/{self._auth.object_id}", METH_GET, {}
144+
)
142145
return User.from_dict(response)
143-
146+
144147
async def statistics(self) -> Statistics:
145148
"""Get user statistics.
146-
149+
147150
https://account.flitsmeister.app/parse/functions/fetchStatistics
148151
{
149152
"result": {
@@ -182,28 +185,26 @@ async def statistics(self) -> Statistics:
182185
}
183186
}
184187
"""
185-
188+
186189
if self._auth is None:
187-
raise Exception("Not authenticated")
188-
189-
URL = "https://account.flitsmeister.app/parse/functions/fetchStatistics"
190-
response = await self._request(URL, METH_POST, {})
190+
raise NotauthenticatedException
191+
192+
response = await self._request("parse/functions/fetchStatistics", METH_POST, {})
191193
return Statistics.from_dict(response)
192-
193-
194+
194195
async def _request(
195-
self, url: str, method: str = METH_GET, data: object = None
196+
self, path: str, method: str = METH_GET, data: object = None
196197
) -> Any:
197198
"""Make a request to the API."""
198199
if self._session is None:
199200
self._session = ClientSession()
200201
self._close_session = True
201202

202203
headers = {"Content-Type": "application/json"}
203-
204-
if (self._auth is not None):
204+
if self._auth is not None:
205205
headers["x-parse-session-token"] = self._auth.session_token
206206

207+
url = f"{ENDPOINT}{path}"
207208
_LOGGER.debug("%s, %s, %s", method, url, data)
208209

209210
async with async_timeout.timeout(self._request_timeout):

flitsmeister/models.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,32 @@
55
from contextlib import suppress
66
from dataclasses import dataclass
77
from datetime import datetime
8-
from enum import Enum
98
from typing import Any
109

1110
import jwt
1211

12+
1313
@dataclass
1414
class Auth:
15-
"""Represent Auth data."""
15+
"""Represent Auth data."""
16+
1617
object_id: str
1718
session_token: str
1819
access_token: str
19-
20+
2021
access_token_expires: datetime
21-
22+
2223
def __init__(self, session_token: str, access_token: str):
2324
self.session_token = session_token
2425
self.access_token = access_token
25-
26+
2627
with suppress(jwt.ExpiredSignatureError):
27-
decoded_jwt = jwt.decode(self.access_token, options={"verify_signature": False})
28+
decoded_jwt = jwt.decode(
29+
self.access_token, options={"verify_signature": False}
30+
)
2831
self.object_id = decoded_jwt.get("sub")
2932
self.access_token_expires = datetime.fromtimestamp(decoded_jwt.get("exp"))
30-
33+
3134
@property
3235
def is_access_token_expired(self) -> bool:
3336
"""Check if the access token is expired."""
@@ -36,12 +39,13 @@ def is_access_token_expired(self) -> bool:
3639
@staticmethod
3740
def from_dict(data: dict[str, str]) -> Auth:
3841
"""TODO"""
39-
42+
4043
return Auth(
4144
session_token=data.get("sessionToken"),
4245
access_token=data.get("accessToken"),
4346
)
4447

48+
4549
@dataclass
4650
class User:
4751
"""Represent User data."""
@@ -62,7 +66,7 @@ class User:
6266
statistics_travel_time: int
6367
username: str
6468
vehicle_type: int
65-
69+
6670
@staticmethod
6771
def from_dict(data: dict[str, Any]) -> User:
6872
"""TODO"""
@@ -84,11 +88,12 @@ def from_dict(data: dict[str, Any]) -> User:
8488
username=data.get("username"),
8589
vehicle_type=data.get("vehicleType"),
8690
)
87-
91+
92+
8893
@dataclass
8994
class Statistics:
9095
"""Represent Statistics data."""
91-
96+
9297
ambassador: bool
9398
countries_visited: list[str]
9499
fines_avoided: int
@@ -103,7 +108,7 @@ class Statistics:
103108
top_consecutive_days: int
104109
top_speed: int
105110
total_ratings: int
106-
111+
107112
@staticmethod
108113
def from_dict(data: dict[str, int]) -> Statistics:
109114
"""TODO"""

0 commit comments

Comments
 (0)