Aucune description

endpoints.py 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # IRIS Source Code
  2. # Copyright (C) 2024 - DFIR-IRIS
  3. # contact@dfir-iris.org
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 3 of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program; if not, write to the Free Software Foundation,
  17. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. from functools import wraps
  19. from flask_sqlalchemy.pagination import Pagination
  20. from app import app
  21. from app.blueprints.responses import response_error, response
  22. logger = app.logger
  23. def response_api_success(data):
  24. return response(200, data=data)
  25. def _get_next_page(paginated_elements: Pagination):
  26. if paginated_elements.has_next:
  27. next_page = paginated_elements.has_next
  28. else:
  29. next_page = None
  30. return next_page
  31. def response_api_paginated(schema, paginated_elements: Pagination):
  32. data = schema.dump(paginated_elements.items, many=True)
  33. next_page = _get_next_page(paginated_elements)
  34. result = {
  35. 'total': paginated_elements.total,
  36. 'data': data,
  37. 'last_page': paginated_elements.pages,
  38. 'current_page': paginated_elements.page,
  39. 'next_page': next_page
  40. }
  41. return response_api_success(result)
  42. def response_api_deleted():
  43. return response(204)
  44. def response_api_created(data):
  45. return response(201, data=data)
  46. def response_api_error(message, data=None):
  47. content = {
  48. 'message': message
  49. }
  50. if data:
  51. content['data'] = data
  52. return response(400, data=content)
  53. def response_api_not_found():
  54. return response(404)
  55. def endpoint_deprecated(alternative_verb, alternative_url):
  56. def inner_wrap(f):
  57. @wraps(f)
  58. def wrap(*args, **kwargs):
  59. result = f(*args, **kwargs)
  60. logger.warning(f'Endpoint will be deprecated soon. Use {alternative_verb} {alternative_url} instead')
  61. result.headers['Link'] = f'<{alternative_url}>; rel="alternate"'
  62. result.headers['Deprecation'] = True
  63. return result
  64. return wrap
  65. return inner_wrap
  66. def endpoint_removed(message, version):
  67. def inner_wrap(f):
  68. @wraps(f)
  69. def wrap(*args, **kwargs):
  70. return response_error(f"Endpoint deprecated in {version}. {message}.", status=410)
  71. return wrap
  72. return inner_wrap