3
3
from typing import Optional
4
4
from urllib .parse import urljoin as join
5
5
6
+ import aiohttp
6
7
import requests
7
8
8
9
log = logging .getLogger (__name__ )
@@ -26,25 +27,48 @@ class TokenUnavailable(Exception):
26
27
pass
27
28
28
29
29
- class HyScoresClient :
30
- def __init__ (
31
- self , url , app : str , timeout : int = 30 , user_agent : Optional [str ] = None
32
- ):
30
+ class _HSClientBase :
31
+ def __init__ (self , url , app : str , timeout : int = 30 ):
33
32
self .url = url
34
- self .session = requests .Session ()
35
33
self .timeout = max (timeout , 0 )
36
34
self .app = app
37
35
38
- if user_agent :
39
- self .session .headers ["user-agent" ] = user_agent
40
-
41
36
self ._token = None
42
37
43
38
@property
44
39
def token (self ):
45
40
return self ._token
46
41
47
- @token .setter
42
+ def require_token (func : callable ):
43
+ def inner (self , * args , ** kwargs ):
44
+ if not self .token :
45
+ raise TokenUnavailable
46
+
47
+ return func (self , * args , ** kwargs )
48
+
49
+ return inner
50
+
51
+ @require_token
52
+ def logout (self ):
53
+ self .token = None
54
+
55
+
56
+ class HyScoresClient (_HSClientBase ):
57
+ def __init__ (
58
+ self , url , app : str , timeout : int = 30 , user_agent : Optional [str ] = None
59
+ ):
60
+ super ().__init__ (
61
+ url = url ,
62
+ app = app ,
63
+ timeout = timeout ,
64
+ )
65
+
66
+ self .session = requests .Session ()
67
+
68
+ if user_agent :
69
+ self .session .headers ["user-agent" ] = user_agent
70
+
71
+ @_HSClientBase .token .setter
48
72
def token (self , val : str ):
49
73
self ._token = val
50
74
self .session .headers .update ({"x-access-tokens" : self ._token })
@@ -80,24 +104,15 @@ def login(self, username: str, password: str):
80
104
81
105
raise AuthError
82
106
83
- def require_token (func : callable ):
84
- def inner (self , * args , ** kwargs ):
85
- if not self .token :
86
- raise TokenUnavailable
87
-
88
- return func (self , * args , ** kwargs )
89
-
90
- return inner
91
-
92
- @require_token
107
+ @_HSClientBase .require_token
93
108
def get_scores (self ) -> list :
94
109
return self .session .get (
95
110
join (self .url , "scores" ),
96
111
timeout = self .timeout ,
97
112
json = {"app" : self .app },
98
113
).json ()["result" ]
99
114
100
- @require_token
115
+ @_HSClientBase . require_token
101
116
def get_score (self , nickname : str ) -> dict :
102
117
result = self .session .get (
103
118
join (self .url , "score" ),
@@ -112,7 +127,7 @@ def get_score(self, nickname: str) -> dict:
112
127
else :
113
128
raise InvalidName
114
129
115
- @require_token
130
+ @_HSClientBase . require_token
116
131
def post_score (self , nickname : str , score : int ) -> bool :
117
132
return self .session .post (
118
133
join (self .url , "score" ),
@@ -124,6 +139,94 @@ def post_score(self, nickname: str, score: int) -> bool:
124
139
},
125
140
).json ()["result" ]
126
141
127
- @require_token
128
- def logout (self ):
129
- self .token = None
142
+
143
+ class HyScoresAsyncClient (_HSClientBase ):
144
+ def __init__ (
145
+ self , url , app : str , timeout : int = 30 , user_agent : Optional [str ] = None
146
+ ):
147
+ super ().__init__ (
148
+ url = url ,
149
+ app = app ,
150
+ timeout = timeout ,
151
+ )
152
+
153
+ self .session = aiohttp .ClientSession (
154
+ timeout = self .timeout ,
155
+ )
156
+
157
+ if user_agent :
158
+ self .session .headers ["user-agent" ] = user_agent
159
+
160
+ @_HSClientBase .token .setter
161
+ def token (self , val : str ):
162
+ self ._token = val
163
+ self .session .headers .update ({"x-access-tokens" : self ._token })
164
+
165
+ async def register (self , username : str , password : str ) -> bool :
166
+ async with self .session .post (
167
+ join (self .url , "register" ),
168
+ timeout = self .timeout ,
169
+ auth = (username , password ),
170
+ json = {"app" : self .app },
171
+ ) as response :
172
+ data = await response .json ()
173
+ return data .get ("result" , False )
174
+
175
+ async def login (self , username : str , password : str ):
176
+ async with self .session .post (
177
+ join (self .url , "login" ),
178
+ timeout = self .timeout ,
179
+ auth = (username , password ),
180
+ json = {"app" : self .app },
181
+ ) as response :
182
+ data = await response .json ()
183
+ result = data .get ("result" , None )
184
+
185
+ if result :
186
+ token = result .get ("token" , None )
187
+ if token :
188
+ self .token = token
189
+ return
190
+
191
+ raise AuthError
192
+
193
+ @_HSClientBase .require_token
194
+ async def get_scores (self ) -> list :
195
+ async with self .session .get (
196
+ join (self .url , "scores" ),
197
+ timeout = self .timeout ,
198
+ json = {"app" : self .app },
199
+ ) as response :
200
+ data = await response .json ()
201
+ return data ["result" ]
202
+
203
+ @_HSClientBase .require_token
204
+ async def get_score (self , nickname : str ) -> dict :
205
+ async with self .session .get (
206
+ join (self .url , "score" ),
207
+ timeout = self .timeout ,
208
+ json = {
209
+ "app" : self .app ,
210
+ "nickname" : nickname ,
211
+ },
212
+ ) as response :
213
+ data = await response .json ()
214
+ result = data ["result" ]
215
+ if type (result ) is dict :
216
+ return result
217
+ else :
218
+ raise InvalidName
219
+
220
+ @_HSClientBase .require_token
221
+ async def post_score (self , nickname : str , score : int ) -> bool :
222
+ async with self .session .post (
223
+ join (self .url , "score" ),
224
+ timeout = self .timeout ,
225
+ json = {
226
+ "app" : self .app ,
227
+ "nickname" : nickname ,
228
+ "score" : score ,
229
+ },
230
+ ) as response :
231
+ data = await response .json ()
232
+ return data ["result" ]
0 commit comments