Skip to content

[Documentation] Example #9

@phigjm

Description

@phigjm

It would be cool to have a complet example code.
E.g. i tried this so far:

from moonraker_api import MoonrakerClient, MoonrakerListener


class APIConnector(MoonrakerListener):
    def __init__(self):
        self.running = False
        self.client = MoonrakerClient(
            self,
            "yourURL.local",
            "7125",
            "",
        )

    async def start(self) -> None:
        """Start the websocket connection."""
        self.running = True
        return await self.client.connect()

    async def stop(self) -> None:
        """Stop the websocket connection."""
        self.running = False
        await self.client.disconnect()


async def get_printer_info():
    api_connector = APIConnector()
    await api_connector.start()
    response = await api_connector.client.request("printer.info")
    return response


printer_info = asyncio.run(get_printer_info())

print(printer_info)

but I always get the error:

response = await api_connector.client.request("printer.info")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: object AwaitableTaskContext can't be used in 'await' expression
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x0000024C10544290>```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions