MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / get

Method get

app/src/API/Handlers/ScriptsHandler.cpp:224–273  ·  view source on GitHub ↗

* @brief Returns the body of one script (UTF-8 text). */

Source from the content-addressed store, hash-verified

222 * @brief Returns the body of one script (UTF-8 text).
223 */
224API::CommandResponse API::Handlers::ScriptsHandler::get(const QString& id,
225 const QJsonObject& params)
226{
227 if (!params.contains(QStringLiteral("kind")))
228 return CommandResponse::makeError(
229 id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: kind"));
230
231 if (!params.contains(QStringLiteral("id")))
232 return CommandResponse::makeError(
233 id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: id"));
234
235 const auto kindStr = params.value(QStringLiteral("kind")).toString();
236 const auto entryId = params.value(QStringLiteral("id")).toString();
237 const auto* kind = findKind(kindStr);
238 if (!kind)
239 return CommandResponse::makeError(
240 id, ErrorCode::InvalidParam, QStringLiteral("Unknown script kind: %1").arg(kindStr));
241
242 const auto manifest = loadManifest(*kind);
243 bool known = false;
244 for (const auto& v : manifest) {
245 if (v.toObject().value(QStringLiteral("file")).toString() == entryId) {
246 known = true;
247 break;
248 }
249 }
250 if (!known)
251 return CommandResponse::makeError(
252 id,
253 ErrorCode::InvalidParam,
254 QStringLiteral("Unknown template id '%1' for kind '%2'. "
255 "Use scripts.list {kind} to see what's available.")
256 .arg(entryId, kindStr));
257
258 QFile f(kind->bodyDir + entryId + kind->extension);
259 if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
260 return CommandResponse::makeError(
261 id,
262 ErrorCode::ExecutionError,
263 QStringLiteral("Failed to read template '%1' (%2)").arg(entryId, f.fileName()));
264
265 const auto body = QString::fromUtf8(f.readAll());
266
267 QJsonObject result;
268 result[QStringLiteral("kind")] = kind->id;
269 result[QStringLiteral("id")] = entryId;
270 result[QStringLiteral("body")] = body;
271 result[QStringLiteral("byteCount")] = body.size();
272 return CommandResponse::makeSuccess(id, result);
273}

Callers 11

valueGetMethod · 0.45
startServerMethod · 0.45
fetchHelpPageMethod · 0.45
fetchHelpIndexMethod · 0.45
AssistantMethod · 0.45
conversationObjectMethod · 0.45
providerAtMethod · 0.45
localBaseUrlMethod · 0.45
setLocalBaseUrlMethod · 0.45
refreshLocalModelsMethod · 0.45
refreshModelsMethod · 0.45

Calls 7

findKindFunction · 0.85
loadManifestFunction · 0.85
containsMethod · 0.45
valueMethod · 0.45
openMethod · 0.45
fileNameMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected