public static async Task <object> Run([HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req, TraceWriter log)
        {
            log.Info($"AMS v2 Function - SubmitMediaJob was triggered!");

            string jsonContent = await req.Content.ReadAsStringAsync();

            dynamic data = JsonConvert.DeserializeObject(jsonContent);

            // Validate input objects
            if (data.assetId == null)
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass assetId in the input object" }));
            }
            if (data.mediaTasks == null)
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass mediaTasks in the input object" }));
            }
            string assetId = data.assetId;
            List <AMSMediaTask> mediaTasks = ((JArray)data.mediaTasks).ToObject <List <AMSMediaTask> >();
            int jobPriority = 10;

            if (data.jobPriority != null)
            {
                jobPriority = data.jobPriority;
            }
            string jobName = "Azure Functions - Media Processing Job";

            if (data.jobName != null)
            {
                jobName = data.jobName;
            }

            MediaServicesCredentials amsCredentials = new MediaServicesCredentials();
            IAsset asset  = null;
            IJob   job    = null;
            uint   taskId = 0;

            try
            {
                // Load AMS account context
                log.Info($"Using AMS v2 REST API Endpoint : {amsCredentials.AmsRestApiEndpoint.ToString()}");

                AzureAdTokenCredentials tokenCredentials = new AzureAdTokenCredentials(amsCredentials.AmsAadTenantDomain,
                                                                                       new AzureAdClientSymmetricKey(amsCredentials.AmsClientId, amsCredentials.AmsClientSecret),
                                                                                       AzureEnvironments.AzureCloudEnvironment);
                AzureAdTokenProvider tokenProvider = new AzureAdTokenProvider(tokenCredentials);
                _context = new CloudMediaContext(amsCredentials.AmsRestApiEndpoint, tokenProvider);

                // Get the Asset
                asset = _context.Assets.Where(a => a.Id == assetId).FirstOrDefault();
                if (asset == null)
                {
                    return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Asset not found" }));
                }

                // Declare a new Media Processing Job
                job          = _context.Jobs.Create(jobName + " - " + asset.Name + " [" + assetId + "]");
                job.Priority = jobPriority;

                foreach (AMSMediaTask mediaTask in mediaTasks)
                {
                    ITask           task      = null;
                    IMediaProcessor processor = MediaServicesHelper.GetLatestMediaProcessorByName(_context, mediaTask.mediaProcessor);
                    if (mediaTask.configuration.StartsWith(base64encodedstringprefix))
                    {
                        byte[] b64decoded = Convert.FromBase64String(mediaTask.configuration.Substring(base64encodedstringprefix.Length));
                        mediaTask.configuration = System.Text.ASCIIEncoding.ASCII.GetString(b64decoded);
                    }
                    task = job.Tasks.AddNew(mediaTask.mediaTaskName, processor, mediaTask.configuration, TaskOptions.None);
                    if (mediaTask.additionalInputAssetIds != null)
                    {
                        foreach (string inputAssetId in mediaTask.additionalInputAssetIds)
                        {
                            IAsset aAsset = _context.Assets.Where(a => a.Id == inputAssetId).FirstOrDefault();
                            task.InputAssets.Add(aAsset);
                        }
                    }
                    // Add primary input asset at last
                    task.InputAssets.Add(asset);

                    string outputAssetName = asset.Name + " - " + mediaTask.mediaProcessor;
                    IAsset outputAsset     = task.OutputAssets.AddNew(outputAssetName, mediaTask.outputStorageAccount, asset.Options);

                    taskId++;
                }

                // media job submission
                job.Submit();
            }
            catch (Exception e)
            {
                log.Info($"Exception {e}");
                return(req.CreateResponse(HttpStatusCode.BadRequest));
            }

            // Prepare output JSON
            int    taskIndex        = 0;
            JArray mediaTaskOutputs = new JArray();

            foreach (var task in job.Tasks)
            {
                JObject o = new JObject();
                o["mediaTaskIndex"]         = taskIndex;
                o["mediaTaskId"]            = task.Id;
                o["mediaTaskName"]          = task.Name;
                o["mediaProcessorId"]       = task.MediaProcessorId;
                o["mediaTaskOutputAssetId"] = task.OutputAssets[0].Id;
                mediaTaskOutputs.Add(o);
                taskIndex++;
            }

            JObject result = new JObject();

            result["jobId"]            = job.Id;
            result["mediaTaskOutputs"] = mediaTaskOutputs;
            return(req.CreateResponse(HttpStatusCode.OK, result));
        }