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.

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