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.

212 lines
7.0 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
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. """
  38. def __init__(self, base_url, headers={}, timeout=60, verify=True):
  39. self._base_url = base_url
  40. self._headers = headers
  41. self._timeout = timeout
  42. self._verify = verify
  43. self._s = requests.Session()
  44. # retry once to reset connection with Keycloak after tomcat's ConnectionTimeout
  45. # see https://github.com/marcospereirampj/python-keycloak/issues/36
  46. self._s.mount('https://', HTTPAdapter(max_retries=1))
  47. self._s.mount('http://', HTTPAdapter(max_retries=1))
  48. @property
  49. def base_url(self):
  50. """ Return base url in use for requests to the server. """
  51. return self._base_url
  52. @base_url.setter
  53. def base_url(self, value):
  54. """ """
  55. self._base_url = value
  56. @property
  57. def timeout(self):
  58. """ Return timeout in use for request to the server. """
  59. return self._timeout
  60. @timeout.setter
  61. def timeout(self, value):
  62. """ """
  63. self._timeout = value
  64. @property
  65. def verify(self):
  66. """ Return verify in use for request to the server. """
  67. return self._verify
  68. @verify.setter
  69. def verify(self, value):
  70. """ """
  71. self._verify = value
  72. @property
  73. def headers(self):
  74. """ Return header request to the server. """
  75. return self._headers
  76. @headers.setter
  77. def headers(self, value):
  78. """ """
  79. self._headers = value
  80. def param_headers(self, key):
  81. """ Return a specific header parameter.
  82. :arg
  83. key (str): Header parameters key.
  84. :return:
  85. If the header parameters exist, return its value.
  86. """
  87. return self.headers.get(key)
  88. def clean_headers(self):
  89. """ Clear header parameters. """
  90. self.headers = {}
  91. def exist_param_headers(self, key):
  92. """ Check if the parameter exists in the header.
  93. :arg
  94. key (str): Header parameters key.
  95. :return:
  96. If the header parameters exist, return True.
  97. """
  98. return self.param_headers(key) is not None
  99. def add_param_headers(self, key, value):
  100. """ Add a single parameter inside the header.
  101. :arg
  102. key (str): Header parameters key.
  103. value (str): Value to be added.
  104. """
  105. self.headers[key] = value
  106. def del_param_headers(self, key):
  107. """ Remove a specific parameter.
  108. :arg
  109. key (str): Key of the header parameters.
  110. """
  111. self.headers.pop(key, None)
  112. def raw_get(self, path, **kwargs):
  113. """ Submit get request to the path.
  114. :arg
  115. path (str): Path for request.
  116. :return
  117. Response the request.
  118. :exception
  119. HttpError: Can't connect to server.
  120. """
  121. try:
  122. return self._s.get(urljoin(self.base_url, path),
  123. params=kwargs,
  124. headers=self.headers,
  125. timeout=self.timeout,
  126. verify=self.verify)
  127. except Exception as e:
  128. raise KeycloakConnectionError(
  129. "Can't connect to server (%s)" % e)
  130. def raw_post(self, path, data, **kwargs):
  131. """ Submit post request to the path.
  132. :arg
  133. path (str): Path for request.
  134. data (dict): Payload for request.
  135. :return
  136. Response the request.
  137. :exception
  138. HttpError: Can't connect to server.
  139. """
  140. try:
  141. return self._s.post(urljoin(self.base_url, path),
  142. params=kwargs,
  143. data=data,
  144. headers=self.headers,
  145. timeout=self.timeout,
  146. verify=self.verify)
  147. except Exception as e:
  148. raise KeycloakConnectionError(
  149. "Can't connect to server (%s)" % e)
  150. def raw_put(self, path, data, **kwargs):
  151. """ Submit put request to the path.
  152. :arg
  153. path (str): Path for request.
  154. data (dict): Payload for request.
  155. :return
  156. Response the request.
  157. :exception
  158. HttpError: Can't connect to server.
  159. """
  160. try:
  161. return self._s.put(urljoin(self.base_url, path),
  162. params=kwargs,
  163. data=data,
  164. headers=self.headers,
  165. timeout=self.timeout,
  166. verify=self.verify)
  167. except Exception as e:
  168. raise KeycloakConnectionError(
  169. "Can't connect to server (%s)" % e)
  170. def raw_delete(self, path, **kwargs):
  171. """ Submit delete request to the path.
  172. :arg
  173. path (str): Path for request.
  174. :return
  175. Response the request.
  176. :exception
  177. HttpError: Can't connect to server.
  178. """
  179. try:
  180. return self._s.delete(urljoin(self.base_url, path),
  181. params=kwargs,
  182. headers=self.headers,
  183. timeout=self.timeout,
  184. verify=self.verify)
  185. except Exception as e:
  186. raise KeycloakConnectionError(
  187. "Can't connect to server (%s)" % e)