Skip to content

Commit

Permalink
Fixed progress events
Browse files Browse the repository at this point in the history
Progress events weren't getting triggered due to a None type issue. This is now fixed.
  • Loading branch information
2blane committed May 19, 2020
1 parent ae74456 commit cf1797c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
18 changes: 11 additions & 7 deletions octoprint_webhooks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,14 @@ def on_print_progress(self, storage, path, progress):
# Reset in case of multiple prints
if self.last_print_progress > progress:
self.last_print_progress = -1
self.last_print_progress_milestones = []
# Get the settings
hooks = self._settings.get(["hooks"])
for hook_index in range(0, len(hooks)):
hook = hooks[hook_index]
active = hook["event_print_progress"]
event_print_progress_interval = hook["event_print_progress_interval"]
#self._logger.info("Print Progress" + storage + " - " + path + " - {0}".format(progress))
#self._logger.info("Print Progress" + storage + " - " + path + " - {0}".format(progress) + " - hook_index:{0}".format(hook_index) + " - active:{0}".format(active))
if active:
try:
interval = int(event_print_progress_interval)
Expand All @@ -212,6 +213,7 @@ def on_print_progress(self, storage, path, progress):
self.last_print_progress_milestones[hook_index] = p
else:
self.last_print_progress_milestones.append(p)
#self._logger.info("Fire Print Progress Event {0}".format(p))
eventManager().fire(Events.PLUGIN_WEBHOOKS_PROGRESS)
# Update the last print progress
self.last_print_progress = progress
Expand Down Expand Up @@ -283,6 +285,8 @@ def on_event(self, event, payload):
# A.2) Get the metadata
job_info = self.get_job_information()

if payload is None:
payload = {}
hooks = self._settings.get(["hooks"])
for hook_index in range(0, len(hooks)):
hook = hooks[hook_index]
Expand All @@ -295,7 +299,6 @@ def on_event(self, event, payload):
# Display an error and tell the user to enable their webhook.
self._plugin_manager.send_plugin_message(self._identifier, dict(type="error", hide=False, msg="Your webhook is disabled. Check the ENABLED box to test this webhook."))
continue
self._logger.info("hook: " + str(hook))

# A.3) Get the last print complete milestone
percent_complete_milestone = 0
Expand Down Expand Up @@ -360,13 +363,13 @@ def on_event(self, event, payload):
oauth_headers = check_for_header(oauth_headers, "content-type", "application/json")
# self._logger.info("oauth headers: " + json.dumps(oauth_headers) + " - data: " + json.dumps(oauth_data))
# self._logger.info("oauth_http_method: " + oauth_http_method + " - oauth_content_type: " + oauth_content_type)
response = requests.request(oauth_http_method, oauth_url, json=oauth_data, headers=oauth_headers)
response = requests.request(oauth_http_method, oauth_url, json=oauth_data, headers=oauth_headers, timeout=30)
else:
# Make sure the Content-Type header is set to application/x-www-form-urlencoded
oauth_headers = check_for_header(oauth_headers, "content-type", "application/x-www-form-urlencoded")
# self._logger.info("oauth headers: " + json.dumps(oauth_headers) + " - data: " + json.dumps(oauth_data))
# self._logger.info("oauth_http_method: " + oauth_http_method + " - oauth_content_type: " + oauth_content_type)
response = requests.request(oauth_http_method, oauth_url, data=oauth_data, headers=oauth_headers)
response = requests.request(oauth_http_method, oauth_url, data=oauth_data, headers=oauth_headers, timeout=30)
# 1.3) Check to make sure we got a valid response code.
self._logger.info("OAuth Response: " + " - " + response.text)
code = response.status_code
Expand Down Expand Up @@ -459,7 +462,7 @@ def on_event(self, event, payload):
response = ""
if http_method == "GET":
# Note: we can't upload a file with GET.
response = requests.get(url, params=data, headers=headers)
response = requests.get(url, params=data, headers=headers, timeout=10)
else:
if try_to_upload_file:
# Delete the Content-Type header if provided so that requests can set it on its own
Expand All @@ -478,14 +481,15 @@ def on_event(self, event, payload):
files = {
uploading_file_name: ("snapshot.jpg", snap, "image/jpeg")
}
# No timeout when uploading file as this could take some time.
response = requests.request(http_method, url, files=files, data=data, headers=headers)
elif content_type == "JSON":
# Make sure the Content-Type header is set to application/json
headers = check_for_header(headers, "content-type", "application/json")
self._logger.info("headers: " + json.dumps(headers))
self._logger.info("data: " + json.dumps(data))
self._logger.info("http_method: " + http_method + " - content_type: " + content_type)
response = requests.request(http_method, url, json=data, headers=headers)
response = requests.request(http_method, url, json=data, headers=headers, timeout=30)
else:
# Make sure the Content-Type header is set to application/x-www-form-urlencoded
headers = check_for_header(headers, "content-type", "application/x-www-form-urlencoded")
Expand All @@ -494,7 +498,7 @@ def on_event(self, event, payload):
self._logger.info("headers: " + json.dumps(headers))
self._logger.info("data: " + json.dumps(data))
self._logger.info("http_method: " + http_method + " - content_type: " + content_type)
response = requests.request(http_method, url, data=data, headers=headers)
response = requests.request(http_method, url, data=data, headers=headers, timeout=30)
self._logger.info("Response: " + response.text)
# Try to parse the response if possible.
code = response.status_code
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
plugin_name = "OctoPrint-Webhooks"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "3.0.0"
plugin_version = "3.0.1"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down

0 comments on commit cf1797c

Please sign in to comment.