11using McMaster . Extensions . CommandLineUtils ;
22using Microsoft . Extensions . Options ;
3- using Omnia . CLI . Extensions ;
3+ using Omnia . CLI . Commands . Application . Infrastructure ;
44using Omnia . CLI . Infrastructure ;
55using ShellProgressBar ;
66using System ;
77using System . Collections . Generic ;
88using System . IO ;
99using System . Linq ;
10- using System . Net . Http ;
1110using System . Threading . Tasks ;
12- using Omnia . CLI . Commands . Application . Infrastructure ;
11+ using Omnia . CLI . Extensions ;
1312
1413namespace Omnia . CLI . Commands . Application
1514{
@@ -18,12 +17,12 @@ namespace Omnia.CLI.Commands.Application
1817 public class ImportCommand
1918 {
2019 private readonly AppSettings _settings ;
21- private readonly HttpClient _httpClient ;
20+ private readonly IApiClient _apiClient ;
2221
23- public ImportCommand ( IOptions < AppSettings > options , IHttpClientFactory httpClientFactory )
22+ public ImportCommand ( IOptions < AppSettings > options , IApiClient apiClient )
2423 {
24+ _apiClient = apiClient ;
2525 _settings = options . Value ;
26- _httpClient = httpClientFactory . CreateClient ( ) ;
2726 }
2827
2928 [ Option ( "--subscription" , CommandOptionType . SingleValue , Description = "Name of the configured subscription." ) ]
@@ -54,7 +53,7 @@ public async Task<int> OnExecute(CommandLineApplication cmd)
5453
5554 var sourceSettings = _settings . GetSubscription ( Subscription ) ;
5655
57- await _httpClient . WithSubscription ( sourceSettings ) ;
56+ await _apiClient . Authenticate ( sourceSettings ) ;
5857
5958 var reader = new ImportDataReader ( ) ;
6059 try
@@ -90,7 +89,7 @@ private async Task<bool> ProcessDefinitions(ICollection<ImportData> data)
9089 foreach ( var dataEntry in data )
9190 {
9291 var ( success , messages ) =
93- await CreateEntities ( progressBar , _httpClient , Tenant , Environment , dataEntry . Definition , dataEntry . DataSource , dataEntry . Data ) ;
92+ await CreateEntities ( progressBar , _apiClient , Tenant , Environment , dataEntry . Definition , dataEntry . DataSource , dataEntry . Data ) ;
9493 progressBar . Tick ( ) ;
9594
9695 if ( success )
@@ -107,7 +106,8 @@ private async Task<bool> ProcessDefinitions(ICollection<ImportData> data)
107106 return ! failed . Any ( ) ;
108107 }
109108
110- private static async Task < ( bool Success , string [ ] Messages ) > CreateEntities ( ProgressBarBase progressBar , HttpClient httpClient ,
109+ private static async Task < ( bool Success , string [ ] Messages ) > CreateEntities ( ProgressBarBase progressBar ,
110+ IApiClient apiClient ,
111111 string tenantCode ,
112112 string environmentCode ,
113113 string definition ,
@@ -120,14 +120,14 @@ private async Task<bool> ProcessDefinitions(ICollection<ImportData> data)
120120 {
121121 foreach ( var ( rowNumber , values ) in data )
122122 {
123- var ( statusCode , errors ) = await CreateEntity ( httpClient , tenantCode , environmentCode , definition , dataSource , values ) ;
123+ var ( statusCode , errors ) = await CreateEntity ( apiClient , tenantCode , environmentCode , definition , dataSource , values ) ;
124124
125125 child . Tick ( statusCode == ( int ) StatusCodes . Success ? null : $ "Error creating entity for { dataSource } { definition } ") ;
126-
127- if ( statusCode == ( int ) StatusCodes . Success ) continue ;
128-
126+
127+ if ( statusCode == ( int ) StatusCodes . Success ) continue ;
128+
129129 child . ForegroundColor = ConsoleColor . DarkRed ;
130-
130+
131131 failedEntities . Add ( $ "Error to import { dataSource } .{ definition } : In row { rowNumber } with errors: { GetErrors ( errors ) } ") ;
132132 }
133133 }
@@ -143,29 +143,16 @@ static string JoinErrors(ApiError errors)
143143 => string . Join ( "" , errors . Errors . Select ( c => $ "\n \r { c . Name } - { c . Message } ") ) ;
144144 }
145145
146- private static async Task < ( int statusCode , ApiError errors ) > CreateEntity ( HttpClient httpClient ,
146+ private static async Task < ( int statusCode , ApiError errors ) > CreateEntity ( IApiClient apiClient ,
147147 string tenantCode ,
148148 string environmentCode ,
149149 string definition ,
150150 string dataSource ,
151151 IDictionary < string , object > data )
152152 {
153- var response = await httpClient . PostAsJsonAsync ( $ "/api/v1/{ tenantCode } /{ environmentCode } /application/{ definition } /{ dataSource } ", data ) ;
154- if ( response . IsSuccessStatusCode )
155- {
156- return ( ( int ) StatusCodes . Success , null ) ;
157- }
158-
159- var apiError = await GetErrorFromApiResponse ( response ) ?? new ApiError ( )
160- {
161- Code = ( ( int ) response . StatusCode ) . ToString ( ) ,
162- Message = ( int ) response . StatusCode != 403 ? response . StatusCode . ToString ( ) : "Access denied!"
163- } ;
164-
165- return ( ( int ) StatusCodes . InvalidOperation , apiError ) ;
153+ var response = await apiClient . Post ( $ "/api/v1/{ tenantCode } /{ environmentCode } /application/{ definition } /{ dataSource } ", data . ToHttpStringContent ( ) ) ;
154+
155+ return response . Success ? ( ( int ) StatusCodes . Success , null ) : ( ( int ) StatusCodes . InvalidOperation , response . ErrorDetails ) ;
166156 }
167-
168- private static Task < ApiError > GetErrorFromApiResponse ( HttpResponseMessage response )
169- => response . Content . ReadAsJsonAsync < ApiError > ( ) ;
170157 }
171158}
0 commit comments