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.

67 lines
2.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
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. from odoo.tools import config
  25. _logger = logging.getLogger(__name__)
  26. def get_route(url):
  27. url_parts = url.split('?')
  28. path = url_parts[0]
  29. query_string = url_parts[1] if len(url_parts) > 1 else None
  30. router = request.httprequest.app.get_db_router(request.db).bind('')
  31. match = router.match(path, query_args=query_string)
  32. method = router.match(path, query_args=query_string)[0]
  33. params = dict(urllib.parse.parse_qsl(query_string))
  34. if len(match) > 1:
  35. params.update(match[1])
  36. return method, params, path
  37. def make_error_response(status, message):
  38. exception = werkzeug.exceptions.HTTPException()
  39. exception.code = status
  40. exception.description = message
  41. return exception
  42. def get_response(url):
  43. if not bool(urllib.parse.urlparse(url).netloc):
  44. base_url = request.env['ir.config_parameter'].sudo().get_param('web.base.url')
  45. method, params, path = get_route(url)
  46. params.update({'csrf_token': request.csrf_token()})
  47. session = requests.Session()
  48. session.cookies['session_id'] = request.session.sid
  49. try:
  50. response = session.post("%s%s" % (base_url, path), params)
  51. return response.status_code, response.headers, response.content
  52. except requests.exceptions.RequestException as exception:
  53. return exception.response.status_code, exception.response.headers, exception.response.reason
  54. else:
  55. try:
  56. response = requests.get(url)
  57. return response.status_code, response.headers, response.content
  58. except requests.exceptions.RequestException as exception:
  59. return exception.response.status_code, exception.response.headers, exception.response.reason