![Python package](https://github.com/cmroche/moonraker-api/workflows/Python%20package/badge.svg)
## Moonracker Websocket API Client
Connect, request and subscribe to the Moonraker Websockets API without polling.
**moonraker-api** is a ***fully async*** Python 3 based package for interfacing with Moonraker's API.
## Getting the package
The easiest way to grab **moonraker-api** is through PyPI
`pip3 install moonraker-api`
## Use Moonraker-API
### Connect and Disconnect
```python
class APIConnector(MoonrakerListener):
def __init__():
self.running = False
self.client = MoonrakerClient(
self,
HOST,
PORT,
API-KEY,
)
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()
```
### Query the API
```python
api_connector = APIConnector()
response = await api_connector.client.request("printer.info")
```
### Handle Push Notifications
```python
class APIConnector(MoonrakerListener):
# Other class details, see above ...
async def state_changed(self, state: str) -> None:
"""Notifies of changing websocket state."""
_LOGGER.debug("Stated changed to %s", state)
if state == WEBSOCKET_STATE_CONNECTING:
pass
elif state == WEBSOCKET_STATE_CONNECTED:
pass
elif state == WEBSOCKET_STATE_READY:
pass
elif state == WEBSOCKET_STATE_PAUSED:
pass
elif state == WEBSOCKET_STATE_STOPPED:
pass
async def on_exception(self, exception: BaseException) -> None:
"""Notifies of exceptions from the websocket run loop."""
_LOGGER.debug("Received exception from API websocket %s", str(exception))
if isinstance(exception, ClientNotAuthenticatedError):
self.entry.async_start_reauth(self.hass)
else:
raise exception
async def on_notification(self, method: str, data: Any) -> None:
"""Notifies of state updates."""
_LOGGER.debug("Received notification %s -> %s", method, data)
# Subscription notifications
if method == "notify_status_update":
message = data[0]
timestamp = data[1]
# Do stuff ...
```
Raw data
{
"_id": null,
"home_page": "https://github.com/cmroche/moonraker-api",
"name": "moonraker-api",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": null,
"author": "Clifford Roche",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/a6/cf/63ca85b63d8572489ebf8deee2753baf56209407bc5db11de36370b9e7b4/moonraker_api-2.0.6.tar.gz",
"platform": null,
"description": "![Python package](https://github.com/cmroche/moonraker-api/workflows/Python%20package/badge.svg)\n\n## Moonracker Websocket API Client\n\nConnect, request and subscribe to the Moonraker Websockets API without polling.\n\n**moonraker-api** is a ***fully async*** Python 3 based package for interfacing with Moonraker's API.\n\n## Getting the package\n\nThe easiest way to grab **moonraker-api** is through PyPI\n`pip3 install moonraker-api`\n\n## Use Moonraker-API\n\n### Connect and Disconnect\n\n```python\nclass APIConnector(MoonrakerListener):\n def __init__():\n self.running = False\n self.client = MoonrakerClient(\n self,\n HOST,\n PORT,\n API-KEY,\n )\n\n async def start(self) -> None:\n \"\"\"Start the websocket connection.\"\"\"\n self.running = True\n return await self.client.connect()\n\n async def stop(self) -> None:\n \"\"\"Stop the websocket connection.\"\"\"\n self.running = False\n await self.client.disconnect()\n```\n\n### Query the API\n\n```python\napi_connector = APIConnector()\nresponse = await api_connector.client.request(\"printer.info\")\n```\n\n### Handle Push Notifications\n\n```python\nclass APIConnector(MoonrakerListener):\n\n # Other class details, see above ...\n\n async def state_changed(self, state: str) -> None:\n \"\"\"Notifies of changing websocket state.\"\"\"\n _LOGGER.debug(\"Stated changed to %s\", state)\n if state == WEBSOCKET_STATE_CONNECTING:\n pass\n elif state == WEBSOCKET_STATE_CONNECTED:\n pass\n elif state == WEBSOCKET_STATE_READY:\n pass\n elif state == WEBSOCKET_STATE_PAUSED:\n pass\n elif state == WEBSOCKET_STATE_STOPPED:\n pass\n\n async def on_exception(self, exception: BaseException) -> None:\n \"\"\"Notifies of exceptions from the websocket run loop.\"\"\"\n _LOGGER.debug(\"Received exception from API websocket %s\", str(exception))\n if isinstance(exception, ClientNotAuthenticatedError):\n self.entry.async_start_reauth(self.hass)\n else:\n raise exception\n\n async def on_notification(self, method: str, data: Any) -> None:\n \"\"\"Notifies of state updates.\"\"\"\n _LOGGER.debug(\"Received notification %s -> %s\", method, data)\n\n # Subscription notifications\n if method == \"notify_status_update\":\n message = data[0]\n timestamp = data[1]\n \n # Do stuff ...\n```\n",
"bugtrack_url": null,
"license": null,
"summary": "Async websocket API client for Moonraker",
"version": "2.0.6",
"project_urls": {
"Homepage": "https://github.com/cmroche/moonraker-api"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4a200468b347b7f0e897184cbf08ab4e1b14da37e1f0d78e6ab1f527edc29415",
"md5": "d457b813d2da518a95b09173806f82d4",
"sha256": "f4d0e9300dd6d932045ea13e38bc783ac69831eb126686b91c9f0f1b3a71c1c3"
},
"downloads": -1,
"filename": "moonraker_api-2.0.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d457b813d2da518a95b09173806f82d4",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 22626,
"upload_time": "2024-08-11T21:32:29",
"upload_time_iso_8601": "2024-08-11T21:32:29.004281Z",
"url": "https://files.pythonhosted.org/packages/4a/20/0468b347b7f0e897184cbf08ab4e1b14da37e1f0d78e6ab1f527edc29415/moonraker_api-2.0.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a6cf63ca85b63d8572489ebf8deee2753baf56209407bc5db11de36370b9e7b4",
"md5": "523a05332f3d1ced413de0c04910677e",
"sha256": "b48cd50578d203cd97927cc45c8cb77ca9fbae0eab3b57c859efb6869fe4a6ac"
},
"downloads": -1,
"filename": "moonraker_api-2.0.6.tar.gz",
"has_sig": false,
"md5_digest": "523a05332f3d1ced413de0c04910677e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 21804,
"upload_time": "2024-08-11T21:32:30",
"upload_time_iso_8601": "2024-08-11T21:32:30.343061Z",
"url": "https://files.pythonhosted.org/packages/a6/cf/63ca85b63d8572489ebf8deee2753baf56209407bc5db11de36370b9e7b4/moonraker_api-2.0.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-11 21:32:30",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "cmroche",
"github_project": "moonraker-api",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "moonraker-api"
}