reyserver 1.1.51__py3-none-any.whl → 1.1.53__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of reyserver might be problematic. Click here for more details.

reyserver/rauth.py CHANGED
@@ -9,11 +9,12 @@
9
9
  """
10
10
 
11
11
 
12
- from typing import Literal
12
+ from typing import Any, Literal
13
+ from datetime import datetime as Datetime
13
14
  from fastapi import APIRouter
14
15
  from reydb import rorm, DatabaseEngine, DatabaseEngineAsync
15
- # from reykit.rdata import encode_jwt, is_hash_bcrypt
16
- # from reykit.rtime import now
16
+ from reykit.rdata import encode_jwt, is_hash_bcrypt
17
+ from reykit.rtime import now, time_to
17
18
 
18
19
  from .rbase import ServerConfig, Bind, exit_api
19
20
 
@@ -263,41 +264,39 @@ async def create_sessions(
263
264
  " SELECT `perm_id`, `name`, CONCAT(`method`, ':', `path`) as `api`\n"
264
265
  ' FROM `test`.`perm`\n'
265
266
  ') AS `perm`\n'
266
- 'ON `role_perm`.`perm_id` = `perm`.`perm_id`'
267
+ 'ON `role_perm`.`perm_id` = `perm`.`perm_id`\n'
268
+ 'GROUP BY `user_id`'
267
269
  )
268
- print(1111111111111)
269
270
  result = await conn.execute(
270
271
  sql,
271
272
  account=account
272
273
  )
273
- print(result.fetchall())
274
- return {'message': 'ok'}
275
-
276
- # # Check.
277
- # table = result.to_table()
278
- # print(table)
279
- # if table == []:
280
- # exit_api(401)
281
- # json = table[0]
282
- # if not is_hash_bcrypt(password, json['password']):
283
- # exit_api(401)
284
-
285
- # # JWT.
286
- # now_timestamp_s = now('timestamp_s')
287
- # json['sub'] = json.pop('user_id')
288
- # json['iat'] = now_timestamp_s
289
- # json['nbf'] = now_timestamp_s
290
- # json['exp'] = now_timestamp_s + sess_seconds
291
- # json['role_names'] = json['role_names'].split(';')
292
- # json['perm_names'] = json['perm_names'].split(';')
293
- # perm_apis: list[str] = json['perm_apis'].split(';')
294
- # perm_api_dict = {}
295
- # for perm_api in perm_apis:
296
- # for method, path in perm_api.split(':', 1):
297
- # paths: list = perm_api_dict.setdefault(method, [])
298
- # paths.append(path)
299
- # json['perm_apis'] = perm_api_dict
300
- # token = encode_jwt(json, key)
301
- # data = {'token': token}
302
- # print(111111, data)
303
- # return data
274
+
275
+ # Check.
276
+ if result.empty:
277
+ exit_api(401)
278
+ json: dict[str, Datetime | Any] = result.to_row()
279
+ if not is_hash_bcrypt(password, json['password']):
280
+ exit_api(401)
281
+
282
+ # JWT.
283
+ now_timestamp_s = now('timestamp_s')
284
+ json['sub'] = json.pop('user_id')
285
+ json['iat'] = now_timestamp_s
286
+ json['nbf'] = now_timestamp_s
287
+ json['exp'] = now_timestamp_s + sess_seconds
288
+ json['role_names'] = json['role_names'].split(';')
289
+ json['perm_names'] = json['perm_names'].split(';')
290
+ perm_apis: list[str] = json['perm_apis'].split(';')
291
+ perm_api_dict = {}
292
+ for perm_api in perm_apis:
293
+ method, path = perm_api.split(':', 1)
294
+ paths: list = perm_api_dict.setdefault(method, [])
295
+ paths.append(path)
296
+ json['perm_apis'] = perm_api_dict
297
+ json['create_time'] = json['create_time'].timestamp()
298
+ json['update_time'] = json['update_time'].timestamp()
299
+ token = encode_jwt(json, key)
300
+ data = {'token': token}
301
+
302
+ return data
reyserver/rbase.py CHANGED
@@ -9,11 +9,9 @@
9
9
  """
10
10
 
11
11
 
12
- from typing import Sequence, Literal, NoReturn
13
- from inspect import iscoroutinefunction
14
- from contextlib import asynccontextmanager, _AsyncGeneratorContextManager
12
+ from typing import Literal, NoReturn
15
13
  from http import HTTPStatus
