Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion react_render/core.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import os
import json
import threading

import requests
from react_render.exceptions import ComponentSourceFileNotFound, ComponentRenderingError

DEFAULT_SERVICE_URL = 'http://localhost:63578/render'

_requests_session_store = threading.local()


def requests_keepalive_session() -> requests.Session:
"""
Creates a session that can utilize keep-alive behaviour across multiple
requests, by maintaining the session within a thread-local.
"""
session = getattr(_requests_session_store, "react_session", None)
if not session:
session = requests.Session()
setattr(_requests_session_store, "react_session", session)
return session


def render_component(path_to_source, props=None, to_static_markup=False, json_encoder=None, service_url=None, timeout=10):
if not os.path.exists(path_to_source):
Expand All @@ -16,7 +32,8 @@ def render_component(path_to_source, props=None, to_static_markup=False, json_en
if service_url is None:
service_url = DEFAULT_SERVICE_URL

response = requests.post(service_url,
session = requests_keepalive_session()
response = session.post(service_url,
timeout=timeout,
headers={'Content-Type': 'application/json'},
data=json_encoder({
Expand Down