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.

59 lines
2.2 KiB

6 years ago
  1. ###################################################################################
  2. #
  3. # Copyright (C) 2018 MuK IT GmbH
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (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 Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. ###################################################################################
  19. import urllib
  20. import logging
  21. import requests
  22. import werkzeug
  23. from odoo.http import request
  24. _logger = logging.getLogger(__name__)
  25. def get_route(url):
  26. url_parts = url.split('?')
  27. path = url_parts[0]
  28. query_string = url_parts[1] if len(url_parts) > 1 else None
  29. router = request.httprequest.app.get_db_router(request.db).bind('')
  30. match = router.match(path, query_args=query_string)
  31. method = router.match(path, query_args=query_string)[0]
  32. params = dict(urllib.parse.parse_qsl(query_string))
  33. if len(match) > 1:
  34. params.update(match[1])
  35. return method, params
  36. def make_error_response(status, message):
  37. exception = werkzeug.exceptions.HTTPException()
  38. exception.code = status
  39. exception.description = message
  40. return exception
  41. def get_response(url):
  42. if not bool(urllib.parse.urlparse(url).netloc):
  43. method, params = get_route(url)
  44. response = method(**params)
  45. return response.status_code, response.headers, response.data
  46. else:
  47. try:
  48. response = requests.get(url)
  49. return response.status_code, response.headers, response.content
  50. except requests.exceptions.RequestException as exception:
  51. return exception.response.status_code, exception.response.headers, exception.response.reason