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.1 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. from urllib.parse import urljoin
  18. from .exceptions import *
  19. import requests
  20. class ConnectionManager(object):
  21. """ Represents a simple server connection.
  22. Args:
  23. base_url (str): The URL server
  24. headers (dict): The header parameters of the requests to the server.
  25. timeout (int): Timeout to use for requests to the server.
  26. """
  27. def __init__(self, base_url, headers={}, timeout=60):
  28. self.__base_url = base_url
  29. self.__headers = headers
  30. self.__timeout = timeout
  31. def get_url(self):
  32. """ Return base url in use for requests to the server. """
  33. return self.__base_url
  34. def get_timeout(self):
  35. """ Return timeout in use for request to the server. """
  36. return self.__timeout
  37. def set_headers(self, params):
  38. """ Update header request to the server.
  39. :arg
  40. params (dict): Parameters header request.
  41. """
  42. self.__headers = params
  43. def get_headers(self):
  44. """ Return header request to the server. """
  45. return self.__headers
  46. def get_param_headers(self, key):
  47. """ Return a single header parameter.
  48. :arg
  49. key (str): Key of the header parameters.
  50. :return:
  51. If the header parameters exist, return value him.
  52. """
  53. return self.__headers[key] if key in self.__headers.keys() else None
  54. def clean_headers(self):
  55. """ Clear header parameters. """
  56. self.__headers = {}
  57. def exist_param_headers(self, key):
  58. """ Check if the parameter exist in header.
  59. :arg
  60. key (str): Key of the header parameters.
  61. :return:
  62. If the header parameters exist, return True.
  63. """
  64. return True if self.get_param_headers(key) else False
  65. def add_param_headers(self, key, value):
  66. """ Add a single parameter in header.
  67. :arg
  68. key (str): Key of the header parameters.
  69. value (str): Value for the header parameter.
  70. """
  71. request_headers = self.__headers.copy()
  72. request_headers.update({key: value})
  73. self.set_headers(request_headers)
  74. def del_param_headers(self, key):
  75. """ Remove a single header parameter.
  76. :arg
  77. key (str): Key of the header parameters.
  78. """
  79. if self.get_param_headers(key):
  80. del self.__headers[key]
  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)