swift
apiserverconnection.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2019 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #ifndef SWIFT_CORE_AFV_CONNECTION_APISERVERCONNECTION_H
7 #define SWIFT_CORE_AFV_CONNECTION_APISERVERCONNECTION_H
8 
9 #include <QEventLoop>
10 #include <QJsonDocument>
11 #include <QNetworkAccessManager>
12 #include <QNetworkReply>
13 #include <QNetworkRequest>
14 #include <QObject>
15 #include <QString>
16 #include <QUuid>
17 
18 #include "core/afv/dto.h"
19 #include "core/application.h"
20 #include "misc/logcategories.h"
21 #include "misc/logmessage.h"
22 #include "misc/slot.h"
23 
24 namespace swift::core::afv::connection
25 {
28 
30  class CApiServerConnection : public QObject
31  {
32  Q_OBJECT
33 
34  public:
37  {
38  NoError
39  };
40 
42  static const QStringList &getLogCategories();
43 
45  CApiServerConnection(const QString &address, QObject *parent = nullptr);
46 
48  bool isAuthenticated() const { return m_isAuthenticated; }
49 
52  void connectTo(const QString &username, const QString &password, const QString &client,
53  const QUuid &networkVersion, ConnectionCallback callback);
54 
56  PostCallsignResponseDto addCallsign(const QString &callsign);
57 
59  void removeCallsign(const QString &callsign);
60 
62  void updateTransceivers(const QString &callsign, const QVector<TransceiverDto> &transceivers);
63 
65  void forceDisconnect();
66 
68  QVector<StationDto> getAllAliasedStations();
69 
71  bool setUrl(const QString &url);
72 
74  const QString &getUrl() const { return m_addressUrl; }
75 
78  const QString &getUserName() const { return m_username; }
79  const QString &getPassword() const { return m_password; }
80  const QString &getClient() const { return m_client; }
81  const QUuid &getNetworkVersion() const { return m_networkVersion; }
83 
84  private:
86  template <typename TResponse>
87  TResponse postNoRequest(const QString &resource)
88  {
89  if (!this->sendToNetworkIfAuthenticated())
90  {
91  // swift::misc::CLogMessage(this).debug(u"AFV not authenticated");
92  return {};
93  }
94 
95  this->checkExpiry();
96 
97  QUrl url(m_addressUrl);
98  url.setPath(resource);
99  QNetworkRequest request(url);
100  request.setRawHeader("Authorization", "Bearer " + m_jwt);
101 
102  const QByteArray receivedData = this->postWithResponse(request);
103  const QJsonDocument doc = QJsonDocument::fromJson(receivedData);
104  const TResponse response = TResponse::fromJson(doc.object());
105  return response;
106  }
107 
109  template <typename TResponse>
110  QVector<TResponse> getAsVector(const QString &resource)
111  {
112  if (!this->sendToNetworkIfAuthenticated())
113  {
114  // swift::misc::CLogMessage(this).debug(u"AFV not authenticated");
115  return {};
116  }
117 
118  this->checkExpiry();
119 
120  QUrl url(m_addressUrl);
121  url.setPath(resource);
122  QNetworkRequest request(url);
123  request.setRawHeader("Authorization", "Bearer " + m_jwt);
124 
125  const QByteArray receivedData = this->getWithResponse(request);
126  const QJsonDocument jsonDoc = QJsonDocument::fromJson(receivedData);
127  QVector<TResponse> dtos;
128  if (jsonDoc.isArray())
129  {
130  const QJsonArray rootArray = jsonDoc.array();
131  for (const auto &o : rootArray)
132  {
133  const QJsonObject d = o.toObject();
134  const TResponse dto = TResponse::fromJson(d);
135  dtos.push_back(dto);
136  }
137  }
138  return dtos;
139  }
140 
142  QByteArray getWithResponse(const QNetworkRequest &request);
143 
145  QByteArray postWithResponse(const QNetworkRequest &request, const QByteArray &data = {});
146 
148  void postNoResponse(const QString &resource, const QJsonDocument &json);
149 
151  void deleteResource(const QString &resource);
152 
154  void checkExpiry();
155 
157  void logReplyErrorMessage(const QNetworkReply *reply, const QString &addMsg = {});
158 
160  void logRequestDuration(const QNetworkReply *reply, const QString &addMsg = {});
161 
163  QEventLoop *newEventLoop();
164 
166  bool sendToNetworkIfAuthenticated() const;
167 
169  static bool isShuttingDown();
170 
171  QString m_addressUrl;
172  QByteArray m_jwt;
173  QString m_username;
174  QString m_password;
175  QUuid m_networkVersion;
176  QString m_client;
177  QDateTime m_expiryLocalUtc;
178  qint64 m_serverToUserOffsetMs;
179  bool m_isAuthenticated = false;
180  };
181 } // namespace swift::core::afv::connection
182 
183 #endif // SWIFT_CORE_AFV_CONNECTION_APISERVERCONNECTION_H
static const QStringList & getLogCategories()
Categories.
void removeCallsign(const QString &callsign)
Remove callsign from network.
PostCallsignResponseDto addCallsign(const QString &callsign)
Add callsign to network.
void forceDisconnect()
Force disconnect from network.
QVector< StationDto > getAllAliasedStations()
All aliased stations.
void connectTo(const QString &username, const QString &password, const QString &client, const QUuid &networkVersion, ConnectionCallback callback)
Connect to network.
void updateTransceivers(const QString &callsign, const QVector< TransceiverDto > &transceivers)
Update transceivers.
CApiServerConnection(const QString &address, QObject *parent=nullptr)
Constructor.
Callable wrapper for a member function with function signature F.
Definition: slot.h:62
Callsign DTO.
Definition: dto.h:87