diff --git a/examples/add_modify_printer.py b/examples/add_modify_printer.py new file mode 100644 index 00000000..709404e6 --- /dev/null +++ b/examples/add_modify_printer.py @@ -0,0 +1,35 @@ +# pylint: disable=W0621 +"""Asynchronous Python client for IPP.""" +import asyncio +from pathlib import Path + +from pyipp import IPP +from pyipp.enums import IppOperation + + +async def main() -> None: + """Show an example of add or modifying printer on your IP print server.""" + content = Path("/path/to/driver.ppd").read_bytes() + + async with IPP( + host="ipp://127.0.0.1:631/printers/My_New_Printer", + username="", + password="", + ) as ipp: + response = await ipp.execute( + IppOperation.CUPS_ADD_MODIFY_PRINTER, + { + "printer-attributes-tag": { + "device-uri": "socket://192.168.0.12:9100", + "printer-info": "My awesome printer", + "printer-location": "office", + }, + "file": content, + }, + ) + + print(response) + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/examples/print_example.py b/examples/print_job.py similarity index 75% rename from examples/print_example.py rename to examples/print_job.py index 2f005559..12541254 100644 --- a/examples/print_example.py +++ b/examples/print_job.py @@ -1,18 +1,17 @@ # pylint: disable=W0621 """Asynchronous Python client for IPP.""" import asyncio +from pathlib import Path from pyipp import IPP from pyipp.enums import IppOperation async def main() -> None: + """Show an example of printing on your IP print server.""" + content = Path("/path/to/pdf.pdf").read_bytes() - pdf_file = '/path/to/pdf.pfd' - with open(pdf_file, 'rb') as f: - content = f.read() - - """Show example of executing operation against your IPP print server.""" + # then the printer must be shared if CUPS is used async with IPP("ipp://192.168.1.92:631/ipp/print") as ipp: response = await ipp.execute( IppOperation.PRINT_JOB, @@ -22,7 +21,7 @@ async def main() -> None: "job-name": "My Test Job", "document-format": "application/pdf", }, - 'data': content, + "file": content, }, ) diff --git a/src/pyipp/serializer.py b/src/pyipp/serializer.py index 3a9df1b6..a180cd19 100644 --- a/src/pyipp/serializer.py +++ b/src/pyipp/serializer.py @@ -93,7 +93,7 @@ def encode_dict(data: dict[str, Any]) -> bytes: encoded += struct.pack(">b", IppTag.END.value) - if "data" in data: - encoded += data["data"] + if isinstance(data.get("file"), bytes): + encoded += data["file"] return encoded diff --git a/tests/fixtures/serializer/add-printer-with-driver-file-request-000.bin b/tests/fixtures/serializer/add-printer-with-driver-file-request-000.bin new file mode 100644 index 00000000..3be68133 Binary files /dev/null and b/tests/fixtures/serializer/add-printer-with-driver-file-request-000.bin differ diff --git a/tests/test_serializer.py b/tests/test_serializer.py index 410f8de8..5724fc1a 100644 --- a/tests/test_serializer.py +++ b/tests/test_serializer.py @@ -74,3 +74,28 @@ def test_encode_dict() -> None: assert result == load_fixture_binary( "serializer/get-printer-attributes-request-000.bin", ) + + +def test_encode_dict_with_file() -> None: + """Test the encode_dict method with upload file.""" + result = serializer.encode_dict( + { + "version": DEFAULT_PROTO_VERSION, + "operation": IppOperation.CUPS_ADD_MODIFY_PRINTER, + "request-id": 1, + "operation-attributes-tag": { + "attributes-charset": DEFAULT_CHARSET, + "attributes-natural-language": DEFAULT_CHARSET_LANGUAGE, + "printer-uri": "ipp://printer.example.com:632/printers/My_New_Printer", + "requesting-user-name": "PythonIPP", + }, + "printer-attributes-tag": { + "device-uri": "socket://0.0.0.0:9100", + }, + "file": b"*PPD-Adobe:...", + }, + ) + + assert result == load_fixture_binary( + "serializer/add-printer-with-driver-file-request-000.bin", + )