Skip to content

Commit

Permalink
Fix: make _register_callback non-static.
Browse files Browse the repository at this point in the history
  • Loading branch information
loociano committed Jun 6, 2020
1 parent 65a8bf9 commit 7245884
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/network/ApiClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_printer_status(self, on_finished: Callable,
on_error: callback if the request fails.
"""
reply = self._network_manager.get(self._create_empty_request('/inquiry'))
ApiClient._register_callback(reply, on_finished, on_error)
self._register_callback(reply, on_finished, on_error)

def increase_upload_speed(
self, on_finished: Callable, on_error: Callable) -> None:
Expand All @@ -50,7 +50,7 @@ def increase_upload_speed(
# Source: https://github.com/nokemono42/MP-Select-Mini-Web
reply = self._network_manager.get(
self._create_empty_request('/set?code=M563%20S4'))
ApiClient._register_callback(reply, on_finished, on_error)
self._register_callback(reply, on_finished, on_error)

def start_print(self, on_finished: Optional[Callable] = None,
on_error=None) -> None:
Expand All @@ -63,7 +63,7 @@ def start_print(self, on_finished: Optional[Callable] = None,
reply = self._network_manager.get(
self._create_empty_request('/set?cmd={P:M}'))
if on_finished is not None:
ApiClient._register_callback(reply, on_finished, on_error)
self._register_callback(reply, on_finished, on_error)

def resume_print(self, on_finished: Callable, on_error=None) -> None:
"""Tells the printer to resume a paused print.
Expand All @@ -75,7 +75,7 @@ def resume_print(self, on_finished: Callable, on_error=None) -> None:
"""
reply = self._network_manager.get(
self._create_empty_request('/set?cmd={P:R}'))
ApiClient._register_callback(reply, on_finished, on_error)
self._register_callback(reply, on_finished, on_error)

def pause_print(self, on_finished: Callable, on_error: Callable) -> None:
"""Tells the printer to pause the print.
Expand All @@ -87,7 +87,7 @@ def pause_print(self, on_finished: Callable, on_error: Callable) -> None:
"""
reply = self._network_manager.get(
self._create_empty_request('/set?cmd={P:P}'))
ApiClient._register_callback(reply, on_finished, on_error)
self._register_callback(reply, on_finished, on_error)

def cancel_print(self, on_finished: Optional[Callable] = None,
on_error=None) -> None:
Expand All @@ -101,7 +101,7 @@ def cancel_print(self, on_finished: Optional[Callable] = None,
reply = self._network_manager.get(
self._create_empty_request('/set?cmd={P:X}'))
if on_finished is not None:
ApiClient._register_callback(reply, on_finished, on_error)
self._register_callback(reply, on_finished, on_error)

def upload_print(self, filename: str, payload: bytes, on_finished: Callable,
on_progress: Callable, on_error: Callable) -> None:
Expand Down Expand Up @@ -132,7 +132,7 @@ def upload_print(self, filename: str, payload: bytes, on_finished: Callable,

reply = self._network_manager.post(request, http_multi_part)
# Upload is special: on_error is connected directly on reply.error
ApiClient._register_callback(reply, on_finished, None)
self._register_callback(reply, on_finished, None)
reply.uploadProgress.connect(on_progress)
reply.error.connect(on_error)
# Prevent HTTP multi-part to be garbage-collected.
Expand Down Expand Up @@ -163,7 +163,7 @@ def set_target_hotend_temperature(self,
return
reply = self._network_manager.get(
self._create_empty_request('/set?cmd={{C:T{:04d}}}'.format(celsius)))
ApiClient._register_callback(reply, on_finished, on_error)
self._register_callback(reply, on_finished, on_error)

def set_target_bed_temperature(self,
celsius: int,
Expand All @@ -182,7 +182,7 @@ def set_target_bed_temperature(self,
return
reply = self._network_manager.get(
self._create_empty_request('/set?cmd={{C:P{:03d}}}'.format(celsius)))
ApiClient._register_callback(reply, on_finished, on_error)
self._register_callback(reply, on_finished, on_error)

def _create_empty_request(self, path: str) -> QNetworkRequest:
""""Creates an empty HTTP request (GET or POST).
Expand All @@ -196,8 +196,7 @@ def _create_empty_request(self, path: str) -> QNetworkRequest:
request.setAttribute(QNetworkRequest.FollowRedirectsAttribute, True)
return request

@staticmethod
def _register_callback(reply: QNetworkReply, on_finished: Callable,
def _register_callback(self, reply: QNetworkReply, on_finished: Callable,
on_error: Optional[Callable]) -> None:
"""Adds a callback to an HTTP request.
Expand All @@ -216,7 +215,7 @@ def parse() -> None:
on_error()
return

on_finished(ApiClient._parse_reply(reply))
on_finished(self._parse_reply(reply))

reply.finished.connect(parse)

Expand Down

0 comments on commit 7245884

Please sign in to comment.