Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions App/Server/RESTServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ void RESTServer::start(fnIsInBlackList_t _fnIPInBlackList)
return RequestHandler->sendFile(ServerConfigs::PublicPath.value(), Path);

if (Path.startsWith(BasePath) == false)
return RequestHandler->sendError(qhttp::ESTATUS_NOT_FOUND, "Path not found: '" + Path + "'", true);
return RequestHandler->sendError(qhttp::ESTATUS_NOT_FOUND, "Path not found: '" + Path + "'", {}, true);

if (Path.startsWith(ServerConfigs::BasePathWithVersion) == false)
return RequestHandler->sendError(qhttp::ESTATUS_NOT_ACCEPTABLE, "Invalid Version or version not specified", true);
return RequestHandler->sendError(qhttp::ESTATUS_NOT_ACCEPTABLE, "Invalid Version or version not specified", {}, true);

if (Path == ServerConfigs::BasePathWithVersion )
return RequestHandler->sendError(qhttp::ESTATUS_NOT_ACCEPTABLE, "No API call provided", true);
return RequestHandler->sendError(qhttp::ESTATUS_NOT_ACCEPTABLE, "No API call provided", {}, true);

TargomanLogInfo(7,
"New API Call ["<<
Expand Down Expand Up @@ -147,7 +147,7 @@ void RESTServer::start(fnIsInBlackList_t _fnIPInBlackList)
}
catch(exTargomanBase& ex)
{
RequestHandler->sendError(static_cast<qhttp::TStatusCode>(ex.httpCode()), ex.what(), ex.httpCode() >= 500);
RequestHandler->sendError(static_cast<qhttp::TStatusCode>(ex.httpCode()), ex.what(), {}, ex.httpCode() >= 500);
}
});

Expand Down
69 changes: 50 additions & 19 deletions App/Server/clsAPIObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,17 @@ intfAPIArgManipulator* clsAPIObject::argSpecs(quint8 _paramIndex) const {
return gUserDefinedTypesInfo.at(this->BaseMethod.parameterType(_paramIndex) - TAPI_BASE_USER_DEFINED_TYPEID);
}

