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.

164 lines
5.1 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
  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. """
  30. def __init__(self, base_url, headers={}, timeout=60):
  31. self._base_url = base_url
  32. self._headers = headers
  33. self._timeout = timeout
  34. @property
  35. def base_url(self):
  36. """ Return base url in use for requests to the server. """
  37. return self._base_url
  38. @base_url.setter
  39. def base_url(self, value):
  40. """ """
  41. self._base_url = value
  42. @property
  43. def timeout(self):
  44. """ Return timeout in use for request to the server. """
  45. return self._timeout
  46. @timeout.setter
  47. def timeout(self, value):
  48. """ """
  49. self._timeout = value
  50. @property
  51. def headers(self):
  52. """ Return header request to the server. """
  53. return self._headers
  54. @headers.setter
  55. def headers(self, value):
  56. """ """
  57. self._headers = value
  58. def param_headers(self, key):
  59. """ Return a specific header parameter.
  60. :arg
  61. key (str): Header parameters key.
  62. :return:
  63. If the header parameters exist, return its value.
  64. """
  65. return self.headers.get(key)
  66. def clean_headers(self):
  67. """ Clear header parameters. """
  68. self.headers = {}
  69. def exist_param_headers(self, key):
  70. """ Check if the parameter exists in the header.
  71. :arg
  72. key (str): Header parameters key.
  73. :return:
  74. If the header parameters exist, return True.
  75. """
  76. return self.param_headers(key) is not None
  77. def add_param_headers(self, key, value):
  78. """ Add a single parameter inside the header.
  79. :arg
  80. key (str): Header parameters key.
  81. value (str): Value to be added.
  82. """
  83. self.headers[key] = value
  84. def del_param_headers(self, key):
  85. """ Remove a specific parameter.
  86. :arg
  87. key (str): Key of the header parameters.
  88. """
  89. self.headers.pop(key, None)
  90. def raw_get(self, path, **kwargs):
  91. """ Submit get request to the path.
  92. :arg
  93. path (str): Path for request.
  94. :return
  95. Response the request.
  96. :exception
  97. HttpError: Can't connect to server.
  98. """
  99. try:
  100. return requests.get(urljoin(self.base_url, path),
  101. params=kwargs,
  102. headers=self.headers,
  103. timeout=self.timeout)
  104. except Exception as e:
  105. raise KeycloakConnectionError(
  106. "Can't connect to server (%s)" % e)
  107. def raw_post(self, path, data, **kwargs):
  108. """ Submit post request to the path.
  109. :arg
  110. path (str): Path for request.
  111. data (dict): Payload for request.
  112. :return
  113. Response the request.
  114. :exception
  115. HttpError: Can't connect to server.
  116. """
  117. try:
  118. return requests.post(urljoin(self.base_url, path),
  119. params=kwargs,
  120. data=data,
  121. headers=self.headers,
  122. timeout=self.timeout)
  123. except Exception as e:
  124. raise KeycloakConnectionError(
  125. "Can't connect to server (%s)" % e)
  126. def raw_put(self, path, data, **kwargs):
  127. """ Submit put request to the path.
  128. :arg
  129. path (str): Path for request.
  130. data (dict): Payload for request.
  131. :return
  132. Response the request.
  133. :exception
  134. HttpError: Can't connect to server.
  135. """
  136. try:
  137. return requests.put(urljoin(self.base_url, path),
  138. params=kwargs,
  139. data=data,
  140. headers=self.headers,
  141. timeout=self.timeout)
  142. except Exception as e:
  143. raise KeycloakConnectionError(
  144. "Can't connect to server (%s)" % e)