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.

97 lines
2.9 KiB

  1. # Copyright 2020 Coop IT Easy SCRL fs
  2. # Robin Keunen <robin@coopiteasy.be>
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. import json
  5. import logging
  6. import requests
  7. from werkzeug.exceptions import (
  8. InternalServerError,
  9. NotFound,
  10. )
  11. from odoo import _, api, fields, models
  12. from odoo.exceptions import (
  13. AccessDenied,
  14. )
  15. _logger = logging.getLogger(__name__)
  16. class EMCBackend(models.Model):
  17. _name = "emc.backend"
  18. _description = "EMC Backend"
  19. name = fields.Char(string="Name", required=True)
  20. location = fields.Char(string="Location")
  21. api_key = fields.Char(string="API Key")
  22. description = fields.Text(string="Description", required=False)
  23. active = fields.Boolean(string="active", default=True)
  24. @api.multi
  25. def http_get(self, url, params=None, headers=None):
  26. self.ensure_one()
  27. headers = self._add_api_key(headers)
  28. if url.startswith("/"):
  29. url = self.location + url
  30. return requests.get(url, params=params, headers=headers)
  31. @api.multi
  32. def http_get_content(self, url, params=None, headers=None):
  33. self.ensure_one()
  34. response = self.http_get(url, params=params, headers=headers)
  35. if response.status_code == 200:
  36. content = response.content.decode("utf-8")
  37. return json.loads(content)
  38. elif response.status_code == 403:
  39. raise AccessDenied(_("You are not allowed to access this resource"))
  40. elif response.status_code == 404:
  41. raise NotFound(
  42. _("Resource not found %s on server" % response.status_code)
  43. )
  44. else: # 500 et al.
  45. content = response.content.decode("utf-8")
  46. raise InternalServerError(
  47. _("request returned status code %s with message %s" % (
  48. response.status_code, content))
  49. )
  50. @api.multi
  51. def http_post(self, url, data, headers=None):
  52. self.ensure_one()
  53. headers = self._add_api_key(headers)
  54. if url.startswith("/"):
  55. url = self.location + url
  56. return requests.post(url, json=data, headers=headers)
  57. @api.multi
  58. def _add_api_key(self, headers):
  59. self.ensure_one()
  60. key_dict = {"API-KEY": self.api_key}
  61. if headers:
  62. headers.update(key_dict)
  63. else:
  64. headers = key_dict
  65. return headers
  66. @api.multi
  67. def action_ping(self):
  68. self.ensure_one()
  69. url = self.location + "/api/ping/test"
  70. try:
  71. response = requests.get(url)
  72. except Exception as e:
  73. _logger.error(e)
  74. raise Warning(_("Failed to connect to backend: %s" % str(e)))
  75. if response.status_code == 200:
  76. content = json.loads(response.content.decode("utf-8"))
  77. raise Warning(_("Success: %s") % content["message"])
  78. else:
  79. raise Warning(
  80. _("Failed to connect to backend: %s" % str(response.content))
  81. )