Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spoolman: add support for aditional headers #870

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -3141,6 +3141,9 @@ server: http://192.168.0.123:7912
sync_rate: 5
# The interval, in seconds, between sync requests with the
# Spoolman server. The default is 5.
additional_headers:
# Additional headers for spoolman connection. Usefull for connecting to for example CloudFlare Zero Trust.
# Dict "name=value" list. The default is empty.
```

#### Setting the active spool from Klipper
Expand Down
18 changes: 15 additions & 3 deletions moonraker/components/spoolman.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Integration with Spoolman
#
# Copyright (C) 2023 Daniel Hultgren <[email protected]>
# Copyright (C) 2024 Damian Łoboda <[email protected]>
#
# This file may be distributed under the terms of the GNU GPLv3 license.

Expand All @@ -10,6 +11,7 @@
import re
import contextlib
import tornado.websocket as tornado_ws
from tornado import httpclient as tornado_httpclient
from ..common import RequestType, HistoryFieldData
from ..utils import json_wrapper as jsonw
from typing import (
Expand Down Expand Up @@ -38,6 +40,9 @@
class SpoolManager:
def __init__(self, config: ConfigHelper):
self.server = config.get_server()
self.additional_headers: Dict[str, str] = config.getdict(
"additional_headers", default={}
)
self.eventloop = self.server.get_event_loop()
self._get_spoolman_urls(config)
self.sync_rate_seconds = config.getint("sync_rate", default=5, minval=1)
Expand Down Expand Up @@ -129,9 +134,13 @@ async def _connect_websocket(self) -> None:
logging.info(f"Connecting To Spoolman: {self.ws_url}")
log_connect = False
try:
self.spoolman_ws = await tornado_ws.websocket_connect(
ws_request = tornado_httpclient.HTTPRequest(
self.ws_url,
headers=self.additional_headers,
connect_timeout=5.,
)
self.spoolman_ws = await tornado_ws.websocket_connect(
ws_request,
ping_interval=20.,
ping_timeout=60.
)
Expand Down Expand Up @@ -216,7 +225,8 @@ async def _check_spool_deleted(self) -> None:
if self.spool_id is not None:
response = await self.http_client.get(
f"{self.spoolman_url}/v1/spool/{self.spool_id}",
connect_timeout=1., request_timeout=2.
connect_timeout=1., request_timeout=2.,
headers=self.additional_headers,
)
if response.status_code == 404:
logging.info(f"Spool ID {self.spool_id} not found, setting to None")
Expand Down Expand Up @@ -305,7 +315,8 @@ async def report_extrusion(self, eventtime: float) -> float:
response = await self.http_client.request(
method="PUT",
url=f"{self.spoolman_url}/v1/spool/{spool_id}/use",
body={"use_length": used_length}
body={"use_length": used_length},
headers=self.additional_headers,
)
if response.has_error():
if response.status_code == 404:
Expand Down Expand Up @@ -367,6 +378,7 @@ async def _proxy_spoolman_request(self, web_request: WebRequest):
method=method,
url=full_url,
body=body,
headers=self.additional_headers,
)
if not use_v2_response:
response.raise_for_status()
Expand Down
Loading