forked from omniedgeio/omniedge-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.h
114 lines (96 loc) · 2.42 KB
/
api.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#ifndef API_H
#define API_H
//#include "proxy.h"
#include <QObject>
#include <QDateTime>
#include <QtNetwork>
#include <QtWebSockets/QtWebSockets>
//enum class ResponseStatus {
// UnknownError,
// Unauthorized, // 401
// InternalServerError, // 500
// NotFound, // 404
// Success, // 200
//};
struct AuthSession {
QString uuid;
QDateTime expiredAt;
};
struct Token {
QString token;
};
struct ConnectInfo {
QString communityName;
QString secretKey;
QString virtualIP;
QString subnetMask;
QString host;
};
struct Profile {
QString uuid;
QString name;
QString email;
};
struct Device {
QString uuid;
QString name;
QString virtualIP;
bool operator()(const Device & a, const Device & b) const{
return a.virtualIP < b.virtualIP;
}
};
struct VirtualNetwork {
QString uuid;
QString name;
QString ipRange;
QList<Device> devices;
};
struct UserInfo {
QString name;
QString email;
QString username; // userID
};
enum class ResponseStatus {
UnknownError,
AccessDenied, // 400
Unauthorized, // 401
InternalFailure, // 500
InvalidToken, // 403
MalformedQueryString, // 404
ServiceUnavailable, // 503
Success, // 200
};
class API : public QObject
{
Q_OBJECT
public:
explicit API(QObject *parent = nullptr);
QString currentToken;
public slots:
void getAuthSession(); // 开始登入, 会重定向至网页
void registerDevice(); // 登入完成后,会自动 register device
void getRefreshToken();
void getUserInfo(); // 登入成功后,会自动 get user info
void getVirtualNetworks(); // 登入成功后,会自动 get virtual networks
void joinVirtualNetwork(QString virtualNetworkUUID);
signals:
void authSession(ResponseStatus status, AuthSession auth);
void token(Token token);
void deviceInfo();
void profile(Profile profile);
void virtualNetworks(QList<VirtualNetwork> virtualNetworks);
void connectInfo(ConnectInfo info);
void error(ResponseStatus status, QString errorString);
private:
QString baseURL;
QWebSocket authSessionWebSocket;
QString currentDeviceUUID;
Profile userProfile;
AuthSession userAuthSession;
ConnectInfo userConnectInfo;
QList<VirtualNetwork> userVirtualNetworks;
QNetworkAccessManager *networkManager;
ResponseStatus getResponseStatus(QNetworkReply*);
bool bOnlyConnectOneTime = false;
};
#endif // API_H