Skip to content

Commit

Permalink
Merge pull request #14 from kuroi-usagi/PUT_for_custom_Shutdown
Browse files Browse the repository at this point in the history
Added PUT to api_custom method
  • Loading branch information
devildant authored Jul 29, 2019
2 parents e055fc9 + c891e93 commit 4861648
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
18 changes: 18 additions & 0 deletions octoprint_shutdownprinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(self):
self._mode_shutdown_api_custom = False
self.api_custom_GET = False
self.api_custom_POST = False
self.api_custom_PUT = False
self.api_custom_url = ""
self.api_custom_json_header = ""
self.api_custom_body = ""
Expand Down Expand Up @@ -74,6 +75,9 @@ def initialize(self):

self.api_custom_GET = self._settings.get_boolean(["api_custom_GET"])
self._logger.debug("api_custom_GET: %s" % self.api_custom_GET)

self.api_custom_PUT = self._settings.get_boolean(["api_custom_PUT"])
self._logger.debug("api_custom_PUT: %s" % self.api_custom_PUT)

self.api_custom_url = self._settings.get(["api_custom_url"])
self._logger.debug("api_custom_url: %s" % self.api_custom_url)
Expand Down Expand Up @@ -307,6 +311,18 @@ def _shutdown_printer_by_API_custom(self):
headers = {}
if self.api_custom_json_header != "":
headers = eval(self.api_custom_json_header)
if self.api_custom_PUT == True:
data = self.api_custom_body
self._logger.info("Shutting down printer with API custom (PUT)")
try:
request = urllib2.Request(self.api_custom_url, data=data, headers=headers)
request.get_method = lambda: "PUT"
contents = urllib2.urlopen(request, timeout=30, context=self.ctx).read()
self._logger.debug("call response (PUT): %s" % contents)
self._extraCommand()
except Exception as e:
self._logger.error("Failed to connect to call api: %s" % e.message)
return
if self.api_custom_POST == True:
data = self.api_custom_body
self._logger.info("Shutting down printer with API custom (POST)")
Expand Down Expand Up @@ -347,6 +363,7 @@ def get_settings_defaults(self):
_mode_shutdown_api_custom = False,
api_custom_POST = False,
api_custom_GET = False,
api_custom_PUT = False,
api_custom_url = "",
api_custom_json_header = "",
api_custom_body = "",
Expand All @@ -370,6 +387,7 @@ def on_settings_save(self, data):
self._mode_shutdown_api_custom = self._settings.get_boolean(["_mode_shutdown_api_custom"])
self.api_custom_POST = self._settings.get_boolean(["api_custom_POST"])
self.api_custom_GET = self._settings.get_boolean(["api_custom_GET"])
self.api_custom_PUT = self._settings.get_boolean(["api_custom_PUT"])
self.api_custom_url = self._settings.get(["api_custom_url"])
self.api_custom_json_header = self._settings.get(["api_custom_json_header"])
self.api_custom_body = self._settings.get(["api_custom_body"])
Expand Down
6 changes: 4 additions & 2 deletions octoprint_shutdownprinter/static/js/shutdownprinter.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,16 @@ $(function() {
]
self.listOffHTTPMethode = [
{"id" : "#shutdownprinter_api_custom_GET"},
{"id" : "#shutdownprinter_api_custom_POST"}
{"id" : "#shutdownprinter_api_custom_POST"}
{"id" : "#shutdownprinter_api_custom_PUT"}
]
self.eventChangeCheckToRadio("#shutdownprinter_mode_shutdown_gcode", self.listOffMode);
self.eventChangeCheckToRadio("#shutdownprinter_mode_shutdown_api", self.listOffMode);
self.eventChangeCheckToRadio("#shutdownprinter_mode_shutdown_api_custom", self.listOffMode);

self.eventChangeCheckToRadio("#shutdownprinter_api_custom_GET", self.listOffHTTPMethode);
self.eventChangeCheckToRadio("#shutdownprinter_api_custom_POST", self.listOffHTTPMethode);
self.eventChangeCheckToRadio("#shutdownprinter_api_custom_POST", self.listOffHTTPMethode);
self.eventChangeCheckToRadio("#shutdownprinter_api_custom_PUT", self.listOffHTTPMethode);

// Hack to remove automatically added Cancel button
// See https://github.com/sciactive/pnotify/issues/141
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
<label class="checkbox">
<input type="checkbox" id="shutdownprinter_api_custom_POST" data-bind="checked: settings.plugins.shutdownprinter.api_custom_POST">POST
</label>
<label class="checkbox">
<input type="checkbox" id="shutdownprinter_api_custom_PUT" data-bind="checked: settings.plugins.shutdownprinter.api_custom_PUT">PUT
</label>
</div>
</div>
<div class="control-group">
Expand Down

0 comments on commit 4861648

Please sign in to comment.