Kaynağa Gözat

Move hardcoded FTP settings into the configuration.

Andrew Klopper 8 yıl önce
ebeveyn
işleme
ec4d147867

+ 18 - 0
BulkPrintingAPI/Configuration/FTPOptions.cs

@@ -0,0 +1,18 @@
1
+using Microsoft.Extensions.Configuration;
2
+
3
+namespace BulkPrintingAPI.Configuration
4
+{
5
+    public class FTPOptions
6
+    {
7
+        public FTPOptions(IConfiguration configuration)
8
+        {
9
+            configuration.Bind(this);
10
+        }
11
+
12
+        public string Host { get; set; }
13
+
14
+        public string Username { get; set; }
15
+
16
+        public string Password { get; set; }
17
+    }
18
+}

+ 5 - 3
BulkPrintingAPI/Controllers/BatchesController.cs

@@ -33,16 +33,18 @@ namespace BulkPrintingAPI.Controllers
33 33
         private readonly ILogger _logger;
34 34
         private readonly IMemoryCache _cache;
35 35
         private readonly DataEncryptionOptions _dataEncryptionOptions;
36
+        private readonly FTPOptions _ftpOptions;
36 37
         private readonly MAX.ClientFactory _clientFactory;
37 38
         private readonly MAXContext _context;
38 39
 
39 40
         public BatchesController(ILoggerFactory loggerFactory, IMemoryCache cache,
40
-            DataEncryptionOptions dataEncryptionOptions,
41
+            DataEncryptionOptions dataEncryptionOptions, FTPOptions ftpOptions,
41 42
             MAX.ClientFactory clientFactory, MAXContext context)
42 43
         {
43 44
             _logger = loggerFactory.CreateLogger(GetType().FullName);
44 45
             _cache = cache;
45 46
             _dataEncryptionOptions = dataEncryptionOptions;
47
+            _ftpOptions = ftpOptions;
46 48
             _clientFactory = clientFactory;
47 49
             _context = context;
48 50
         }
@@ -82,7 +84,7 @@ namespace BulkPrintingAPI.Controllers
82 84
 
83 85
             if (!batch.ReadyForDownload)
84 86
             {
85
-                await Utils.DownloadVouchersAsync(_context, batch);
87
+                await Utils.DownloadVouchersAsync(_ftpOptions, _context, batch);
86 88
             }
87 89
 
88 90
             return Ok(batch);
@@ -117,7 +119,7 @@ namespace BulkPrintingAPI.Controllers
117 119
 
118 120
             try
119 121
             {
120
-                await Utils.DownloadVouchersAsync(_context, orderResponse.Batch);
122
+                await Utils.DownloadVouchersAsync(_ftpOptions, _context, orderResponse.Batch);
121 123
             }
122 124
             catch (Exception e)
123 125
             {

+ 5 - 4
BulkPrintingAPI/Controllers/Utils.cs

@@ -302,16 +302,17 @@ namespace BulkPrintingAPI.Controllers
302 302
             }
303 303
         }
304 304
 
305
-        public static async Task DownloadVouchersAsync(MAXContext context, Batch batch)
305
+        public static async Task DownloadVouchersAsync(FTPOptions ftpOptions, MAXContext context,
306
+            Batch batch)
306 307
         {
307 308
             var remoteFileName = string.Format("{0}_{1}.dat", batch.Account.Id, batch.Id);
308 309
             using (var voucherStream = new MemoryStream())
309 310
             {
310 311
                 using (var ftp = new FtpClient(new FtpClientConfiguration()
311 312
                 {
312
-                    Host = "41.223.25.5",
313
-                    Username = "anonymous",
314
-                    Password = "andrew@"
313
+                    Host = ftpOptions.Host,
314
+                    Username = ftpOptions.Username,
315
+                    Password = ftpOptions.Password
315 316
                 }))
316 317
                 {
317 318
                     await ftp.LoginAsync().ConfigureAwait(false);

+ 2 - 0
BulkPrintingAPI/Startup.cs

@@ -42,6 +42,8 @@ namespace BulkPrintingAPI
42 42
                 Configuration.GetSection("DataEncryption")));
43 43
             services.AddSingleton(new MAX.ClientFactory(
44 44
                 Configuration.GetSection("MAX")));
45
+            services.AddSingleton(new FTPOptions(
46
+                Configuration.GetSection("FTP")));
45 47
             services.AddDbContext<MAX.Models.MAXContext>(
46 48
                 options => options.UseSqlServer(
47 49
                     Configuration["Database:ConnectionString"],