Skip to content

Commit

Permalink
Minor cleanup of HTTPConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
julianneswinoga committed Jan 3, 2024
1 parent 9cc8750 commit 810bccd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
8 changes: 4 additions & 4 deletions examples/simple_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"""
Start FlightGear with `--httpd=5050`
"""
props_conn = HTTPConnection('localhost', 5050)
pprint(props_conn.list_props('/', recurse_limit=0)) # List the top-level properties, no recursion
http_conn = HTTPConnection('localhost', 5050)
pprint(http_conn.list_props('/', recurse_limit=0)) # List the top-level properties, no recursion
while True:
alt_ft = props_conn.get_prop('/position/altitude-ft')
alt_ft = http_conn.get_prop('/position/altitude-ft')
print(f'Altitude: {alt_ft:.1f}ft')
props_conn.set_prop('/position/altitude-ft', alt_ft + 20.0)
http_conn.set_prop('/position/altitude-ft', alt_ft + 20.0)
time.sleep(0.1)
16 changes: 9 additions & 7 deletions flightgear_python/fg_if.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,7 @@ class HTTPConnection(PropsConnectionBase):
"""

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.url = f'http://{host}:{tcp_port}/json'
self.session = requests.Session()
self.timeout_s = timeout_s

Expand All @@ -453,7 +451,7 @@ def get_prop(self, prop_str: str) -> Any:
"""
prop_str = self.check_and_normalize_prop_path(prop_str)
resp_json = self.request_shim('GET', self.url + prop_str).json()
converted_value = self._auto_convert_fg_prop(resp_json["value"], resp_json["type"])
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 @@ -468,15 +466,19 @@ def set_prop(self, prop_str: str, value: Any):
# Fetch the type of the property
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"]}
data = {
'path': prop_str,
'value': str(value),
'type': resp_json['type'],
}
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.request_shim('GET', self.url + path).json()
if resp_json["nChildren"] == 0:
if resp_json['nChildren'] == 0:
return [], []
resp_list = resp_json["children"]
resp_list = resp_json['children']
# extract of directories, absolute path
val_list: List[PropertyTreeValue] = []
dir_list: List[str] = []
Expand Down

0 comments on commit 810bccd

Please sign in to comment.