You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2017 Marcos Pereira <marcospereira.mpj@gmail.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Lesser General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (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
  13. # GNU 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, see <http://www.gnu.org/licenses/>.
  17. import requests
  18. class KeycloakError(Exception):
  19. def __init__(self, error_message="", response_code=None,
  20. response_body=None):
  21. Exception.__init__(self, error_message)
  22. self.response_code = response_code
  23. self.response_body = response_body
  24. self.error_message = error_message
  25. def __str__(self):
  26. if self.response_code is not None:
  27. return "{0}: {1}".format(self.response_code, self.error_message)
  28. else:
  29. return "{0}".format(self.error_message)
  30. class KeycloakAuthenticationError(KeycloakError):
  31. pass
  32. class KeycloakConnectionError(KeycloakError):
  33. pass
  34. class KeycloakOperationError(KeycloakError):
  35. pass
  36. class KeycloakListError(KeycloakOperationError):
  37. pass
  38. class KeycloakGetError(KeycloakOperationError):
  39. pass
  40. class KeycloakCreateError(KeycloakOperationError):
  41. pass
  42. class KeycloakUpdateError(KeycloakOperationError):
  43. pass
  44. class KeycloakDeleteError(KeycloakOperationError):
  45. pass
  46. class KeycloakProtectError(KeycloakOperationError):
  47. pass
  48. class KeycloakTransferProjectError(KeycloakOperationError):
  49. pass
  50. class KeycloakBuildCancelError(KeycloakOperationError):
  51. pass
  52. class KeycloakBuildRetryError(KeycloakOperationError):
  53. pass
  54. class KeycloakBlockError(KeycloakOperationError):
  55. pass
  56. def raise_error_from_response(response, error, expected_code=200):
  57. if expected_code == response.status_code:
  58. if expected_code == requests.codes.no_content:
  59. return {}
  60. return response.json()
  61. try:
  62. message = response.json()['message']
  63. except (KeyError, ValueError):
  64. message = response.content
  65. if isinstance(error, dict):
  66. error = error.get(response.status_code, KeycloakOperationError)
  67. else:
  68. if response.status_code == 401:
  69. error = KeycloakAuthenticationError
  70. raise error(error_message=message,
  71. response_code=response.status_code,
  72. response_body=response.content)