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.

229 lines
7.6 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
6 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
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
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
7 years ago
  1. # -*- coding: utf-8 -*-
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (C) 2017 Marcos Pereira <marcospereira.mpj@gmail.com>
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. # this software and associated documentation files (the "Software"), to deal in
  9. # the Software without restriction, including without limitation the rights to
  10. # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. # the Software, and to permit persons to whom the Software is furnished to do so,
  12. # subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in all
  15. # copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. try:
  24. from urllib.parse import urljoin
  25. except ImportError:
  26. from urlparse import urljoin
  27. import requests
  28. from requests.adapters import HTTPAdapter
  29. from .exceptions import (KeycloakConnectionError)
  30. class ConnectionManager(object):
  31. """ Represents a simple server connection.
  32. Args:
  33. base_url (str): The server URL.
  34. headers (dict): The header parameters of the requests to the server.
  35. timeout (int): Timeout to use for requests to the server.
  36. verify (bool): Verify server SSL.
  37. proxies (dict): The proxies servers requests is sent by.
  38. """
  39. def __init__(self, base_url, headers={}, timeout=60, verify=True, proxies=None):
  40. self._base_url = base_url
  41. self._headers = headers
  42. self._timeout = timeout
  43. self._verify = verify
  44. self._s = requests.Session()
  45. self._s.auth = lambda x: x # don't let requests add auth headers
  46. # retry once to reset connection with Keycloak after tomcat's ConnectionTimeout
  47. # see https://github.com/marcospereirampj/python-keycloak/issues/36
  48. for protocol in ('https://', 'http://'):
  49. adapter = HTTPAdapter(max_retries=1)
  50. # adds POST to retry whitelist
  51. allowed_methods = set(adapter.max_retries.allowed_methods)
  52. allowed_methods.add('POST')
  53. adapter.max_retries.allowed_methods = frozenset(allowed_methods)
  54. self._s.mount(protocol, adapter)
  55. if proxies:
  56. self._s.proxies = proxies
  57. def __del__(self):
  58. self._s.close()
  59. @property
  60. def base_url(self):
  61. """ Return base url in use for requests to the server. """
  62. return self._base_url
  63. @base_url.setter
  64. def base_url(self, value):
  65. """ """
  66. self._base_url = value
  67. @property
  68. def timeout(self):
  69. """ Return timeout in use for request to the server. """
  70. return self._timeout
  71. @timeout.setter
  72. def timeout(self, value):
  73. """ """
  74. self._timeout = value
  75. @property
  76. def verify(self):
  77. """ Return verify in use for request to the server. """
  78. return self._verify
  79. @verify.setter
  80. def verify(self, value):
  81. """ """
  82. self._verify = value
  83. @property
  84. def headers(self):
  85. """ Return header request to the server. """
  86. return self._headers
  87. @headers.setter
  88. def headers(self, value):
  89. """ """
  90. self._headers = value
  91. def param_headers(self, key):
  92. """ Return a specific header parameter.
  93. :arg
  94. key (str): Header parameters key.
  95. :return:
  96. If the header parameters exist, return its value.
  97. """
  98. return self.headers.get(key)
  99. def clean_headers(self):
  100. """ Clear header parameters. """
  101. self.headers = {}
  102. def exist_param_headers(self, key):
  103. """ Check if the parameter exists in the header.
  104. :arg
  105. key (str): Header parameters key.
  106. :return:
  107. If the header parameters exist, return True.
  108. """
  109. return self.param_headers(key) is not None
  110. def add_param_headers(self, key, value):
  111. """ Add a single parameter inside the header.
  112. :arg
  113. key (str): Header parameters key.
  114. value (str): Value to be added.
  115. """
  116. self.headers[key] = value
  117. def del_param_headers(self, key):
  118. """ Remove a specific parameter.
  119. :arg
  120. key (str): Key of the header parameters.
  121. """
  122. self.headers.pop(key, None)
  123. def raw_get(self, path, **kwargs):
  124. """ Submit get request to the path.
  125. :arg
  126. path (str): Path for request.
  127. :return
  128. Response the request.
  129. :exception
  130. HttpError: Can't connect to server.
  131. """
  132. try:
  133. return self._s.get(urljoin(self.base_url, path),
  134. params=kwargs,
  135. headers=self.headers,
  136. timeout=self.timeout,
  137. verify=self.verify)
  138. except Exception as e:
  139. raise KeycloakConnectionError(
  140. "Can't connect to server (%s)" % e)
  141. def raw_post(self, path, data, **kwargs):
  142. """ Submit post request to the path.
  143. :arg
  144. path (str): Path for request.
  145. data (dict): Payload for request.
  146. :return
  147. Response the request.
  148. :exception
  149. HttpError: Can't connect to server.
  150. """
  151. try:
  152. return self._s.post(urljoin(self.base_url, path),
  153. params=kwargs,
  154. data=data,
  155. headers=self.headers,
  156. timeout=self.timeout,
  157. verify=self.verify)
  158. except Exception as e:
  159. raise KeycloakConnectionError(
  160. "Can't connect to server (%s)" % e)
  161. def raw_put(self, path, data, **kwargs):
  162. """ Submit put request to the path.
  163. :arg
  164. path (str): Path for request.
  165. data (dict): Payload for request.
  166. :return
  167. Response the request.
  168. :exception
  169. HttpError: Can't connect to server.
  170. """
  171. try:
  172. return self._s.put(urljoin(self.base_url, path),
  173. params=kwargs,
  174. data=data,
  175. headers=self.headers,
  176. timeout=self.timeout,
  177. verify=self.verify)
  178. except Exception as e:
  179. raise KeycloakConnectionError(
  180. "Can't connect to server (%s)" % e)
  181. def raw_delete(self, path, data={}, **kwargs):
  182. """ Submit delete request to the path.
  183. :arg
  184. path (str): Path for request.
  185. data (dict): Payload for request.
  186. :return
  187. Response the request.
  188. :exception
  189. HttpError: Can't connect to server.
  190. """
  191. try:
  192. return self._s.delete(urljoin(self.base_url, path),
  193. params=kwargs,
  194. data=data,
  195. headers=self.headers,
  196. timeout=self.timeout,
  197. verify=self.verify)
  198. except Exception as e:
  199. raise KeycloakConnectionError(
  200. "Can't connect to server (%s)" % e)