2
2
3
3
from __future__ import annotations
4
4
5
- import asyncio
6
5
import logging
7
- from collections .abc import Callable , Coroutine
8
- from http import HTTPStatus
9
6
from typing import Any , TypeVar
10
7
11
8
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
14
11
15
- from .models import Auth , User , Statistics
12
+ from .models import Auth , Statistics , User
16
13
17
14
_LOGGER = logging .getLogger (__name__ )
18
15
19
16
T = TypeVar ("T" )
20
17
18
+ ENDPOINT = "https://account.flitsmeister.app/"
21
19
22
20
21
+ class NotauthenticatedException (Exception ):
22
+ """Not authenticated exception."""
23
+
23
24
24
25
class FM :
25
26
"""Implementation of Flitsmeister."""
26
27
27
28
_session : ClientSession | None
28
29
_close_session : bool = False
29
30
_request_timeout : int = 10
30
-
31
+
31
32
_auth : Auth | None = None
32
33
33
34
def __init__ (
34
- self ,
35
- client_session : ClientSession = None ,
35
+ self ,
36
+ client_session : ClientSession = None ,
36
37
request_timeout : int = 10 ,
37
- auth : Auth | None = None
38
+ auth : Auth | None = None ,
38
39
):
39
40
"""Create a FM object.
40
41
@@ -47,11 +48,10 @@ def __init__(
47
48
self ._session = client_session
48
49
self ._request_timeout = request_timeout
49
50
self ._auth = auth
50
-
51
-
51
+
52
52
async def login (self , username : str , password : str ) -> Auth :
53
53
"""Login to the API.
54
-
54
+
55
55
https://account.flitsmeister.app/parse/login { "_method": "GET", "password": "<password>", "username": "<email>"}
56
56
{
57
57
"objectId": "1EqBUC03nK",
@@ -60,14 +60,17 @@ async def login(self, username: str, password: str) -> Auth:
60
60
(And a lot more -for now irrelevant- data)
61
61
}
62
62
"""
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
+ )
66
69
return Auth .from_dict (response )
67
-
70
+
68
71
async def user (self ) -> User :
69
72
"""Get user information.
70
-
73
+
71
74
https://account.flitsmeister.app/parse/classes/_User/<USER_ID>
72
75
{
73
76
"4411EvEnabled": false,
@@ -130,20 +133,20 @@ async def user(self) -> User:
130
133
"validated": true,
131
134
"vehicleType": 1
132
135
}
133
-
136
+
134
137
"""
135
-
138
+
136
139
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
+ )
142
145
return User .from_dict (response )
143
-
146
+
144
147
async def statistics (self ) -> Statistics :
145
148
"""Get user statistics.
146
-
149
+
147
150
https://account.flitsmeister.app/parse/functions/fetchStatistics
148
151
{
149
152
"result": {
@@ -182,28 +185,26 @@ async def statistics(self) -> Statistics:
182
185
}
183
186
}
184
187
"""
185
-
188
+
186
189
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 , {})
191
193
return Statistics .from_dict (response )
192
-
193
-
194
+
194
195
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
196
197
) -> Any :
197
198
"""Make a request to the API."""
198
199
if self ._session is None :
199
200
self ._session = ClientSession ()
200
201
self ._close_session = True
201
202
202
203
headers = {"Content-Type" : "application/json" }
203
-
204
- if (self ._auth is not None ):
204
+ if self ._auth is not None :
205
205
headers ["x-parse-session-token" ] = self ._auth .session_token
206
206
207
+ url = f"{ ENDPOINT } { path } "
207
208
_LOGGER .debug ("%s, %s, %s" , method , url , data )
208
209
209
210
async with async_timeout .timeout (self ._request_timeout ):
0 commit comments