Browse Source

Merge pull request #11017 from Chocobo1/apictrl

Improvements to APIController class
adaptive-webui-19844
Mike Tzou 5 years ago committed by GitHub
parent
commit
7f1bff4eb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/base/bittorrent/tracker.cpp
  2. 4
      src/base/http/types.h
  3. 14
      src/webui/api/apicontroller.cpp
  4. 7
      src/webui/api/apicontroller.h
  5. 2
      src/webui/webapplication.h

2
src/base/bittorrent/tracker.cpp

@ -128,7 +128,7 @@ Http::Response Tracker::processRequest(const Http::Request &request, const Http: @@ -128,7 +128,7 @@ Http::Response Tracker::processRequest(const Http::Request &request, const Http:
void Tracker::respondToAnnounceRequest()
{
const QMap<QString, QByteArray> &queryParams = m_request.query;
const QHash<QString, QByteArray> &queryParams = m_request.query;
TrackerAnnounceRequest announceReq;
// IP

4
src/base/http/types.h

@ -98,8 +98,8 @@ namespace Http @@ -98,8 +98,8 @@ namespace Http
QString method;
QString path;
QStringMap headers;
QMap<QString, QByteArray> query;
QStringMap posts;
QHash<QString, QByteArray> query;
QHash<QString, QString> posts;
QVector<UploadedFile> files;
};

14
src/webui/api/apicontroller.cpp

@ -28,6 +28,8 @@ @@ -28,6 +28,8 @@
#include "apicontroller.h"
#include <algorithm>
#include <QJsonDocument>
#include <QMetaObject>
@ -45,8 +47,8 @@ QVariant APIController::run(const QString &action, const StringMap &params, cons @@ -45,8 +47,8 @@ QVariant APIController::run(const QString &action, const StringMap &params, cons
m_params = params;
m_data = data;
const QString methodName {action + QLatin1String("Action")};
if (!QMetaObject::invokeMethod(this, methodName.toLatin1().constData()))
const QByteArray methodName = action.toLatin1() + "Action";
if (!QMetaObject::invokeMethod(this, methodName.constData()))
throw APIError(APIErrorType::NotFound);
return m_result;
@ -69,9 +71,13 @@ const DataMap &APIController::data() const @@ -69,9 +71,13 @@ const DataMap &APIController::data() const
void APIController::checkParams(const QSet<QString> &requiredParams) const
{
const QSet<QString> params {this->params().keys().toSet()};
const bool hasAllRequiredParams = std::all_of(requiredParams.cbegin(), requiredParams.cend()
, [this](const QString &requiredParam)
{
return params().contains(requiredParam);
});
if (!params.contains(requiredParams))
if (!hasAllRequiredParams)
throw APIError(APIErrorType::BadParams);
}

7
src/webui/api/apicontroller.h

@ -28,7 +28,7 @@ @@ -28,7 +28,7 @@
#pragma once
#include <QMap>
#include <QHash>
#include <QObject>
#include <QSet>
#include <QVariant>
@ -36,8 +36,9 @@ @@ -36,8 +36,9 @@
class QString;
struct ISessionManager;
using StringMap = QMap<QString, QString>;
using DataMap = QMap<QString, QByteArray>;
using DataMap = QHash<QString, QByteArray>;
using StringMap = QHash<QString, QString>;
class APIController : public QObject
{

2
src/webui/webapplication.h

@ -123,7 +123,7 @@ private: @@ -123,7 +123,7 @@ private:
WebSession *m_currentSession = nullptr;
Http::Request m_request;
Http::Environment m_env;
QMap<QString, QString> m_params;
QHash<QString, QString> m_params;
const QString m_cacheID;
const QRegularExpression m_apiPathPattern {(QLatin1String("^/api/v2/(?<scope>[A-Za-z_][A-Za-z_0-9]*)/(?<action>[A-Za-z_][A-Za-z_0-9]*)$"))};

Loading…
Cancel
Save