def _finishedCallback(self, reply: QNetworkReply) -> None: Logger.log("i", "Finished callback %s %s", reply.attribute(QNetworkRequest.HttpStatusCodeAttribute), reply.url().toString()) status_code = reply.attribute( QNetworkRequest.HttpStatusCodeAttribute) # type: int # check if we should retry the last chunk if self._retries < self.MAX_RETRIES and status_code in self.RETRY_HTTP_CODES: self._retries += 1 Logger.log("i", "Retrying %s/%s request %s", self._retries, self.MAX_RETRIES, reply.url().toString()) try: self._uploadChunk() except ValueError: # Asynchronously it could have completed in the meanwhile. pass return # Http codes that are not to be retried are assumed to be errors. if status_code > 308: self._errorCallback(reply, None) return Logger.log( "d", "status_code: %s, Headers: %s, body: %s", status_code, [bytes(header).decode() for header in reply.rawHeaderList()], bytes(reply.readAll()).decode()) self._chunkUploaded()
def _onUploadFinished(self, reply: QNetworkReply) -> None: """ Checks whether a chunk of data was uploaded successfully, starting the next chunk if needed. """ Logger.log("i", "Finished callback %s %s", reply.attribute(QNetworkRequest.HttpStatusCodeAttribute), reply.url().toString()) status_code = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) # type: Optional[int] if not status_code: Logger.log("e", "Reply contained no status code.") self._onUploadError(reply, None) return # check if we should retry the last chunk if self._retries < self.MAX_RETRIES and status_code in self.RETRY_HTTP_CODES: self._retries += 1 Logger.log("i", "Retrying %s/%s request %s", self._retries, self.MAX_RETRIES, reply.url().toString()) try: self._upload() except ValueError: # Asynchronously it could have completed in the meanwhile. pass return # Http codes that are not to be retried are assumed to be errors. if status_code > 308: self._onUploadError(reply, None) return Logger.log("d", "status_code: %s, Headers: %s, body: %s", status_code, [bytes(header).decode() for header in reply.rawHeaderList()], bytes(reply.readAll()).decode()) self._on_success(self._file_name) self.stop()
def gotResponse(self, QNetworkReply): try: status = QNetworkReply.attribute( QNetworkRequest.HttpStatusCodeAttribute) except Exception as e: log('[e] http/yaml error: %s' % str(e)) self.updatesLabel.setText('Error') return if status != 200: self.updatesLabel.setText('Network error: ' + str(status)) try: for h in (QNetworkReply.rawHeaderList()): log('[w] %s: %s' % (str( h, 'utf-8'), str(QNetworkReply.rawHeader(h), 'utf-8'))) except Exception as e: log('[e]: %s' % str(e)) return try: response = QNetworkReply.readAll() response = str(response, 'utf-8') ver = safe_load(response) except Exception as e: log('[e] http/yaml error: %s' % str(e)) self.updatesLabel.setText('error') return if ver is None: self.updatesLabel.setText('<network error>') return if 'version' in ver and 'date' in ver: verStr = 'Last published version is %s, build %s.' % ( ver['version'], ver['date']) self.updatesLabel.setText(verStr)