mirror of
https://gitee.com/fasiondog/hikyuu.git
synced 2024-12-04 04:48:17 +08:00
封装session对restful api方法;实时刷新qtreewidget界面
This commit is contained in:
parent
6f9d144622
commit
2c772eed6a
@ -1,6 +1,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from .restful import login
|
||||||
from .assist import getServerStatus
|
from .assist import getServerStatus
|
||||||
from .user import login
|
|
||||||
|
|
||||||
__all__ = ['login', 'getServerStatus']
|
__all__ = ['login', 'getServerStatus']
|
||||||
|
@ -2,24 +2,19 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from .config import getServerApiUrl, defaultRequestHeader
|
from .restful import get, session_get
|
||||||
from .restful import get
|
|
||||||
from .user import update_token
|
|
||||||
|
|
||||||
from data import SessionModel
|
from data import SessionModel
|
||||||
from translate import _translate
|
from translate import _translate
|
||||||
|
|
||||||
|
|
||||||
def getServerStatus(session: SessionModel):
|
def getServerStatus(session: SessionModel):
|
||||||
url = getServerApiUrl(session.host, session.port, "assist", "status")
|
|
||||||
headers = defaultRequestHeader()
|
|
||||||
headers["hku_token"] = session.token
|
|
||||||
try:
|
try:
|
||||||
r = get(url, headers=headers)
|
r = session_get(session, "assist", "status")
|
||||||
update_token(r, session)
|
|
||||||
if r["result"]:
|
if r["result"]:
|
||||||
return "running", _translate("ServerApi", "running")
|
return "running", _translate("ServerApi", "running")
|
||||||
else:
|
else:
|
||||||
return "stop", _translate("ServerApi", "failed! {}").format(r["errmsg"])
|
return "stop", _translate("ServerApi", "failed! {}").format(r["errmsg"])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
logging.info("{}: {}".format(e.__class__.__name__, e))
|
||||||
return "stop", _translate("ServerApi", "failed connect!")
|
return "stop", _translate("ServerApi", "failed connect!")
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
import requests
|
import requests
|
||||||
import functools
|
import functools
|
||||||
|
from .config import getServerApiUrl, defaultRequestHeader
|
||||||
|
from data import SessionModel
|
||||||
|
|
||||||
|
|
||||||
class HttpStatusError(Exception):
|
class HttpStatusError(Exception):
|
||||||
@ -63,3 +65,38 @@ def patch(url, data=None, **kwargs):
|
|||||||
@wrap_restful
|
@wrap_restful
|
||||||
def put(url, data=None, **kwargs):
|
def put(url, data=None, **kwargs):
|
||||||
return requests.put(url, data, **kwargs)
|
return requests.put(url, data, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class RestErrorCode:
|
||||||
|
INVALID_ENUM_VALUE = 10000
|
||||||
|
MISS_TOKEN = 10001
|
||||||
|
UNAUTHORIZED = 10002
|
||||||
|
AUTHORIZE_EXPIRED = 10003
|
||||||
|
|
||||||
|
|
||||||
|
def login(session: SessionModel):
|
||||||
|
url = getServerApiUrl(session.host, session.port, "user", "login")
|
||||||
|
headers = defaultRequestHeader()
|
||||||
|
res = post(url, headers=headers, json={"user": session.user, "password": session.password})
|
||||||
|
session.token = res["token"]
|
||||||
|
session.userid = res["userid"]
|
||||||
|
return session
|
||||||
|
|
||||||
|
|
||||||
|
def session_get(session: SessionModel, service, api, params=None, **kwargs):
|
||||||
|
def inner_get(session: SessionModel, service, api, params, **kwargs):
|
||||||
|
url = getServerApiUrl(session.host, session.port, service, api)
|
||||||
|
headers = defaultRequestHeader()
|
||||||
|
headers["hku_token"] = session.token
|
||||||
|
r = get(url, headers=headers, params=params, **kwargs)
|
||||||
|
if "update_token" in r:
|
||||||
|
session.token = r["update_token"]
|
||||||
|
return r
|
||||||
|
if not session.token:
|
||||||
|
session = login(session)
|
||||||
|
res = inner_get(session, service, api, params, **kwargs)
|
||||||
|
if not res["result"] and res['errcode'] == RestErrorCode.AUTHORIZE_EXPIRED:
|
||||||
|
session = login(session)
|
||||||
|
res = inner_get(session, service, api, params, **kwargs)
|
||||||
|
return res
|
||||||
|
|
||||||
|
@ -7,20 +7,12 @@ from .restful import post
|
|||||||
from data import SessionModel
|
from data import SessionModel
|
||||||
|
|
||||||
|
|
||||||
def login(session: SessionModel):
|
# def login(session: SessionModel):
|
||||||
url = getServerApiUrl(session.host, session.port, "user", "login")
|
# url = getServerApiUrl(session.host, session.port, "user", "login")
|
||||||
headers = defaultRequestHeader()
|
# headers = defaultRequestHeader()
|
||||||
return post(url, headers=headers, json={"user": session.user, "password": session.password})
|
# return post(url, headers=headers, json={"user": session.user, "password": session.password})
|
||||||
|
|
||||||
|
|
||||||
def update_token(res, session: SessionModel):
|
|
||||||
""" 如果响应中包含 "update_token, 则更新相应的session中的token
|
|
||||||
|
|
||||||
:param SessionModel session: 会话
|
|
||||||
:param res: 已经加码json的请求响应数据
|
|
||||||
"""
|
|
||||||
if "update_token" in res:
|
|
||||||
session.token = res["update_token"]
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from sqlalchemy import Column, Integer, String
|
from sqlalchemy import Column, Integer, String, BigInteger
|
||||||
from .LocalDatabase import get_local_db
|
from .LocalDatabase import get_local_db
|
||||||
|
|
||||||
db = get_local_db()
|
db = get_local_db()
|
||||||
@ -17,6 +17,7 @@ class SessionModel(db.model):
|
|||||||
password = Column(String(64))
|
password = Column(String(64))
|
||||||
remark = Column(String(1024))
|
remark = Column(String(1024))
|
||||||
token = Column(String(256))
|
token = Column(String(256))
|
||||||
|
userid = Column(BigInteger)
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.name = ''
|
self.name = ''
|
||||||
@ -25,6 +26,7 @@ class SessionModel(db.model):
|
|||||||
self.user = ''
|
self.user = ''
|
||||||
self.password = ''
|
self.password = ''
|
||||||
self.remark = ''
|
self.remark = ''
|
||||||
|
self.userid = None
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'SessionModel(id={}, name="{}", host="{}", port={}, user="{}", password="******")'.format(
|
return 'SessionModel(id={}, name="{}", host="{}", port={}, user="{}", password="******")'.format(
|
||||||
|
@ -6,7 +6,7 @@ import ServerApi
|
|||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
from PyQt5 import QtWidgets, QtCore, QtGui
|
from PyQt5 import QtWidgets, QtCore, QtGui
|
||||||
from .Ui_HkuEditSessionDialog import Ui_HkuEditSessionDialog
|
from .Ui_HkuEditSessionDialog import Ui_HkuEditSessionDialog
|
||||||
from .HkuWaitingDialog import HkuWaitingDialog
|
from data import SessionModel
|
||||||
|
|
||||||
_translate = QtCore.QCoreApplication.translate
|
_translate = QtCore.QCoreApplication.translate
|
||||||
|
|
||||||
@ -52,6 +52,8 @@ class HkuEditSessionDialog(QtWidgets.QDialog, Ui_HkuEditSessionDialog):
|
|||||||
self.remark_textEdit.setText(session_model.remark)
|
self.remark_textEdit.setText(session_model.remark)
|
||||||
|
|
||||||
def getData(self):
|
def getData(self):
|
||||||
|
if self.session_model is None:
|
||||||
|
self.session_model = SessionModel()
|
||||||
self.session_model.name = self.name
|
self.session_model.name = self.name
|
||||||
self.session_model.host = self.host
|
self.session_model.host = self.host
|
||||||
self.session_model.port = self.port
|
self.session_model.port = self.port
|
||||||
@ -98,14 +100,11 @@ class HkuEditSessionDialog(QtWidgets.QDialog, Ui_HkuEditSessionDialog):
|
|||||||
@QtCore.pyqtSlot()
|
@QtCore.pyqtSlot()
|
||||||
def on_test_pushButton_clicked(self):
|
def on_test_pushButton_clicked(self):
|
||||||
try:
|
try:
|
||||||
r = ServerApi.login(self.getData())
|
ServerApi.login(self.getData())
|
||||||
if r["result"]:
|
QtWidgets.QMessageBox.about(
|
||||||
QtWidgets.QMessageBox.about(
|
self, _translate("HkuEditSessionDialog", "success"),
|
||||||
self, _translate("HkuEditSessionDialog", "success"),
|
_translate("HkuEditSessionDialog", "Connect successfully!")
|
||||||
_translate("HkuEditSessionDialog", "Connect successfully!")
|
)
|
||||||
)
|
|
||||||
else:
|
|
||||||
QtWidgets.QMessageBox.about(self, _translate("HkuEditSessionDialog", "Failed"), ret["errmsg"])
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
QtWidgets.QMessageBox.about(
|
QtWidgets.QMessageBox.about(
|
||||||
self, _translate("HkuEditSessionDialog", "Failed"),
|
self, _translate("HkuEditSessionDialog", "Failed"),
|
||||||
|
Binary file not shown.
@ -77,7 +77,7 @@
|
|||||||
<location filename="../dialog/HkuEditSessionDialog.py" line="110"/>
|
<location filename="../dialog/HkuEditSessionDialog.py" line="110"/>
|
||||||
<source>Failed connect! Please check the host/ip and port
|
<source>Failed connect! Please check the host/ip and port
|
||||||
%s</source>
|
%s</source>
|
||||||
<translation>连接失败!请检查主机/IP地址或端口号\n %s</translation>
|
<translation>连接失败!请检查主机/IP地址或端口号 %s</translation>
|
||||||
</message>
|
</message>
|
||||||
</context>
|
</context>
|
||||||
<context>
|
<context>
|
||||||
|
@ -3,17 +3,15 @@
|
|||||||
import resource
|
import resource
|
||||||
import ServerApi
|
import ServerApi
|
||||||
|
|
||||||
from PyQt5 import QtCore, QtGui
|
from PyQt5 import QtCore, QtGui, QtWidgets
|
||||||
|
|
||||||
|
|
||||||
class HkuCheckServerStatusThread(QtCore.QThread):
|
class HkuCheckServerStatusThread(QtCore.QThread):
|
||||||
"""定时检测服务器状态"""
|
|
||||||
status_changed = QtCore.pyqtSignal(list)
|
|
||||||
|
|
||||||
def __init__(self, session_widget):
|
def __init__(self, session_widget):
|
||||||
super(HkuCheckServerStatusThread, self).__init__()
|
super(HkuCheckServerStatusThread, self).__init__()
|
||||||
self.session_widget = session_widget
|
self.session_widget = session_widget
|
||||||
self.working = True
|
self.working = True
|
||||||
|
self.first = True
|
||||||
self.icons = {
|
self.icons = {
|
||||||
"running": QtGui.QIcon(":/icon/circular_green.png"),
|
"running": QtGui.QIcon(":/icon/circular_green.png"),
|
||||||
"stop": QtGui.QIcon(":/icon/circular_yellow.png")
|
"stop": QtGui.QIcon(":/icon/circular_yellow.png")
|
||||||
@ -26,7 +24,10 @@ class HkuCheckServerStatusThread(QtCore.QThread):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
pass
|
pass
|
||||||
self.sleep(5)
|
if self.first:
|
||||||
|
self.sleep(1)
|
||||||
|
else:
|
||||||
|
self.sleep(30)
|
||||||
|
|
||||||
def _run(self):
|
def _run(self):
|
||||||
items = [self.session_widget.tree.topLevelItem(i) for i in range(self.session_widget.tree.topLevelItemCount())]
|
items = [self.session_widget.tree.topLevelItem(i) for i in range(self.session_widget.tree.topLevelItemCount())]
|
||||||
@ -35,3 +36,7 @@ class HkuCheckServerStatusThread(QtCore.QThread):
|
|||||||
status, msg = ServerApi.getServerStatus(session)
|
status, msg = ServerApi.getServerStatus(session)
|
||||||
item.setText(1, msg)
|
item.setText(1, msg)
|
||||||
item.setIcon(1, self.icons[status])
|
item.setIcon(1, self.icons[status])
|
||||||
|
if items:
|
||||||
|
# 刷新 treewidget 显示界面
|
||||||
|
self.session_widget.tree.viewport().update()
|
||||||
|
self.first = False
|
||||||
|
@ -8,6 +8,8 @@ _translate = QtCore.QCoreApplication.translate
|
|||||||
|
|
||||||
|
|
||||||
class HkuSessionViewWidget(QtWidgets.QDockWidget):
|
class HkuSessionViewWidget(QtWidgets.QDockWidget):
|
||||||
|
status_changed = QtCore.pyqtSignal()
|
||||||
|
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
super(HkuSessionViewWidget, self).__init__(parent)
|
super(HkuSessionViewWidget, self).__init__(parent)
|
||||||
self.setObjectName("HKUServerViewWidget")
|
self.setObjectName("HKUServerViewWidget")
|
||||||
|
Loading…
Reference in New Issue
Block a user