/// <summary> /// Builds a new document envelope from the supplied <see cref="RequestDetails"/> instance. /// </summary> /// <param name="scenario">Name of the scenario we're building a document envelope for.</param> /// <param name="requestDetails"><see cref="RequestDetails"/> instance containing additional request details.</param> /// <param name="logPrefix">Logging prefix to use.</param> /// <param name="log"><see cref="ILogger"/> instance used for logging.</param> /// <returns><see cref="JObject"/> instance containing the document envelope.</returns> private static async Task <JObject> BuildDocumentEnvelopeAsync(string scenario, RequestDetails requestDetails, string logPrefix, ILogger log) { // Build the base envelope log.LogDebug($"{logPrefix}Building the base envelope"); JObject envelope = EnvelopeBuilder.BuildDocumentEnvelope(requestDetails); // Get the RoutingSlip from config log.LogDebug($"{logPrefix}Getting full RoutingSlip from config"); JObject routingSlip = await new ApimRestClient(requestDetails).GetRoutingSlipAsync(scenario); // Select the routes array JArray routes = (JArray)routingSlip?.First?.First; // Check if we have any routes if (routes != null) { log.LogDebug($"{logPrefix}Removing RoutingParameters from RoutingSlip"); // Remove the routing parameters foreach (JObject item in routes) { item.Properties().Where(key => key.Name.Equals("routingParameters")).ToList().ForEach(attr => attr.Remove()); } } log.LogDebug($"{logPrefix}Adding RoutingSlip to base Envelope"); // Update the routing slip section JObject routingSlipSection = (JObject)envelope?["header"]?["routingSlip"]; routingSlipSection.Add(new JProperty("scenario", scenario)); routingSlipSection.Add(new JProperty("nextRoute", 0)); routingSlipSection.Add(routingSlip.First); return(envelope); }