Skip to content

Commit

Permalink
Add timeout_s param to HTTPConnection, shim session.request so we can…
Browse files Browse the repository at this point in the history
… return a FGConnectionError, add wrong port HTTP integration test
  • Loading branch information
julianneswinoga committed Jan 3, 2024
1 parent 620bb28 commit b9aa576
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
23 changes: 16 additions & 7 deletions flightgear_python/fg_if.py
Expand Up @@ -425,13 +425,22 @@ class HTTPConnection(PropsConnectionBase):
:param host: IP address of FG (usually localhost)
:param tcp_port: Port of the telnet socket (i.e. the ``5050`` from\
``--httpd=5050``)
:param timeout_s: Optional timeout value in seconds for the HTTP connection
"""

def __init__(self, host: str, tcp_port: int):
def __init__(self, host: str, tcp_port: int, timeout_s: float = 2.0):
self.host = host
self.port = tcp_port
self.url = f'http://{self.host}:{self.port}/json'
self.session = requests.Session()
self.timeout_s = timeout_s

def request_shim(self, method: str, url: str, *args, **kwargs) -> requests.Response:
# Shim layer over session.request so we can set default options and handle errors in a unified fashion
try:
return self.session.request(method, url, *args, timeout=self.timeout_s, **kwargs)
except requests.exceptions.ConnectionError:
raise FGConnectionError(f'Problem connecting to {self.url}')

def get_prop(self, prop_str: str) -> Any:
"""
Expand All @@ -443,9 +452,9 @@ def get_prop(self, prop_str: str) -> Any:
will pre-convert it (i.e. make an int from a string)
"""
prop_str = self.check_and_normalize_prop_path(prop_str)
resp_json = self.session.get(self.url + prop_str).json()
value = self._auto_convert_fg_prop(resp_json["value"], resp_json["type"])
return value
resp_json = self.request_shim('GET', self.url + prop_str).json()
converted_value = self._auto_convert_fg_prop(resp_json["value"], resp_json["type"])
return converted_value

def set_prop(self, prop_str: str, value: Any):
"""
Expand All @@ -457,14 +466,14 @@ def set_prop(self, prop_str: str, value: Any):
"""
prop_str = self.check_and_normalize_prop_path(prop_str)
# Fetch the type of the property
resp_json = self.session.get(self.url + prop_str).json()
resp_json = self.request_shim('GET', self.url + prop_str).json()
# Set the property
data = {"path": prop_str, "value": str(value), "type": resp_json["type"]}
self.session.post(self.url + prop_str, json=data)
self.request_shim('POST', self.url + prop_str, json=data)
# We don't care about the response

def get_values_and_dirs(self, path: str) -> Tuple[List[PropertyTreeValue], List[str]]:
resp_json = self.session.get(self.url + path).json()
resp_json = self.request_shim('GET', self.url + path).json()
if resp_json["nChildren"] == 0:
return [], []
resp_list = resp_json["children"]
Expand Down
7 changes: 7 additions & 0 deletions tests/test_integration_FG_http.py
Expand Up @@ -3,6 +3,7 @@
import pytest

from flightgear_python.fg_if import HTTPConnection
from flightgear_python.fg_util import FGConnectionError


pytestmark = pytest.mark.fg_integration
Expand Down Expand Up @@ -39,3 +40,9 @@ def test_http_set_prop():
h_con.set_prop('/controls/flight/aileron', -0.5)
aileron_value = h_con.get_prop('/controls/flight/aileron')
assert aileron_value == -0.5


def test_http_wrong_port():
t_con = HTTPConnection('localhost', 123)
with pytest.raises(FGConnectionError):
t_con.get_prop('/controls/flight/aileron')

0 comments on commit b9aa576

Please sign in to comment.