16
- from fastapi import FastAPI, HTTPException, UploadFile as File
14
+ from fastapi import HTTPException, UploadFile as File
17
15
  from fastapi.params import (
18
16
  Depends,
19
17
  Path,
@@ -26,7 +24,7 @@ from fastapi.params import (
26
24
  )
27
25
  from reydb.rconn import DatabaseConnectionAsync
28
26
  from reydb.rorm import DatabaseORMModel, DatabaseORMSessionAsync
29
- from reykit.rbase import CoroutineFunctionSimple, Base, Exit, StaticMeta, ConfigMeta, Singleton, throw
27
+ from reykit.rbase import Base, Exit, StaticMeta, ConfigMeta, Singleton, throw
30
28
 
31
29
  from . import rserver
32
30
 
@@ -259,57 +257,6 @@ class ServerBind(ServerBase, metaclass=StaticMeta):
259
257
  """
260
258
 
261
259
 
262
- def create_lifespan(
263
- before: CoroutineFunctionSimple | Sequence[CoroutineFunctionSimple] | None = None,
264
- after: CoroutineFunctionSimple | Sequence[CoroutineFunctionSimple] | None = None,
265
- ) -> _AsyncGeneratorContextManager[None, None]:
266
- """
267
- Create asynchronous function of lifespan manager.
268
-
269
- Parameters
270
- ----------
271
- before : Execute before server start.
272
- after : Execute after server end.
273
-
274
- Returns
275
- -------
276
- Asynchronous function.
277
- """
278
-
279
- # Parameter.
280
- if before is None:
281
- before = ()
282
- elif iscoroutinefunction(before):
283
- before = (before,)
284
- if after is None:
285
- after = ()
286
- elif iscoroutinefunction(after):
287
- after = (after,)
288
-
289
-
290
- @asynccontextmanager
291
- async def lifespan(app: FastAPI):
292
- """
293
- Server lifespan manager.
294
-
295
- Parameters
296
- ----------
297
- app : Server APP.
298
- """
299
-
300
- # Before.
301
- for task in before:
302
- await task()
303
- yield
304
-
305
- # After.
306
- for task in after:
307
- await after()
308
-
309
-
310
- return lifespan
311
-
312
-
313
260
  def create_depend_db(database: str, mode: Literal['sess', 'conn']) -> Depends:
314
261
  """
315
262
  Create dependencie type of asynchronous database.
reyserver/rserver.py CHANGED
@@ -12,6 +12,7 @@
12
12
  from typing import Literal
13
13
  from collections.abc import Sequence, Callable, Coroutine
14
14
  from inspect import iscoroutinefunction
15
+ from contextlib import asynccontextmanager, _AsyncGeneratorContextManager
15
16
  from uvicorn import run as uvicorn_run
16
17
  from starlette.middleware.base import _StreamingResponse
17
18
  from fastapi import FastAPI, Request
@@ -67,7 +68,6 @@ class Server(ServerBase, Singleton):
67
68
  # Parameter.
68
69
  if type(ssl_cert) != type(ssl_key):
69
70
  throw(AssertionError, ssl_cert, ssl_key)
70
- lifespan = Bind.create_lifespan(before, after)
71
71
  if depend is None:
72
72
  depend = ()
73
73
  elif iscoroutinefunction(depend):
@@ -76,6 +76,7 @@ class Server(ServerBase, Singleton):
76
76
  Bind.Depend(task)
77
77
  for task in depend
78
78
  ]
79
+ lifespan = self.__create_lifespan(before, after)
79
80
 
80
81
  # Build.
81
82
  ServerConfig.server = self
@@ -111,6 +112,61 @@ class Server(ServerBase, Singleton):
111
112
  'File API store directory path.'
112
113
 
113
114
 
