Parcourir la Source

Move hardcoded FTP settings into the configuration.

Andrew Klopper il y a 8 ans
Parent
révision
ec4d147867

+ 18 - 0
BulkPrintingAPI/Configuration/FTPOptions.cs

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

+ 5 - 4
BulkPrintingAPI/Controllers/Utils.cs

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
             var remoteFileName = string.Format("{0}_{1}.dat", batch.Account.Id, batch.Id);
308
             var remoteFileName = string.Format("{0}_{1}.dat", batch.Account.Id, batch.Id);
308
             using (var voucherStream = new MemoryStream())
309
             using (var voucherStream = new MemoryStream())
309
             {
310
             {
310
                 using (var ftp = new FtpClient(new FtpClientConfiguration()
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
                     await ftp.LoginAsync().ConfigureAwait(false);
318
                     await ftp.LoginAsync().ConfigureAwait(false);

+ 2 - 0
BulkPrintingAPI/Startup.cs

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