QVariant clsAPIObject::invoke(bool _isUpdateMethod,
const QStringList& _args,
QList<QPair<QString, QString>> _bodyArgs,
qhttp::THeaderHash _headers,
qhttp::THeaderHash _cookies,
QJsonObject _jwt,
QString _remoteIP,
QString _extraAPIPath) const
QVariant clsAPIObject::invoke(
bool _isUpdateMethod,
const QStringList& _args,
/*OUT*/ QVariantMap &_responseHeaders,
QList<QPair<QString, QString>> _bodyArgs,
qhttp::THeaderHash _headers,
qhttp::THeaderHash _cookies,
QJsonObject _jwt,
QString _remoteIP,
QString _extraAPIPath
) const
{
Q_ASSERT_X(this->parent(), "parent module", "Parent module not found to invoke method");

Expand Down Expand Up @@ -212,50 +215,70 @@ QVariant clsAPIObject::invoke(bool _isUpdateMethod,
else if (LastArgumentWithValue < Arguments.size() - 1)
Arguments = Arguments.mid(0, LastArgumentWithValue + 1);

QVariant Result;
QString CacheKey;
QString CacheKey;
if (this->Cache4Secs != 0 || this->Cache4SecsCentral != 0)
CacheKey = this->makeCacheKey(Arguments);

if (this->Cache4Secs != 0) {
QVariant CachedValue = InternalCache::storedValue(CacheKey);
if (CachedValue.isValid()) {
gServerStats.APIInternalCacheStats[this->BaseMethod.name()].inc();
return CachedValue;

QVariantMap Map = CachedValue.toMap();
_responseHeaders = Map.value("headers").toMap();
return Map.value("result");
}
}

if(this->Cache4SecsCentral){
if (this->Cache4SecsCentral) {
QVariant CachedValue = CentralCache::storedValue(CacheKey);
if(CachedValue.isValid()){
gServerStats.APICentralCacheStats[this->BaseMethod.name()].inc();
return CachedValue;

QVariantMap Map = CachedValue.toMap();
_responseHeaders = Map.value("headers").toMap();
return Map.value("result");
}
}

intfAPIArgManipulator *APIMethod;

if (this->BaseMethod.returnType() >= TAPI_BASE_USER_DEFINED_TYPEID) {
Q_ASSERT(this->BaseMethod.returnType() - TAPI_BASE_USER_DEFINED_TYPEID < gUserDefinedTypesInfo.size());
Q_ASSERT(gUserDefinedTypesInfo.at(this->BaseMethod.returnType() - TAPI_BASE_USER_DEFINED_TYPEID) != nullptr);

Result = gUserDefinedTypesInfo.at(this->BaseMethod.returnType() - TAPI_BASE_USER_DEFINED_TYPEID)->invokeMethod(this, Arguments);
APIMethod = gUserDefinedTypesInfo.at(this->BaseMethod.returnType() - TAPI_BASE_USER_DEFINED_TYPEID);
}
else {
Q_ASSERT(this->BaseMethod.returnType() < gOrderedMetaTypeInfo.size());
Q_ASSERT(gOrderedMetaTypeInfo.at(this->BaseMethod.returnType()) != nullptr);

Result = gOrderedMetaTypeInfo.at(this->BaseMethod.returnType())->invokeMethod(this, Arguments);
APIMethod = gOrderedMetaTypeInfo.at(this->BaseMethod.returnType());
}

QVariant Result = APIMethod->invokeMethod(
this,
Arguments,
_responseHeaders);

QVariantMap ResultWithHeader = QVariantMap({
{ "result", Result },
{ "headers", _responseHeaders },
});
if (this->Cache4Secs != 0)
InternalCache::setValue(CacheKey, Result, this->Cache4Secs);
InternalCache::setValue(CacheKey, ResultWithHeader, this->Cache4Secs);
else if (this->Cache4SecsCentral != 0)
CentralCache::setValue(CacheKey, Result, this->Cache4SecsCentral);
CentralCache::setValue(CacheKey, ResultWithHeader, this->Cache4SecsCentral);

gServerStats.APICallsStats[this->BaseMethod.name()].inc();
return Result;
}

void clsAPIObject::invokeMethod(const QVariantList& _arguments, QGenericReturnArgument _returnArg) const
void clsAPIObject::invokeMethod(
const QVariantList& _arguments,
QGenericReturnArgument _returnArg,
/*OUT*/ QVariantMap &_responseHeaders
) const
{
bool InvocationResult = true;
QMetaMethod InvokableMethod;
Expand Down Expand Up @@ -289,8 +312,16 @@ void clsAPIObject::invokeMethod(const QVariantList& _arguments, QGenericReturnAr
Arguments[i] = QGenericArgument();
}

QObject *parent = this->parent();
intfPureModule* pureModule = dynamic_cast<intfPureModule*>(parent);
QObject::connect(pureModule, &intfPureModule::addResponseHeader,
[&_responseHeaders](const QString &_header, const QString &_value) {
_responseHeaders.insert(_header, _value);
}
);

InvocationResult = InvokableMethod.invoke(
this->parent(),
parent,
this->IsAsync ? Qt::QueuedConnection : Qt::DirectConnection,
_returnArg,
Arguments[0],
Expand Down
37 changes: 20 additions & 17 deletions App/Server/clsAPIObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
******************************************************************************/
/**
* @author S.Mehran M.Ziabary <ziabary@targoman.com>
* @author Kambiz Zandi <kambizzandi@gmail.com>
*/

#ifndef TARGOMAN_API_SERVER_CLSAPIOBJECT_HPP
Expand All @@ -28,9 +29,7 @@

#include "Interfaces/API/intfPureModule.h"

namespace Targoman {
namespace API {
namespace Server {
namespace Targoman::API::Server {

#define PARAM_JWT "TAPI::JWT_t"
#define PARAM_COOKIES "TAPI::COOKIES_t"
Expand Down Expand Up @@ -110,17 +109,22 @@ class clsAPIObject : public intfAPIObject, public QObject
return this->BaseMethod.DefaultValues.at(_paramIndex);
}

QVariant invoke(bool _isUpdateMethod,
const QStringList& _args,
QList<QPair<QString, QString>> _bodyArgs = {},
qhttp::THeaderHash _headers = {},
qhttp::THeaderHash _cookies = {},
QJsonObject _jwt = {},
QString _remoteIP = {},
QString _extraAPIPath = {}
) const;

void invokeMethod(const QVariantList& _arguments, QGenericReturnArgument _returnArg) const;
QVariant invoke(
bool _isUpdateMethod,
const QStringList& _args,
/*OUT*/ QVariantMap &_responseHeaders,
QList<QPair<QString, QString>> _bodyArgs = {},
qhttp::THeaderHash _headers = {},
qhttp::THeaderHash _cookies = {},
QJsonObject _jwt = {},
QString _remoteIP = {},
QString _extraAPIPath = {}
) const;

void invokeMethod(
const QVariantList& _arguments,
QGenericReturnArgument _returnArg,
/*OUT*/ QVariantMap &_responseHeaders) const;
bool isPolymorphic(const QMetaMethodExtended& _method);

private:
Expand All @@ -143,7 +147,6 @@ class clsAPIObject : public intfAPIObject, public QObject
friend class OpenAPIGenerator;
};

}
}
}
} //namespace Targoman::API::Server

#endif // TARGOMAN_API_SERVER_CLSAPIOBJECT_HPP
Loading