Ejemplo n.º 1
0
        public override async Task <IApiResponse> ExecuteAsync(IApiRequest request)
        {
            if (String.IsNullOrEmpty(_command.Script))
            {
                throw new RequestModelException($"'script' must be specified for script command");
            }
            String code = await _reader.ReadTextFileAsync(_command.Path, _command.Script.AddExtension("js"));

            _script.SetCurrentDirectory(_command.Path);
            var args = new ExpandoObject();

            if (request.Body != null)
            {
                args.Set("body", request.Body);
            }
            if (request.Query != null)
            {
                args.Set("query", request.Query);
            }
            if (!String.IsNullOrEmpty(_command.Id))
            {
                args.Set("id", _command.Id);
            }

            var prms = _command.Parameters.Clone(null);

            if (request.UserId != 0)
            {
                prms.Set("UserId", request.UserId);
            }
            if (request.TenantId != null)
            {
                prms.Set("TenantId", request.TenantId);
            }
            if (!String.IsNullOrEmpty(request.Segment))
            {
                prms.Set("Segment", request.Segment);
            }

            var obj = _script.Execute(code, prms, args);

            return(new ApiResponse()
            {
                ContentType = MimeTypes.Application.Json,
                Body = JsonConvert.SerializeObject(Wrap(obj), JsonHelpers.CompactSerializerSettings)
            });
        }
Ejemplo n.º 2
0
        public async Task <ServerCommandResult> Execute(RequestCommand cmd, ExpandoObject dataToExec)
        {
            if (String.IsNullOrEmpty(cmd.file))
            {
                throw new RequestModelException($"'file' must be specified for command '{cmd.command}'");
            }
            String code = await _reader.ReadTextFileAsync(cmd.Path, cmd.file.AddExtension("js"));

            if (code == null)
            {
                throw new RequestModelException($"File not found '{cmd.file}'");
            }
            _engine.SetCurrentDirectory(cmd.Path);
            var retval = _engine.Execute(code, dataToExec, cmd.args);

            return(new ServerCommandResult()
            {
                Data = JsonConvert.SerializeObject(retval, JsonHelpers.StandardSerializerSettings)
            });
        }