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.

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