/// <summary> /// Activate the service account. /// </summary> /// <returns></returns> private async Task ActivateAccountAsync() { var aRes = await _client.SendAsync($"{_options.Api.Uri}/auth/activate", HttpMethod.Post); if (!aRes.IsSuccessStatusCode) { throw new HttpClientRequestException(aRes); } }
/// <summary> /// Read the JSON package, iterate through it and send the items to the configured endpoint URL. /// </summary> /// <param name="file"></param> /// <returns></returns> public async Task <int> ImportAsync(FileInfo file) { if (file == null) { throw new ArgumentNullException(nameof(file)); } if (!file.Exists) { throw new ArgumentException($"Argument '{nameof(file)}' must be an existing file."); } if (String.IsNullOrWhiteSpace(_options.Api.Uri)) { throw new InvalidOperationException($"Configuration 'Api:Uri' is required."); } if (String.IsNullOrWhiteSpace(_options.Api.ImportUrl)) { throw new InvalidOperationException($"Configuration 'Api:ImportUrl' is required."); } // Activate the service account. var aRes = await _client.SendAsync($"{_options.Api.Uri}/auth/activate", HttpMethod.Post); if (!aRes.IsSuccessStatusCode) { throw new HttpClientRequestException(aRes); } _logger.LogInformation($"url: {_options.Api.ImportUrl}, quantity: {_options.Import.Quantity}"); var properties = await JsonSerializer.DeserializeAsync <IEnumerable <object> >(file.OpenRead()); var index = _options.Import.Skip; var total = properties.Count(); var failures = 0; var iteration = 0; _logger.LogInformation($"Items in file {total}"); while (total > index) { var items = properties.Skip(index).Take(_options.Import.Quantity); _logger.LogInformation($"Import iteration: {iteration}, Items remaining: {total - index}"); var success = await RetryAsync(_options.Api.HttpMethod.GetHttpMethod(), $"{_options.Api.Uri}{_options.Api.ImportUrl}", items); failures = success ? 0 : failures + 1; // Failed request, abort the importer. if (failures >= _options.AbortAfterFailure) { return(1); } // Exit the import, we're done. if (_options.Import.Iterations > 0 && ++iteration >= _options.Import.Iterations) { break; } index += _options.Import.Quantity; } return(0); }