Пример #1
0
void ScriptService::getImpl(const QByteArray& operation, const APIParameters &parameters, APIServiceResponse &response)
{
	if(operation=="list")
	{
		//list all scripts, this should be thread safe
		QStringList allScripts = scriptMgr->getScriptList();

		response.writeJSON(QJsonDocument(QJsonArray::fromStringList(allScripts)));
	}
	else if (operation == "info")
	{
		if(parameters.contains("id"))
		{
			//retrieve detail about a single script
			QString scriptId = QString::fromUtf8(parameters.value("id"));

			if(parameters.contains("html"))
			{
				QString html = scriptMgr->getHtmlDescription(scriptId, false);
				response.setHeader("Content-Type","text/html; charset=UTF-8");
				response.setData(wrapHtml(html, scriptId).toUtf8());
				return;
			}

			QJsonObject obj;
			//if the script name is wrong, this will return empty strings
			obj.insert("id",scriptId);
			QString d = scriptMgr->getName(scriptId).trimmed();
			obj.insert("name",d);
			obj.insert("name_localized", StelTranslator::globalTranslator->qtranslate(d));
			d = scriptMgr->getDescription(scriptId).trimmed();
			obj.insert("description",d);
			obj.insert("description_localized", StelTranslator::globalTranslator->qtranslate(d));
			obj.insert("author",scriptMgr->getAuthor(scriptId).trimmed());
			obj.insert("license",scriptMgr->getLicense(scriptId).trimmed());
			//shortcut often causes a large delay because the whole file gets searched, and it is usually missing, so we ignore it
			//obj.insert("shortcut",scriptMgr->getShortcut(scriptId));

			response.writeJSON(QJsonDocument(obj));
		}
		else
		{
			response.writeRequestError("need parameter: id");
		}
	}
	else if(operation == "status")
	{
		//generic script status
		QJsonObject obj;
		obj.insert("scriptIsRunning",scriptMgr->scriptIsRunning());
		obj.insert("runningScriptId",scriptMgr->runningScriptId());

		response.writeJSON(QJsonDocument(obj));
	}
	else
	{
		//TODO some sort of service description?
		response.writeRequestError("unsupported operation. GET: list,info,status POST: run,stop");
	}
}
Пример #2
0
void AbstractAPIService::post(const QByteArray &operation, const APIParameters &parameters, const QByteArray &data, APIServiceResponse& response)
{
	Q_UNUSED(operation);
	Q_UNUSED(parameters);
	Q_UNUSED(data);

	response.setStatus(405,"Method Not allowed");
	QString str(QStringLiteral("Method POST not allowed for service %2"));
	response.setData(str.arg(getPath()).toLatin1());
}
Пример #3
0
void ScriptService::postImpl(const QByteArray& operation, const APIParameters &parameters, const QByteArray &data, APIServiceResponse &response)
{
	Q_UNUSED(data);

	if(operation=="run")
	{
		//retrieve detail about a single script
		QString scriptId = QString::fromUtf8(parameters.value("id"));

		if(scriptMgr->scriptIsRunning())
		{
			response.setData("error: a script is already running");
			return;
		}

		QString script;
		//Prepare the script. Through a refactor, this is separate from actually running the script,
		//and can be done in the HTTP thread. It can also notify the caller of an invalid script ID.
		bool ret = scriptMgr->prepareScript(script,scriptId);

		if(!ret)
		{
			response.setData("error: could not prepare script, wrong id?");
			return;
		}

		//ret = scriptMgr->runScript(scriptId);
		//we may be in another thread! for the actual execution of the script, we need to invoke the event queue
		//we can not use blocking connection here because runPreprocessedScript is blocking!
		//there is also no way to check if the script was actually started except for polling some time later
		QMetaObject::invokeMethod(scriptMgr,"runPreprocessedScript",
					  Qt::QueuedConnection,Q_ARG(QString,script),Q_ARG(QString,scriptId));
		response.setData("ok");
	}
	else if(operation=="direct")
	{
		//directly runs the given script code
		QString code = QString::fromUtf8(parameters.value("code"));
		QString useIncludes = QString::fromUtf8(parameters.value("useIncludes"));

		if(code.isEmpty())
		{
			response.writeRequestError("need parameter: code");
		}
		else
		{
			if(scriptMgr->scriptIsRunning())
			{
				response.setData("error: a script is already running");
				return;
			}

			//use QVariant logic to convert to bool
			bool bUseIncludes = QVariant(useIncludes).toBool();

			//also requires queued connection
			QMetaObject::invokeMethod(scriptMgr,"runScriptDirect", Qt::QueuedConnection,
						  Q_ARG(QString,code),
						  Q_ARG(QString,bUseIncludes ? QStringLiteral("") : QString() ));
			response.setData("ok");
		}
	}
	else if(operation=="stop")
	{
		//scriptMgr->stopScript();
		QMetaObject::invokeMethod(scriptMgr,"stopScript",SERVICE_DEFAULT_INVOKETYPE);

		response.setData("ok");
	}
	else
	{
		//TODO some sort of service description?
		response.writeRequestError("unsupported operation. GET: list,info,status POST: run,direct,stop");
	}
}