public async Task <string> ImportCkModel(string tenantId, ScopeIdsDto scopeId, string ckModelFilePath) { ArgumentValidation.ValidateString(nameof(tenantId), tenantId); ArgumentValidation.ValidateExistingFile(nameof(ckModelFilePath), ckModelFilePath); var request = new RestRequest("models/ImportCk", Method.POST); request.AddQueryParameter("tenantId", tenantId); request.AddQueryParameter("scopeId", ((int)scopeId).ToString()); if (Path.GetExtension(ckModelFilePath)?.ToLower() == ".zip") { request.AddFile("file", ckModelFilePath, contentType: "application/zip"); } else if (Path.GetExtension(ckModelFilePath)?.ToLower() == ".json") { request.AddFile("file", ckModelFilePath, contentType: "application/json"); } else { throw new ServiceClientException($"'{ckModelFilePath}' is not a supported file."); } IRestResponse <string> response = await Client.ExecuteAsync <string>(request); ValidateResponse(response); return(response.Data); }
public async Task ImportCkAsync(string tenantId, string key, ScopeIdsDto scopeId, IJobCancellationToken cancellationToken) { try { if (scopeId == ScopeIdsDto.System) { throw new InvalidOperationException("Scope SYSTEM cannot be imported, because this scope is handled by system."); } Logger.Info($"Reading input file from cache for CK import to '{tenantId}'"); var tempFile = await GetTempFile(key); Logger.Info($"Starting import of file '{tempFile}'"); using var systemSession = await _systemContext.StartSystemSessionAsync(); systemSession.StartTransaction(); await _systemContext.ImportCkModelAsync(systemSession, tenantId, (ScopeIds)scopeId, tempFile, cancellationToken.ShutdownToken); await systemSession.CommitTransactionAsync(); await ClearCache(key); Logger.Info($"Import of file '{tempFile}' completed."); } catch (Exception e) { Logger.Error(e, "Import failed with error."); throw; } }
public async Task <IActionResult> ImportCk([Required] string tenantId, ScopeIdsDto scopeId, [FromForm] IFormFile file) { try { var cacheKey = await AddFileToCache(file); var id = _backgroundJobClient.Enqueue <ImportModelJob>(job => job.ImportCkAsync(tenantId, cacheKey, scopeId, JobCancellationToken.Null)); return(Ok(id)); } catch (InvalidOperationException e) { return(BadRequest(new InternalServerError(e.Message))); } }