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.

156 lines
5.2 KiB

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 URL server
  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. def get_url(self):
  35. """ Return base url in use for requests to the server. """
  36. return self.__base_url
  37. def get_timeout(self):
  38. """ Return timeout in use for request to the server. """
  39. return self.__timeout
  40. def set_headers(self, params):
  41. """ Update header request to the server.
  42. :arg
  43. params (dict): Parameters header request.
  44. """
  45. self.__headers = params
  46. def get_headers(self):
  47. """ Return header request to the server. """
  48. return self.__headers
  49. def get_param_headers(self, key):
  50. """ Return a single header parameter.
  51. :arg
  52. key (str): Key of the header parameters.
  53. :return:
  54. If the header parameters exist, return value him.
  55. """
  56. return self.__headers[key] if key in self.__headers.keys() else None
  57. def clean_headers(self):
  58. """ Clear header parameters. """
  59. self.__headers = {}
  60. def exist_param_headers(self, key):
  61. """ Check if the parameter exist in header.
  62. :arg
  63. key (str): Key of the header parameters.
  64. :return:
  65. If the header parameters exist, return True.
  66. """
  67. return True if self.get_param_headers(key) else False
  68. def add_param_headers(self, key, value):
  69. """ Add a single parameter in header.
  70. :arg
  71. key (str): Key of the header parameters.
  72. value (str): Value for the header parameter.
  73. """
  74. request_headers = self.__headers.copy()
  75. request_headers.update({key: value})
  76. self.set_headers(request_headers)
  77. def del_param_headers(self, key):
  78. """ Remove a single header parameter.
  79. :arg
  80. key (str): Key of the header parameters.
  81. """
  82. if self.get_param_headers(key):
  83. del self.__headers[key]
  84. def raw_get(self, path, **kwargs):
  85. """ Submit get request to the path.
  86. :arg
  87. path (str): Path for request.
  88. :return
  89. Response the request.
  90. :exception
  91. HttpError: Can't connect to server.
  92. """
  93. try:
  94. return requests.get(urljoin(self.get_url(), path),
  95. params=kwargs,
  96. headers=self.get_headers(),
  97. timeout=self.get_timeout())
  98. except Exception as e:
  99. raise KeycloakConnectionError(
  100. "Can't connect to server (%s)" % e)
  101. def raw_post(self, path, data, **kwargs):
  102. """ Submit post request to the path.
  103. :arg
  104. path (str): Path for request.
  105. data (dict): Payload for request.
  106. :return
  107. Response the request.
  108. :exception
  109. HttpError: Can't connect to server.
  110. """
  111. try:
  112. return requests.post(urljoin(self.get_url(), path),
  113. params=kwargs,
  114. data=data,
  115. headers=self.get_headers(),
  116. timeout=self.get_timeout())
  117. except Exception as e:
  118. raise KeycloakConnectionError(
  119. "Can't connect to server (%s)" % e)
  120. def raw_put(self, path, data, **kwargs):
  121. """ Submit put request to the path.
  122. :arg
  123. path (str): Path for request.
  124. data (dict): Payload for request.
  125. :return
  126. Response the request.
  127. :exception
  128. HttpError: Can't connect to server.
  129. """
  130. try:
  131. return requests.put(urljoin(self.get_url(), path),
  132. params=kwargs,
  133. data=data,
  134. headers=self.get_headers(),
  135. timeout=self.get_timeout())
  136. except Exception as e:
  137. raise KeycloakConnectionError(
  138. "Can't connect to server (%s)" % e)