From 1dcb9a9e9e796e042e210360964e4fd1fe907058 Mon Sep 17 00:00:00 2001 From: Chris Talkington Date: Mon, 6 Apr 2020 19:33:36 -0500 Subject: [PATCH 01/13] Update ipp.py --- pyipp/ipp.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/pyipp/ipp.py b/pyipp/ipp.py index 1a71a4a04..759697cad 100644 --- a/pyipp/ipp.py +++ b/pyipp/ipp.py @@ -77,6 +77,7 @@ async def _request( uri: str = "", data: Optional[Any] = None, params: Optional[Mapping[str, str]] = None, + : bool = True, ) -> Any: """Handle a request to an IPP server.""" scheme = "https" if self.tls else "http" @@ -145,20 +146,7 @@ async def _request( }, ) - content = await response.read() - - try: - parsed_content = parse_response(content) - except (StructError, Exception) as exception: # disable=broad-except - raise IPPParseError from exception - - if parsed_content["status-code"] != 0: - raise IPPError( - "Unexpected printer status code", - {"status-code": parsed_content["status-code"]}, - ) - - return parsed_content + return await response.read() def _build_printer_uri(self) -> str: scheme = "ipps" if self.tls else "ipp" @@ -189,7 +177,20 @@ def _message(self, operation: IppOperation, msg: dict) -> dict: async def execute(self, operation: IppOperation, message: dict) -> dict: """Send a request message to the server.""" message = self._message(operation, message) - return await self._request(data=message) + response = await self._request(data=message) + + try: + parsed = parse_response(response) + except (StructError, Exception) as exception: # disable=broad-except + raise IPPParseError from exception + + if parsed["status-code"] != 0: + raise IPPError( + "Unexpected printer status code", + {"status-code": parsed["status-code"]}, + ) + + return parsed async def close(self) -> None: """Close open client session.""" From d305420b92b9f8cb47a4179aebeaf6b829ebb0cb Mon Sep 17 00:00:00 2001 From: Chris Talkington Date: Mon, 6 Apr 2020 19:35:58 -0500 Subject: [PATCH 02/13] Update ipp.py --- pyipp/ipp.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pyipp/ipp.py b/pyipp/ipp.py index 759697cad..2278ec136 100644 --- a/pyipp/ipp.py +++ b/pyipp/ipp.py @@ -77,7 +77,6 @@ async def _request( uri: str = "", data: Optional[Any] = None, params: Optional[Mapping[str, str]] = None, - : bool = True, ) -> Any: """Handle a request to an IPP server.""" scheme = "https" if self.tls else "http" From 910338d66a2eabc83bcffd5e4179924d92e25288 Mon Sep 17 00:00:00 2001 From: Chris Talkington Date: Mon, 6 Apr 2020 19:46:02 -0500 Subject: [PATCH 03/13] Update ipp.py --- pyipp/ipp.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyipp/ipp.py b/pyipp/ipp.py index 2278ec136..102f9bdd5 100644 --- a/pyipp/ipp.py +++ b/pyipp/ipp.py @@ -1,6 +1,6 @@ """Asynchronous Python client for IPP.""" import asyncio -from socket import gaierror as SocketGIAEroor +from socket import gaierror as SocketGIAError from struct import error as StructError from typing import Any, Mapping, Optional @@ -121,7 +121,7 @@ async def _request( raise IPPConnectionError( "Timeout occurred while connecting to IPP server." ) from exception - except (aiohttp.ClientError, SocketGIAEroor) as exception: + except (aiohttp.ClientError, SocketGIAError) as exception: raise IPPConnectionError( "Error occurred while communicating with IPP server." ) from exception From f1ffda8135afd43b10e7586d6160fff77ca202bb Mon Sep 17 00:00:00 2001 From: Chris Talkington Date: Mon, 6 Apr 2020 19:52:13 -0500 Subject: [PATCH 04/13] Create debug.py --- examples/debug.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 examples/debug.py diff --git a/examples/debug.py b/examples/debug.py new file mode 100644 index 000000000..9d8d0d5ef --- /dev/null +++ b/examples/debug.py @@ -0,0 +1,39 @@ +# pylint: disable=W0621 +"""Asynchronous Python client for IPP.""" +import asyncio + +from pyipp import IPP + + +async def main(): + """Show example of connecting to your IPP print server.""" + async with IPP("ipps://EPSON761251.local:631/ipp/print") as ipp: + response = await ipp.execute( + 0x000B, + { + "operation-attributes-tag": { + "requested-attributes": [ + "printer-name", + "printer-type", + "printer-location", + "printer-info", + "printer-make-and-model", + "printer-state", + "printer-state-message", + "printer-state-reason", + "printer-uri-supported", + "device-uri", + "printer-is-shared", + ], + }, + }, + raw=True, + ) + + with open("printer-attributes.bin", "wb") as f: + f.write(response) + f.close() + +if __name__ == "__main__": + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) From 8b16548d9032334218c2f0a9b5b4ba42ac001c53 Mon Sep 17 00:00:00 2001 From: Chris Talkington Date: Mon, 6 Apr 2020 19:56:18 -0500 Subject: [PATCH 05/13] Update ipp.py --- pyipp/ipp.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pyipp/ipp.py b/pyipp/ipp.py index 102f9bdd5..4f97171f5 100644 --- a/pyipp/ipp.py +++ b/pyipp/ipp.py @@ -178,6 +178,8 @@ async def execute(self, operation: IppOperation, message: dict) -> dict: message = self._message(operation, message) response = await self._request(data=message) + if raw: + return try: parsed = parse_response(response) except (StructError, Exception) as exception: # disable=broad-except @@ -191,6 +193,12 @@ async def execute(self, operation: IppOperation, message: dict) -> dict: return parsed + async def raw(self, operation: IppOperation, message: dict) -> bytes: + """Send a request message to the server and return raw response.""" + message = self._message(operation, message) + + return await self._request(data=message) + async def close(self) -> None: """Close open client session.""" if self._session and self._close_session: From 43fcc284248ac0ea26d50cf670430b82c4a86432 Mon Sep 17 00:00:00 2001 From: Chris Talkington Date: Mon, 6 Apr 2020 19:56:49 -0500 Subject: [PATCH 06/13] Update ipp.py --- pyipp/ipp.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pyipp/ipp.py b/pyipp/ipp.py index 4f97171f5..c6de1616e 100644 --- a/pyipp/ipp.py +++ b/pyipp/ipp.py @@ -178,8 +178,6 @@ async def execute(self, operation: IppOperation, message: dict) -> dict: message = self._message(operation, message) response = await self._request(data=message) - if raw: - return try: parsed = parse_response(response) except (StructError, Exception) as exception: # disable=broad-except From d9f1071c65a41cfe0ef547de92e93cf008943eaf Mon Sep 17 00:00:00 2001 From: Chris Talkington Date: Mon, 6 Apr 2020 19:58:04 -0500 Subject: [PATCH 07/13] Update debug.py --- examples/debug.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/debug.py b/examples/debug.py index 9d8d0d5ef..0652dbf65 100644 --- a/examples/debug.py +++ b/examples/debug.py @@ -8,7 +8,7 @@ async def main(): """Show example of connecting to your IPP print server.""" async with IPP("ipps://EPSON761251.local:631/ipp/print") as ipp: - response = await ipp.execute( + response = await ipp.raw( 0x000B, { "operation-attributes-tag": { @@ -27,7 +27,6 @@ async def main(): ], }, }, - raw=True, ) with open("printer-attributes.bin", "wb") as f: From 8bee3b830aadc8b65f590b34826f1dbb9bf8e399 Mon Sep 17 00:00:00 2001 From: Chris Talkington Date: Mon, 6 Apr 2020 20:05:38 -0500 Subject: [PATCH 08/13] Update and rename test_printer.py to test_interface.py --- tests/{test_printer.py => test_interface.py} | 27 ++++++++++++++++++++ 1 file changed, 27 insertions(+) rename tests/{test_printer.py => test_interface.py} (83%) diff --git a/tests/test_printer.py b/tests/test_interface.py similarity index 83% rename from tests/test_printer.py rename to tests/test_interface.py index bcac499d2..46548fc5b 100644 --- a/tests/test_printer.py +++ b/tests/test_interface.py @@ -100,3 +100,30 @@ async def test_printer(aresponses): assert printer.markers[4].level == 92 assert printer.markers[4].low_level == 15 assert printer.markers[4].high_level == 100 + +@pytest.mark.asyncio +async def test_raw(aresponses): + """Test raw method is handled correctly.""" + aresponses.add( + MATCH_DEFAULT_HOST, + DEFAULT_PRINTER_PATH, + "POST", + aresponses.Response( + status=200, + headers={"Content-Type": "application/ipp"}, + body=load_fixture_binary("get-printer-attributes-epsonxp6000.bin"), + ), + ) + + async with ClientSession() as session: + ipp = IPP(DEFAULT_PRINTER_URI, session=session) + response = await ipp.raw( + IppOperation.GET_PRINTER_ATTRIBUTES, + { + "operation-attributes-tag": { + "requested-attributes": DEFAULT_PRINTER_ATTRIBUTES, + }, + }, + ) + + assert isinstance(response, bytes) From 3888c01150c1c0d215ba3ffbf2b5a8e8a9487cea Mon Sep 17 00:00:00 2001 From: Chris Talkington Date: Mon, 6 Apr 2020 20:06:01 -0500 Subject: [PATCH 09/13] Rename test_ipp.py to test_client.py --- tests/{test_ipp.py => test_client.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{test_ipp.py => test_client.py} (100%) diff --git a/tests/test_ipp.py b/tests/test_client.py similarity index 100% rename from tests/test_ipp.py rename to tests/test_client.py From cceaf3f09ad573158139f8a510051f0b798383cc Mon Sep 17 00:00:00 2001 From: Chris Talkington Date: Mon, 6 Apr 2020 20:06:19 -0500 Subject: [PATCH 10/13] Update test_client.py --- tests/test_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_client.py b/tests/test_client.py index db2fa1eb1..c6dde93da 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,4 +1,4 @@ -"""Tests for IPP.""" +"""Tests for IPP Client.""" import asyncio import pytest From 22d0b38883d55e6fb51c8a04b0095ad6da28cf3b Mon Sep 17 00:00:00 2001 From: Chris Talkington Date: Mon, 6 Apr 2020 20:06:50 -0500 Subject: [PATCH 11/13] Update test_interface.py --- tests/test_interface.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_interface.py b/tests/test_interface.py index 46548fc5b..b657eea31 100644 --- a/tests/test_interface.py +++ b/tests/test_interface.py @@ -1,4 +1,4 @@ -"""Tests for IPP Printer model.""" +"""Tests for IPP public interface.""" import pytest from aiohttp import ClientSession from pyipp import IPP, Printer From 82e1ce36de642639e52d454dcc72b05c97471029 Mon Sep 17 00:00:00 2001 From: Chris Talkington Date: Mon, 6 Apr 2020 20:09:42 -0500 Subject: [PATCH 12/13] Update test_interface.py --- tests/test_interface.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_interface.py b/tests/test_interface.py index b657eea31..68f2239b6 100644 --- a/tests/test_interface.py +++ b/tests/test_interface.py @@ -2,6 +2,8 @@ import pytest from aiohttp import ClientSession from pyipp import IPP, Printer +from pyipp.const import DEFAULT_PRINTER_ATTRIBUTES +from pyipp.enums import IppOperation from . import ( DEFAULT_PRINTER_HOST, @@ -101,6 +103,7 @@ async def test_printer(aresponses): assert printer.markers[4].low_level == 15 assert printer.markers[4].high_level == 100 + @pytest.mark.asyncio async def test_raw(aresponses): """Test raw method is handled correctly.""" From a99b2a10d8399c227703672cba98ecaa40db7f90 Mon Sep 17 00:00:00 2001 From: Chris Talkington Date: Mon, 6 Apr 2020 20:10:16 -0500 Subject: [PATCH 13/13] Update debug.py --- examples/debug.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/debug.py b/examples/debug.py index 0652dbf65..0deacd854 100644 --- a/examples/debug.py +++ b/examples/debug.py @@ -33,6 +33,7 @@ async def main(): f.write(response) f.close() + if __name__ == "__main__": loop = asyncio.get_event_loop() loop.run_until_complete(main())