public async Task <UploadResponse> UploadWithRetry(string organizationUnitId, UploadRootObject uploadObject) { var serializedValue = JsonConvert.SerializeObject(uploadObject, new JsonSerializerSettings()); if (!DigitalVaultHttpClient.DefaultRequestHeaders.Contains("Dgp-UserContext")) { DigitalVaultHttpClient.DefaultRequestHeaders.Add("Dgp-UserContext", "document-provider"); } var accessToken = await GetAccesToken(string.Empty); DigitalVaultHttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); for (int i = 1; i <= MaxRequestAttempts; i++) { try { HttpContent contentPost = new StringContent(serializedValue, Encoding.UTF8, "application/json"); var response = await DigitalVaultHttpClient.PostAsync($"organizationunits/{organizationUnitId}/documents", contentPost); var responseContent = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { throw new Exception($"Upload for organisation unit {organizationUnitId} failed ({response.StatusCode}): {responseContent}"); } Console.WriteLine($"POST upload document response ({(int)response.StatusCode}): {responseContent} "); return(JsonConvert.DeserializeObject <UploadResponse>(responseContent)); } catch (Exception) { await Task.Delay(new TimeSpan(0, 0, RequestDelayInSeconds)); } } throw new Exception("Maximum upload attempts exceeded!"); }
private static async Task UploadDocument(string organisationalUnit, string destinationName, string bulkOperationName, string documentName, DateTime referenceDate) { var externalName = $"Mijn testdocument van {DateTime.Now.ToShortDateString()}"; var fileContent = GetFile("testdocument.pdf"); var fileContentAsBase64String = Convert.ToBase64String(fileContent); var upload = new UploadRootObject() { Name = documentName, ExternalName = externalName, Content = fileContentAsBase64String, // avoid min-date "01/01/0001 00:00:00" ReferenceDate = referenceDate, // only alphanumeric characters, underscores and dashes are allowed (name is used as route parameter in Vault UI) BulkOperation = bulkOperationName, Metadata = new List <Metadata>(), Destinations = new List <Destination>() }; var destination = new Destination() { Name = destinationName, AutoCommit = true, // make document visible to the user; if false, an administrator must commit the document manually later on NotificationNeeded = false, Tags = new List <string>() { "test", destinationName } }; upload.Destinations.Add(destination); // Document type - must be known within the vault; // Defining a document type and the related metadata for an organisational unit is an administrative task // and can be done with the Digital Vault admin UI in the near future. upload.DocumentType = "normal"; // Metadata - the list of possible metadata fields is related to the document type // A metadata-field must be defined with the document type before it can be used during upload; // If a required metadata-field or values isn't supplied during upload, an 500-error is returned; // Therefore if you don't know if a value is always present, don't define a metadata-field as required upload.Metadata.AddRange(new List <Metadata> { new Metadata("gebruikers_id", "123456"), new Metadata("tags", "001234"), new Metadata("documentnaam", externalName), new Metadata("refertejaar_maand", DateTime.Now.ToString("yyyy/MM")), new Metadata("naam", destination.Name), new Metadata("voornaam", "test"), new Metadata("rijksregisternummer", destinationName), new Metadata("personeelsnummer", "001234"), new Metadata("vestigingsplaats", "locatie 1"), new Metadata("afreken_eenheid", "snip-it"), new Metadata("werkgever", "Snippet Inc."), new Metadata("bestandsnaam", documentName) });; var response = await _service.UploadWithRetry(organisationalUnit, upload); }