コード例 #1
0
        /**
         * Creating a new file that contains the envelopeId and orderNumber
         */
        private static void saveDoc(string envelopeId, string orderNumber)
        {
            try
            {
                ApiClient dsApiClient = new ApiClient();
                JWTAuth   dsJWTAuth   = new JWTAuth(dsApiClient);
                // Checks for the token before calling the function getToken
                dsJWTAuth.CheckToken();
                var config = new Configuration(new ApiClient(dsJWTAuth.getBasePath()));
                config.AddDefaultHeader("Authorization", "Bearer " + dsJWTAuth.getToken());
                EnvelopesApi envelopesApi = new EnvelopesApi(config);

                // Create the output directory if needed
                string outputPath = Path.Combine(mainPath, "output");

                if (!Directory.Exists(outputPath))
                {
                    DirectoryInfo directory = Directory.CreateDirectory(outputPath);
                    if (!directory.Exists)
                    {
                        throw new Exception(DateTime.Now + " Failed to create directory.");
                    }
                }

                Stream results  = envelopesApi.GetDocument(dsJWTAuth.getAccountID(), envelopeId, "combined");
                string filePath = Path.Combine(mainPath, Path.Combine("output", DSConfig.FilePrefix + orderNumber + ".pdf"));
                // Create the output file
                using (System.IO.Stream stream = File.Create(filePath))
                    results.CopyTo(stream);

                if (!File.Exists(filePath))
                {
                    throw new Exception(DateTime.Now + " Failed to create file");
                }

                // Catch exception if BREAK_TEST equals to true or if orderNumber contains "/break"
                if (DSConfig.EnableBreakTest.Equals("true") && ("" + orderNumber).Contains("/break"))
                {
                    throw new Exception(DateTime.Now + " Break test");
                }
            }
            catch (ApiException e)
            {
                Console.WriteLine(DateTime.Now + " API exception: " + e.Message);
                throw new Exception(DateTime.Now + " saveDoc error");
            }

            // Catch exception while trying to save the document
            catch (Exception e)
            {
                Console.WriteLine(DateTime.Now + " Error while fetching and saving docs for envelope " + envelopeId + ", order " + orderNumber);
                Console.WriteLine(e.Message);
                throw new Exception(DateTime.Now + " saveDoc error");
            }
        }