Skip to content

Commit

Permalink
changed sync client to return bytes instead of decoded str
Browse files Browse the repository at this point in the history
  • Loading branch information
rsnodgrass committed Jan 29, 2024
1 parent 3b93dc5 commit c388014
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
4 changes: 3 additions & 1 deletion example-async.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ async def main():
await client.ping.ping()

#help(client.volume)

result = await client.volume.get()
print(f"WHAT: {result}")
print(f"Response: {result}")

await client.volume.set(volume=20)

await client.power.off()
Expand Down
10 changes: 7 additions & 3 deletions example-sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ def main():
connection_config=config_overrides)

client.send_raw(b'!PING?')
print(client.ping.ping())
client.power.off()
client.ping.ping()
client.ping.ping()

result = client.volume.get()
print(f"Response: {result}")

client.volume.set(volume=15)

client.power.off()


main()
7 changes: 4 additions & 3 deletions pyavcontrol/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,19 @@ def _prepare_request(**kwargs):
def _extract_vars_in_response(response: bytes) -> dict:
"""Given a response, extract all the known values using the response
message regex defined for this action."""
response_text = response.decode(client.encoding())

if msg := action.definition.get('msg'):
if regex := msg.get('regex'):
return re.match(regex, response).groupdict()
return re.match(regex, response_text).groupdict()

return {}

# noinspection PyUnusedLocal
def _activity_call_sync(self, **kwargs):
"""Synchronous version of making a client call"""
if request := _prepare_request(**kwargs):
if response := client.send_raw(request, wait_for_response=action.response_expected):
response = response.decode(client.encoding())
return _extract_vars_in_response(response)
return
LOG.warning(f'Failed to make request for {action.group}.{action.name}')
Expand All @@ -177,7 +179,6 @@ async def _activity_call_async(self, **kwargs):
if request := _prepare_request(**kwargs):
# noinspection PyUnresolvedReferences
if response := await client.send_raw(request, wait_for_response=action.response_expected):
response = response.decode(client.encoding())
return _extract_vars_in_response(response)
return
LOG.warning(f'Failed to make request for {action.group}.{action.name}')
Expand Down
6 changes: 2 additions & 4 deletions pyavcontrol/connection/sync_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,9 @@ def write_rate_limited(data_bytes: bytes):
callback(result)
return result

def handle_receive(self) -> str:
def handle_receive(self) -> bytes:
skip = 0

print(self._eol)

len_eol = len(self._eol)

# FIXME: implement a much better receive mechanism, without timeouts.
Expand All @@ -128,4 +126,4 @@ def handle_receive(self) -> str:

ret = bytes(result)
LOG.debug(f'Received {self._url} "%s"', ret)
return ret.decode(self._encoding)
return ret

0 comments on commit c388014

Please sign in to comment.