private async Task <EngineDoc> ImportApp(string jwtToken, string multiCloudMachineName, string sourceDocumentId, string fileName, string url, string version)
        {
            string fileId        = url.Substring(url.LastIndexOf('/') + 1);
            string additionalUri = "apps/import?fileId=" + fileId + "&mode=autoreplace" + "&appId=" + CalculateDocumentOrTagId(sourceDocumentId, fileName);

            using (HttpResponseMessage response = await SetupAndSendRequest(HttpMethod.Post, multiCloudMachineName + additionalUri, "", jwtToken, "UserAgent", "QDS/" + version))
            {
                try
                {
                    switch (response.StatusCode)
                    {
                    case HttpStatusCode.RequestEntityTooLarge:
                        PrintMessage("Failure - Could not upload document to engine, since engine reported that the app exceeds the maximum size. statusCode= " + response.StatusCode + ", reason= " + response.ReasonPhrase, false);
                        throw new HttpAppSizeException("App size exceeds max size");

                    case HttpStatusCode.GatewayTimeout:
                        PrintMessage("Failure - Could not upload document in engine, API gateway in QSEfe/Multicloud reported that it timed out when waiting for a response. statusCode= " + response.StatusCode + ", reason= " + response.ReasonPhrase, false);
                        WorkflowExceptionStrategy.ThrowException(response);
                        break;
                    }
                    if (!response.IsSuccessStatusCode)
                    {
                        PrintMessage("Failure - Could not upload document to engine. statusCode= " + response.StatusCode + ", reason= " + response.ReasonPhrase, false);
                        WorkflowExceptionStrategy.ThrowException(response);
                    }
                    else
                    {
                        PrintMessage("Success - Upload document " + fileName + " to engine", false);
                    }

                    var responseContent = await response.Content.ReadAsStringAsync() ?? "{}";

                    EngineDoc result = JsonConvert.DeserializeObject <EngineDoc>(responseContent);
                    return(result);
                }
                catch (Exception e)
                {
                    PrintMessage("Failed to import app " + fileName + " to engine. Exception: " + e.Message, false);
                    return(null);
                }
            }
        }
        private async Task <string> ImportApp(string jwtToken, string multiCloudMachineName, string sourceDocumentId, string fileName, string url, string mode, string appId, string version)
        {
            string fileId             = url.Substring(url.LastIndexOf('/') + 1);
            string fileNameWithOutExt = Path.GetFileNameWithoutExtension(fileName);
            string additionalUri      = "apps/import?fileId=" + fileId + "&mode=" + mode + "&appId=" + (appId == null ? CalculateDocumentOrTagId(sourceDocumentId, fileNameWithOutExt) : appId) + "&fallbackName=" + fileNameWithOutExt;

            using (HttpResponseMessage response = await SetupAndSendRequest(HttpMethod.Post, multiCloudMachineName + additionalUri, "", jwtToken, SetupQdsHeaderValue(version)))
            {
                try
                {
                    switch (response.StatusCode)
                    {
                    case HttpStatusCode.RequestEntityTooLarge:
                        PrintMessage("Failure - Could not upload document to engine, since engine reported that the app exceeds the maximum size. statusCode= " + response.StatusCode + ", reason= " + response.ReasonPhrase, false);
                        throw new HttpAppSizeException("App size exceeds max size");

                    case HttpStatusCode.GatewayTimeout:
                        PrintMessage("Failure - Could not upload document in engine, API gateway in QSEfe/Multicloud reported that it timed out when waiting for a response. statusCode= " + response.StatusCode + ", reason= " + response.ReasonPhrase, false);
                        WorkflowExceptionStrategy.ThrowException(response);
                        break;
                    }
                    if (!response.IsSuccessStatusCode)
                    {
                        PrintMessage("Failure - Could not upload document to engine. statusCode= " + response.StatusCode + ", reason= " + response.ReasonPhrase, false);
                        WorkflowExceptionStrategy.ThrowException(response);
                    }
                    else
                    {
                        PrintMessage("Success - Upload document " + fileName + " to engine", false);
                    }
                    return(await response.Content.ReadAsStringAsync() ?? "{}");
                }
                catch (Exception e)
                {
                    PrintMessage("Failed to import " + fileName + " to engine. Exception: " + e.Message, false);
                    return(string.Empty);
                }
            }
        }
        public async Task <HttpResponseMessage> DistributeDocument(string fileNameAndPath, string cloudDeploymentResourceUrl, string sourceDocumentId, string jwtToken)
        {
            string multiCloudMachineName = cloudDeploymentResourceUrl.TrimEnd('/') + "/api/v1/";
            string qvDocName             = Path.GetFileNameWithoutExtension(fileNameAndPath);
            var    l_FileStream          = new FileStream(fileNameAndPath, FileMode.Open, FileAccess.Read);
            var    stream  = new BufferedStream(l_FileStream, 8192);
            var    content = new StreamContent(stream, 65536);

            try
            {
                content.Headers.Add("Content-Type", "binary/octet-stream");
                using (HttpClient cloudClient = new HttpClient())
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, multiCloudMachineName + "apps/import?mode=autoreplace&appId=" + sourceDocumentId + "&fallbackName=" + qvDocName)
                    {
                        Content = content
                    };
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
                    HttpResponseMessage autoReplaceDocResponse = await cloudClient.SendAsync(request);

                    switch (autoReplaceDocResponse.StatusCode)
                    {
                    case HttpStatusCode.RequestEntityTooLarge:
                        PrintMessage("Failure - Could not upload document to engine, since engine reported that the app exceeds the maximum size. statusCode= " + autoReplaceDocResponse.StatusCode + ", reason= " + autoReplaceDocResponse.ReasonPhrase, false);
                        throw new HttpAppSizeException("App size exceeds max size");

                    case HttpStatusCode.GatewayTimeout:
                        PrintMessage("Failure - Could not upload document in engine, API gateway in QSEfe/Multicloud reported that it timed out when waiting for a response. statusCode= " + autoReplaceDocResponse.StatusCode + ", reason= " + autoReplaceDocResponse.ReasonPhrase, false);
                        WorkflowExceptionStrategy.ThrowException(autoReplaceDocResponse);
                        break;
                    }
                    if (!autoReplaceDocResponse.IsSuccessStatusCode)
                    {
                        PrintMessage("Failure - Could not upload document " + qvDocName + "to engine. statusCode= " + autoReplaceDocResponse.StatusCode + ", reason= " + autoReplaceDocResponse.ReasonPhrase, false);
                        WorkflowExceptionStrategy.ThrowException(autoReplaceDocResponse);
                    }

                    else
                    {
                        Console.WriteLine("Success - Document  " + qvDocName + " uploaded to engine");
                    }

                    var responseContent = await autoReplaceDocResponse.Content.ReadAsStringAsync() ?? "{}";

                    EngineDoc result = JsonConvert.DeserializeObject <EngineDoc>(responseContent);

                    HttpResponseMessage createItemResponse = await CreateItem(result, jwtToken, multiCloudMachineName, qvDocName);

                    return(createItemResponse);
                }
            }
            catch (HttpAppSizeException)
            {
                PrintMessage("App " + qvDocName + " exceeds max size", false);
            }
            catch (WorkflowException e)
            {
                PrintMessage("Failure - Could not upload document " + qvDocName + " to engine, workflowException. Message =" + e.Message, false);
            }
            catch (TaskCanceledException e)
            {
                PrintMessage("Failure - Could not upload document " + qvDocName + " to engine, Connection timeout. Message =" + e.Message, false);
            }
            catch (Exception ex)
            {
                PrintMessage("Failure - Could not upload document " + qvDocName + " to engine, Other exception of unknown cause. Message =" + ex.Message, false);
            }
            finally
            {
                content.Dispose();
                stream.Dispose();
                l_FileStream.Dispose();
            }
            return(null);
        }