Skip to content
Snippets Groups Projects
Commit a580abdd0122 authored by Vincent Hatakeyama's avatar Vincent Hatakeyama
Browse files

:sparkles: Redner server an use an unix socket too so also handle it

parent bb5f59d02ac3
No related branches found
Tags 11.0.2.6.0
1 merge request!232.6.0 ✨ Redner server an use an unix socket too so also handle it
......@@ -2,6 +2,11 @@
NEWS
====
11.0.2.6.0
==========
Redner server an use an unix socket too so also handle it.
11.0.2.5.1
==========
......
======
Redner
......@@ -1,5 +2,6 @@
Redner
=======
======
Redner is an innovative solution to produce transactional emails
and documents in PDF or HTML format.
......@@ -26,6 +28,7 @@
UI Changes
----------
* Setting > Redner > Templates
Note: When you have existing templates you want to register onto a new
......
......@@ -21,7 +21,7 @@
"name": "Redner",
"summary": """
Allows to generate transactional emails and documents in PDF or HTML format""",
"version": "11.0.2.5.1",
"version": "11.0.2.6.0",
"author": "XCG Consulting",
"category": "Technical",
"depends": ["base", "mail", "converter"],
......@@ -33,5 +33,6 @@
"views/report_redner.xml",
"views/menu.xml",
],
"external_dependencies": {"python": ["requests_unixsocket"]},
"demo": [],
}
import logging
import time
from urllib.parse import quote
import requests
......@@ -3,5 +4,6 @@
import requests
from odoo import _
from odoo.exceptions import ValidationError
......@@ -6,3 +8,5 @@
from odoo.exceptions import ValidationError
_logger = logging.getLogger(__name__)
try:
......@@ -8,10 +12,7 @@
try:
from urllib.parse import urljoin
except ImportError:
from urlparse import urljoin
logger = logging.getLogger(__name__)
import requests_unixsocket
except (ImportError, IOError) as err:
_logger.debug(err)
class Redner:
......@@ -20,8 +21,8 @@
Args:
api_key(str): provide your Redner API key.
server_url(str): Redner server URL.
server_url(str): Redner server URL or socket path.
timeout(float): Timeout per Redner call, in seconds.
"""
self.api_key = api_key
......@@ -24,8 +25,7 @@
timeout(float): Timeout per Redner call, in seconds.
"""
self.api_key = api_key
self.server_url = server_url
self.account = account
self.timeout = timeout
......@@ -29,7 +29,14 @@
self.account = account
self.timeout = timeout
self.session = requests.sessions.Session()
if server_url.startswith("/"):
self.session = requests_unixsocket.Session()
self.server_url = "http+unix://{}/api/".format(
quote(server_url, safe="")
)
else:
self.session = requests.sessions.Session()
self.server_url = server_url
self.templates = Templates(self)
def call(self, path, http_verb="post", **params):
......@@ -33,7 +40,7 @@
self.templates = Templates(self)
def call(self, path, http_verb="post", **params):
"""Call redner with the specified paramters.
"""Call redner with the specified parameters.
Delegate to ``call_impl``; this is a wrapper to have some retries
before giving up as redner sometimes mistakenly rejects our queries.
"""
......@@ -44,7 +51,7 @@
return self.call_impl(path, http_verb=http_verb, **params)
except Exception as error:
if retry_counter == MAX_REDNERD_TRIES - 1:
logger.error("Redner error: %s", str(error))
_logger.error("Redner error: %s", str(error))
raise error
def call_impl(self, path, http_verb="post", **params):
......@@ -61,7 +68,9 @@
if not self.server_url:
raise ValidationError(
"Cannot find redner config url. "
"Please add it in odoo.conf or in ir.config_parameter"
_(
"Cannot find redner config url. "
"Please add it in odoo.conf or in ir.config_parameter"
)
)
......@@ -66,5 +75,5 @@
)
url = urljoin(self.server_url, path)
url = self.server_url + path
_http_verb = http_verb.upper()
......@@ -69,7 +78,7 @@
_http_verb = http_verb.upper()
logger.info("Redner: Calling %s...", _http_verb)
logger.debug("Redner: Sending to %s > %s", url, params)
_logger.info("Redner: Calling %s...", _http_verb)
_logger.debug("Redner: Sending to %s > %s", url, params)
start = time.time()
r = getattr(self.session, http_verb, "post")(
......@@ -80,8 +89,8 @@
)
complete_time = time.time() - start
logger.info(
_logger.info(
"Redner: Received %s in %.2fms.",
r.status_code,
complete_time * 1000,
)
......@@ -84,10 +93,10 @@
"Redner: Received %s in %.2fms.",
r.status_code,
complete_time * 1000,
)
logger.debug("Redner: Received %s", r.text)
_logger.debug("Redner: Received %s", r.text)
try:
response = r.json()
except Exception:
# If we cannot decode JSON then it's an API error
......@@ -89,9 +98,9 @@
try:
response = r.json()
except Exception:
# If we cannot decode JSON then it's an API error
# having response as text could help debuggin with sentry
# having response as text could help debugging with sentry
response = r.text
if not str(r.status_code).startswith("2"):
......
requests_unixsocket
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment