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