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.

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