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.

153 lines
5.0 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
  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. 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 specific header parameter.
  51. :arg
  52. key (str): Header parameters key.
  53. :return:
  54. If the header parameters exist, return its value.
  55. """
  56. return self.__headers.get(key)
  57. def clean_headers(self):
  58. """ Clear header parameters. """
  59. self.__headers = {}
  60. def exist_param_headers(self, key):
  61. """ Check if the parameter exists in the header.
  62. :arg
  63. key (str): Header parameters key.
  64. :return:
  65. If the header parameters exist, return True.
  66. """
  67. return self.get_param_headers(key) is not None
  68. def add_param_headers(self, key, value):
  69. """ Add a single parameter inside the header.
  70. :arg
  71. key (str): Header parameters key.
  72. value (str): Value to be added.
  73. """
  74. self.__headers[key] = value
  75. def del_param_headers(self, key):
  76. """ Remove a specific parameter.
  77. :arg
  78. key (str): Key of the header parameters.
  79. """
  80. self.__headers.pop(key, None)
  81. def raw_get(self, path, **kwargs):
  82. """ Submit get request to the path.
  83. :arg
  84. path (str): Path for request.
  85. :return
  86. Response the request.
  87. :exception
  88. HttpError: Can't connect to server.
  89. """
  90. try:
  91. return requests.get(urljoin(self.get_url(), path),
  92. params=kwargs,
  93. headers=self.get_headers(),
  94. timeout=self.get_timeout())
  95. except Exception as e:
  96. raise KeycloakConnectionError(
  97. "Can't connect to server (%s)" % e)
  98. def raw_post(self, path, data, **kwargs):
  99. """ Submit post request to the path.
  100. :arg
  101. path (str): Path for request.
  102. data (dict): Payload for request.
  103. :return
  104. Response the request.
  105. :exception
  106. HttpError: Can't connect to server.
  107. """
  108. try:
  109. return requests.post(urljoin(self.get_url(), path),
  110. params=kwargs,
  111. data=data,
  112. headers=self.get_headers(),
  113. timeout=self.get_timeout())
  114. except Exception as e:
  115. raise KeycloakConnectionError(
  116. "Can't connect to server (%s)" % e)
  117. def raw_put(self, path, data, **kwargs):
  118. """ Submit put request to the path.
  119. :arg
  120. path (str): Path for request.
  121. data (dict): Payload for request.
  122. :return
  123. Response the request.
  124. :exception
  125. HttpError: Can't connect to server.
  126. """
  127. try:
  128. return requests.put(urljoin(self.get_url(), path),
  129. params=kwargs,
  130. data=data,
  131. headers=self.get_headers(),
  132. timeout=self.get_timeout())
  133. except Exception as e:
  134. raise KeycloakConnectionError(
  135. "Can't connect to server (%s)" % e)