public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log,
            CancellationToken token)
        {
            ITransformationProcess process;
            string id;
            Guid   processId;

            switch (req.Method)
            {
            // Trigger the transformation of a whole site
            case "POST":
                string target = "TargetSite";

                // Create the process
                process = await transformationExecutor.CreateTransformationProcessAsync(token);

                // Start to enqueue items
                await process.StartProcessAsync(
                    pnpContextFactory,
                    sourceContext,
                    target,
                    token);

                return(new OkObjectResult(new { process.Id }));

            // Get the status of a running transformation
            case "GET":
                id = req.Query["id"];
                if (!Guid.TryParse(id, out processId))
                {
                    return(new BadRequestResult());
                }

                process = await transformationExecutor.LoadTransformationProcessAsync(processId, token);

                var status = await process.GetStatusAsync(token);

                return(new OkObjectResult(status));

            // Cancel a running transformation
            case "DELETE":
                id = req.Query["id"];
                if (!Guid.TryParse(id, out processId))
                {
                    return(new BadRequestResult());
                }

                process = await transformationExecutor.LoadTransformationProcessAsync(processId, token);

                await process.StopProcessAsync(token);

                return(new OkResult());
            }

            return(new BadRequestResult());
        }
Example #2
0
        /// <summary>
        /// Creates a new transformation process and waits for its completion
        /// </summary>
        /// <param name="transformationExecutor">The executor to use</param>
        /// <param name="sourceProvider">The source provider</param>
        /// <param name="targetContext">The target context</param>
        /// <param name="token">The cancellation token, if any</param>
        /// <returns>The status of the process</returns>
        public static async Task <TransformationProcessStatus> TransformAsync(
            this ITransformationExecutor transformationExecutor,
            ISourceProvider sourceProvider,
            PnPContext targetContext,
            CancellationToken token = default)
        {
            if (transformationExecutor == null)
            {
                throw new ArgumentNullException(nameof(transformationExecutor));
            }

            var process = await transformationExecutor.CreateTransformationProcessAsync(token).ConfigureAwait(false);

            using (process as IDisposable)
            {
                return(await process.StartAndWaitProcessAsync(sourceProvider, targetContext, token).ConfigureAwait(false));
            }
        }