115
+ def __create_lifespan(
116
+ self,
117
+ before: CoroutineFunctionSimple | Sequence[CoroutineFunctionSimple] | None = None,
118
+ after: CoroutineFunctionSimple | Sequence[CoroutineFunctionSimple] | None = None,
119
+ ) -> _AsyncGeneratorContextManager[None, None]:
120
+ """
121
+ Create asynchronous function of lifespan manager.
122
+
123
+ Parameters
124
+ ----------
125
+ before : Execute before server start.
126
+ after : Execute after server end.
127
+
128
+ Returns
129
+ -------
130
+ Asynchronous function.
131
+ """
132
+
133
+ # Parameter.
134
+ if before is None:
135
+ before = ()
136
+ elif iscoroutinefunction(before):
137
+ before = (before,)
138
+ if after is None:
139
+ after = ()
140
+ elif iscoroutinefunction(after):
141
+ after = (after,)
142
+
143
+
144
+ @asynccontextmanager
145
+ async def lifespan(app: FastAPI):
146
+ """
147
+ Server lifespan manager.
148
+
149
+ Parameters
150
+ ----------
151
+ app : Server APP.
152
+ """
153
+
154
+ # Before.
155
+ for task in before:
156
+ await task()
157
+ yield
158
+
159
+ # After.
160
+ for task in after:
161
+ await after()
162
+
163
+ # Database.
164
+ await self.db.dispose_all()
165
+
166
+
167
+ return lifespan
168
+
169
+
114
170
  def __add_default_middleware(self) -> None:
115
171
  """
116
172
  Add default handle middleware.
@@ -138,7 +194,10 @@ class Server(ServerBase, Singleton):
138
194
  response = await call_next(request)
139
195
 
140
196
  # After.
141
- if request.method == 'POST':
197
+ if (
198
+ response.status_code == 200
199
+ and request.method == 'POST'
200
+ ):
142
201
  response.status_code = 201
143
202
 
144
203
  return response
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: reyserver
3
- Version: 1.1.51
3
+ Version: 1.1.53
4
4
  Summary: Backend server method set.
5
5
  Project-URL: homepage, https://github.com/reyxbo/reyserver/
6
6
  Author-email: Rey <reyxbo@163.com>
@@ -0,0 +1,11 @@
1
+ reyserver/__init__.py,sha256=7GX64p7uI2eetJH9NJ-DTg-8iyQwOsGcviADFJCPxVA,373
2
+ reyserver/rall.py,sha256=riyDRTUsigco_Bee1H4aZFb8IgvjnxdX9qcnVb9i9mE,270
3
+ reyserver/rauth.py,sha256=oa6Iwuutcj9eO6RH7DaT-rPn_YqYwKuyHYaBd3lZoZE,11728
4
+ reyserver/rbase.py,sha256=jgjAkT1kxzVKB60xfWxZbnQJsehhcTBr24AclTYjc4g,5603
5
+ reyserver/rclient.py,sha256=IWZ3smyIP0_YJrfSrM8JFCr0FCtN02AyT3hp8YuSsDQ,5103
6
+ reyserver/rfile.py,sha256=bvuXOYO3UDM1jMiyNzQDz56_0ekZUEIRcfNFAhGgdUY,9010
7
+ reyserver/rserver.py,sha256=wQMAtCb3iu3XfYSiGS7TbqHslxdcMFLAmCaYdNToDbY,8241
8
+ reyserver-1.1.53.dist-info/METADATA,sha256=9DvHdPLFWZ6BASCz_WFMmGLB5ZmTstbC0_99kaQjhw4,1689
9
+ reyserver-1.1.53.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
10
+ reyserver-1.1.53.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
11
+ reyserver-1.1.53.dist-info/RECORD,,
@@ -1,11 +0,0 @@
1
- reyserver/__init__.py,sha256=7GX64p7uI2eetJH9NJ-DTg-8iyQwOsGcviADFJCPxVA,373
2
- reyserver/rall.py,sha256=riyDRTUsigco_Bee1H4aZFb8IgvjnxdX9qcnVb9i9mE,270
3
- reyserver/rauth.py,sha256=T2oafUAmXr4WfVYmEHO5v0j9K703KUYLGNVEom0J76Q,11716
4
- reyserver/rbase.py,sha256=xgdLP_O77e-pSrRWm9GVSziPSqEOh2w20cWkF4HBeWo,7042
5
- reyserver/rclient.py,sha256=IWZ3smyIP0_YJrfSrM8JFCr0FCtN02AyT3hp8YuSsDQ,5103
6
- reyserver/rfile.py,sha256=bvuXOYO3UDM1jMiyNzQDz56_0ekZUEIRcfNFAhGgdUY,9010
7
- reyserver/rserver.py,sha256=gOvLfaLqiDgHcZbzd3h2iIaDtraCuBcmsy1d61TaJ2c,6717
8
- reyserver-1.1.51.dist-info/METADATA,sha256=F4eENaUB_2lE1aEeV4ZZ_W7AHW7iB6NfPuTsR1fy-8U,1689
9
- reyserver-1.1.51.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
10
- reyserver-1.1.51.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
11
- reyserver-1.1.51.dist-info/RECORD,,