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.

199 lines
6.2 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
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2017 Marcos Pereira <marcospereira.mpj@gmail.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Lesser General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. try:
  18. from urllib.parse import urljoin
  19. except ImportError:
  20. from urlparse import urljoin
  21. from .exceptions import *
  22. import requests
  23. class ConnectionManager(object):
  24. """ Represents a simple server connection.
  25. Args:
  26. base_url (str): The server URL.
  27. headers (dict): The header parameters of the requests to the server.
  28. timeout (int): Timeout to use for requests to the server.
  29. verify (bool): Verify server SSL.
  30. """
  31. def __init__(self, base_url, headers={}, timeout=60, verify=True):
  32. self._base_url = base_url
  33. self._headers = headers
  34. self._timeout = timeout
  35. self._verify = verify
  36. @property
  37. def base_url(self):
  38. """ Return base url in use for requests to the server. """
  39. return self._base_url
  40. @base_url.setter
  41. def base_url(self, value):
  42. """ """
  43. self._base_url = value
  44. @property
  45. def timeout(self):
  46. """ Return timeout in use for request to the server. """
  47. return self._timeout
  48. @timeout.setter
  49. def timeout(self, value):
  50. """ """
  51. self._timeout = value
  52. @property
  53. def verify(self):
  54. """ Return verify in use for request to the server. """
  55. return self._verify
  56. @verify.setter
  57. def verify(self, value):
  58. """ """
  59. self._verify = value
  60. @property
  61. def headers(self):
  62. """ Return header request to the server. """
  63. return self._headers
  64. @headers.setter
  65. def headers(self, value):
  66. """ """
  67. self._headers = value
  68. def param_headers(self, key):
  69. """ Return a specific header parameter.
  70. :arg
  71. key (str): Header parameters key.
  72. :return:
  73. If the header parameters exist, return its value.
  74. """
  75. return self.headers.get(key)
  76. def clean_headers(self):
  77. """ Clear header parameters. """
  78. self.headers = {}
  79. def exist_param_headers(self, key):
  80. """ Check if the parameter exists in the header.
  81. :arg
  82. key (str): Header parameters key.
  83. :return:
  84. If the header parameters exist, return True.
  85. """
  86. return self.param_headers(key) is not None
  87. def add_param_headers(self, key, value):
  88. """ Add a single parameter inside the header.
  89. :arg
  90. key (str): Header parameters key.
  91. value (str): Value to be added.
  92. """
  93. self.headers[key] = value
  94. def del_param_headers(self, key):
  95. """ Remove a specific parameter.
  96. :arg
  97. key (str): Key of the header parameters.
  98. """
  99. self.headers.pop(key, None)
  100. def raw_get(self, path, **kwargs):
  101. """ Submit get request to the path.
  102. :arg
  103. path (str): Path for request.
  104. :return
  105. Response the request.
  106. :exception
  107. HttpError: Can't connect to server.
  108. """
  109. try:
  110. return requests.get(urljoin(self.base_url, path),
  111. params=kwargs,
  112. headers=self.headers,
  113. timeout=self.timeout,
  114. verify=self.verify)
  115. except Exception as e:
  116. raise KeycloakConnectionError(
  117. "Can't connect to server (%s)" % e)
  118. def raw_post(self, path, data, **kwargs):
  119. """ Submit post request to the path.
  120. :arg
  121. path (str): Path for request.
  122. data (dict): Payload for request.
  123. :return
  124. Response the request.
  125. :exception
  126. HttpError: Can't connect to server.
  127. """
  128. try:
  129. return requests.post(urljoin(self.base_url, path),
  130. params=kwargs,
  131. data=data,
  132. headers=self.headers,
  133. timeout=self.timeout,
  134. verify=self.verify)
  135. except Exception as e:
  136. raise KeycloakConnectionError(
  137. "Can't connect to server (%s)" % e)
  138. def raw_put(self, path, data, **kwargs):
  139. """ Submit put request to the path.
  140. :arg
  141. path (str): Path for request.
  142. data (dict): Payload for request.
  143. :return
  144. Response the request.
  145. :exception
  146. HttpError: Can't connect to server.
  147. """
  148. try:
  149. return requests.put(urljoin(self.base_url, path),
  150. params=kwargs,
  151. data=data,
  152. headers=self.headers,
  153. timeout=self.timeout,
  154. verify=self.verify)
  155. except Exception as e:
  156. raise KeycloakConnectionError(
  157. "Can't connect to server (%s)" % e)
  158. def raw_delete(self, path, **kwargs):
  159. """ Submit delete request to the path.
  160. :arg
  161. path (str): Path for request.
  162. :return
  163. Response the request.
  164. :exception
  165. HttpError: Can't connect to server.
  166. """
  167. try:
  168. return requests.delete(urljoin(self.base_url, path),
  169. params=kwargs,
  170. headers=self.headers,
  171. timeout=self.timeout,
  172. verify=self.verify)
  173. except Exception as e:
  174. raise KeycloakConnectionError(
  175. "Can't connect to server (%s)" % e)