kisa-utils 0.37.2__py3-none-any.whl → 0.37.3__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.
@@ -5,7 +5,8 @@ Kisa Server Utilities
5
5
  import kisa_utils as kutils
6
6
  from kisa_utils.storage import Path
7
7
  from kisa_utils.response import Response, Ok, Error
8
- from flask import Flask, request, jsonify, wrappers, render_template_string, current_app, g as app_ctx
8
+ from flask import Flask, request, jsonify, wrappers, render_template_string
9
+ from flask import current_app, g as app_ctx, make_response
9
10
  from flask_cors import CORS
10
11
  from functools import wraps
11
12
  import copy
@@ -351,11 +352,27 @@ def entry(func):
351
352
 
352
353
  # return wrapper
353
354
 
355
+ @enforceRequirements
356
+ def __verifyResponseHeaders(headers:dict[str, str|int|float], /) -> Response:
357
+ '''
358
+ validate response headers to be set
359
+ '''
360
+
361
+ keyTypes, valuetypes = get_args(__verifyResponseHeaders.__annotations__['headers'])
362
+
363
+ for header in headers:
364
+ if not isinstance(header, keyTypes):
365
+ return Error(f'response header `{header}` expected to be of type `{keyTypes}`')
366
+ if not isinstance(headers[header], valuetypes):
367
+ return Error(f'response header `{header}` value expected to be of type `{valuetypes}`')
368
+
369
+ return Ok()
354
370
 
355
371
  # @enforceRequirements
356
372
  def endpoint(
357
373
  name: str|Callable = '', group: str = '', methods:list[str] = ['POST'],
358
- security: Optional[Callable] = None
374
+ security: Optional[Callable] = None,
375
+ responseHeaders:dict[str, str|int|float] = {}
359
376
  ) -> Callable:
360
377
  """
361
378
  creates flask endpoints
@@ -370,6 +387,9 @@ def endpoint(
370
387
 
371
388
  methods = _normalize_and_validate_methods(methods)
372
389
 
390
+ if responseHeaders and not (resp := __verifyResponseHeaders(responseHeaders)):
391
+ raise ValueError(resp.log)
392
+
373
393
  if 1:
374
394
  _group = group + '/' if group else ''
375
395
  _name = '/' + _group + (name or func.__name__)
@@ -519,7 +539,29 @@ def endpoint(
519
539
  if not isinstance(resp, expectedReturnType):
520
540
  raise TypeError(f'`{handler.__name__}` did NOT return a {expectedReturnType.__name__} object')
521
541
 
522
- return resp
542
+ if isinstance(resp, tuple):
543
+ if 2 != len(resp):
544
+ raise Exception(f'expected 2-item tuple from the endpoint handler!')
545
+ if not isinstance(resp[1], int):
546
+ raise Exception(f'expected status_code as the second item in the tuple from the endpoint handler!')
547
+
548
+ if not responseHeaders:
549
+ return resp
550
+
551
+ if isinstance(resp, tuple):
552
+ resp,statusCode = resp
553
+ else:
554
+ statusCode = 200
555
+
556
+ if isinstance(resp, Response):
557
+ resp = resp.asJSON
558
+
559
+ response = make_response(resp,statusCode)
560
+
561
+ for header in responseHeaders:
562
+ response.headers[header] = responseHeaders[header]
563
+
564
+ return response
523
565
 
524
566
  w.__name__ = f'__kisa_wrapper_{_group}_{handler.__name__}'
525
567
  __SERVER_APP.route(_name, methods=methods)(w)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kisa-utils
3
- Version: 0.37.2
3
+ Version: 0.37.3
4
4
  Summary: Utility functions and modules for KISA Developers
5
5
  Author: Tom Bukenya
6
6
  Author-email: glayn2bukman@gmail.com
@@ -19,11 +19,11 @@ kisa_utils/token.py,sha256=ReCIBsq95RMYCrDCyHgU1y_Eq-xp_PBpZiHxwsY6Fj4,8015
19
19
  kisa_utils/permissions/__config__.py,sha256=i3ELkOydDnjKx2ozQTxLZdZ8DXSeUncnl2kRxANjFmM,613
20
20
  kisa_utils/permissions/__init__.py,sha256=k7WbNlE8i9Vyf_SdbXbTh8D3gt4obDe3f8rONVVmNH4,36291
21
21
  kisa_utils/servers/__init__.py,sha256=lPqDyGTrFo0qwPZ2WA9Xtcpc5D8AIU4huqgFx1iZf68,19
22
- kisa_utils/servers/flask.py,sha256=Z8k8gtUulZw5KJnDFgGa1o1TX5zqbWkmeFDERMPWd-g,35002
22
+ kisa_utils/servers/flask.py,sha256=PXH0WQh7LBi6Pj9WugIyXXHwbUTZIBQjMWt9SAysmgA,36586
23
23
  kisa_utils/structures/__init__.py,sha256=JBU1j3A42jQ62ALKnsS1Hav9YXcYwjDw1wQJtohXPbU,83
24
24
  kisa_utils/structures/utils.py,sha256=M4qBWjkrG9grHgIu_OZ6TEISsHRtZq5hF-S4mScelgs,2484
25
25
  kisa_utils/structures/validator.py,sha256=8FTi_GDPTNplxNJLTH8DraNyHZPjYFRvPBlI0sIvjBE,3573
26
- kisa_utils-0.37.2.dist-info/METADATA,sha256=suXXHe2bnfMykd5io8ADDhXLdiqLUXvqi0gp895Husk,477
27
- kisa_utils-0.37.2.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
28
- kisa_utils-0.37.2.dist-info/top_level.txt,sha256=URxY4sRuqmirOxWtztpVmPoGQdksEMYO6hmYsEDGz2Y,75
29
- kisa_utils-0.37.2.dist-info/RECORD,,
26
+ kisa_utils-0.37.3.dist-info/METADATA,sha256=28yDyc18EqX8MIQYC4eNRYjtizKrjd7v2v-tNj4F4Ik,477
27
+ kisa_utils-0.37.3.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
28
+ kisa_utils-0.37.3.dist-info/top_level.txt,sha256=URxY4sRuqmirOxWtztpVmPoGQdksEMYO6hmYsEDGz2Y,75
29
+ kisa_utils-0.37.3.dist-info/RECORD,,