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.

139 lines
4.4 KiB

7 years ago
7 years ago
  1. """
  2. """
  3. import requests
  4. from urllib.parse import urljoin, urlencode
  5. from .exceptions import *
  6. class ConnectionManager(object):
  7. """ Represents a simple server connection.
  8. Args:
  9. base_url (str): The URL server
  10. headers (dict): The header parameters of the requests to the server.
  11. timeout (int): Timeout to use for requests to the server.
  12. """
  13. def __init__(self, base_url, headers={}, timeout=60):
  14. self.__base_url = base_url
  15. self.__headers = headers
  16. self.__timeout = timeout
  17. def get_url(self):
  18. """ Return base url in use for requests to the server. """
  19. return self.__base_url
  20. def get_timeout(self):
  21. """ Return timeout in use for request to the server. """
  22. return self.__timeout
  23. def set_headers(self, params):
  24. """ Update header request to the server.
  25. :arg
  26. params (dict): Parameters header request.
  27. """
  28. self.__headers = params
  29. def get_headers(self):
  30. """ Return header request to the server. """
  31. return self.__headers
  32. def get_param_headers(self, key):
  33. """ Return a single header parameter.
  34. :arg
  35. key (str): Key of the header parameters.
  36. :return:
  37. If the header parameters exist, return value him.
  38. """
  39. return self.__headers[key] if key in self.__headers.keys() else None
  40. def clean_headers(self):
  41. """ Clear header parameters. """
  42. self.__headers = {}
  43. def exist_param_headers(self, key):
  44. """ Check if the parameter exist in header.
  45. :arg
  46. key (str): Key of the header parameters.
  47. :return:
  48. If the header parameters exist, return True.
  49. """
  50. return True if self.get_param_headers(key) else False
  51. def add_param_headers(self, key, value):
  52. """ Add a single parameter in header.
  53. :arg
  54. key (str): Key of the header parameters.
  55. value (str): Value for the header parameter.
  56. """
  57. request_headers = self.__headers.copy()
  58. request_headers.update({key: value})
  59. self.set_headers(request_headers)
  60. def del_param_headers(self, key):
  61. """ Remove a single header parameter.
  62. :arg
  63. key (str): Key of the header parameters.
  64. """
  65. if self.get_param_headers(key):
  66. del self.__headers[key]
  67. def raw_get(self, path, **kwargs):
  68. """ Submit get request to the path.
  69. :arg
  70. path (str): Path for request.
  71. :return
  72. Response the request.
  73. :exception
  74. HttpError: Can't connect to server.
  75. """
  76. try:
  77. return requests.get(urljoin(self.get_url(), path),
  78. params=kwargs,
  79. headers=self.get_headers(),
  80. timeout=self.get_timeout())
  81. except Exception as e:
  82. raise KeycloakConnectionError(
  83. "Can't connect to server (%s)" % e)
  84. def raw_post(self, path, data, **kwargs):
  85. """ Submit post request to the path.
  86. :arg
  87. path (str): Path for request.
  88. data (dict): Payload for request.
  89. :return
  90. Response the request.
  91. :exception
  92. HttpError: Can't connect to server.
  93. """
  94. try:
  95. return requests.post(urljoin(self.get_url(), path),
  96. params=kwargs,
  97. data=data,
  98. headers=self.get_headers(),
  99. timeout=self.get_timeout())
  100. except Exception as e:
  101. raise KeycloakConnectionError(
  102. "Can't connect to server (%s)" % e)
  103. def raw_put(self, path, data, **kwargs):
  104. """ Submit put request to the path.
  105. :arg
  106. path (str): Path for request.
  107. data (dict): Payload for request.
  108. :return
  109. Response the request.
  110. :exception
  111. HttpError: Can't connect to server.
  112. """
  113. try:
  114. return requests.put(urljoin(self.get_url(), path),
  115. params=kwargs,
  116. data=data,
  117. headers=self.get_headers(),
  118. timeout=self.get_timeout())
  119. except Exception as e:
  120. raise KeycloakConnectionError(
  121. "Can't connect to server (%s)" % e)