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.

107 lines
2.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
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. from json.decoder import JSONDecodeError
  18. import requests
  19. class KeycloakError(Exception):
  20. def __init__(self, error_message="", response_code=None,
  21. response_body=None):
  22. Exception.__init__(self, error_message)
  23. self.response_code = response_code
  24. self.response_body = response_body
  25. self.error_message = error_message
  26. def __str__(self):
  27. if self.response_code is not None:
  28. return "{0}: {1}".format(self.response_code, self.error_message)
  29. else:
  30. return "{0}".format(self.error_message)
  31. class KeycloakAuthenticationError(KeycloakError):
  32. pass
  33. class KeycloakConnectionError(KeycloakError):
  34. pass
  35. class KeycloakOperationError(KeycloakError):
  36. pass
  37. class KeycloakListError(KeycloakOperationError):
  38. pass
  39. class KeycloakGetError(KeycloakOperationError):
  40. pass
  41. class KeycloakCreateError(KeycloakOperationError):
  42. pass
  43. class KeycloakUpdateError(KeycloakOperationError):
  44. pass
  45. class KeycloakDeleteError(KeycloakOperationError):
  46. pass
  47. class KeycloakSecretNotFound(KeycloakOperationError):
  48. pass
  49. class KeycloakRPTNotFound(KeycloakOperationError):
  50. pass
  51. class KeycloakAuthorizationConfigError(KeycloakOperationError):
  52. pass
  53. def raise_error_from_response(response, error, expected_code=200):
  54. if expected_code == response.status_code:
  55. if expected_code == requests.codes.no_content:
  56. return {}
  57. try:
  58. return response.json()
  59. except JSONDecodeError as e:
  60. return response.content
  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)