-
Notifications
You must be signed in to change notification settings - Fork 6
/
Sample01_ImportNativeFiles.cs
160 lines (135 loc) · 6.76 KB
/
Sample01_ImportNativeFiles.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// <copyright file="Sample01_ImportNativeFiles.cs" company="Relativity ODA LLC">
// © Relativity All Rights Reserved.
// </copyright>
namespace Relativity.Import.Samples.DotNetClient.SampleCollection
{
using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Relativity.Import.Samples.DotNetClient.Helpers;
using Relativity.Import.V1;
using Relativity.Import.V1.Builders.DataSource;
using Relativity.Import.V1.Builders.Documents;
using Relativity.Import.V1.Models;
using Relativity.Import.V1.Models.Settings;
using Relativity.Import.V1.Models.Sources;
/// <summary>
/// Class containing examples of using import service SDK.
/// </summary>
public partial class ImportServiceSample
{
/// <summary>
/// Example of simple import native files.
/// </summary>
/// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
public async Task Sample01_ImportNativeFiles()
{
Console.WriteLine($"Running {nameof(this.Sample01_ImportNativeFiles)}");
// GUID identifiers for import job and data source.
Guid importId = Guid.NewGuid();
Guid sourceId = Guid.NewGuid();
// destination workspace artifact Id.
const int workspaceId = 1000000;
// set of columns indexes in load file used in import settings.
const int controlNumberColumnIndex = 0;
const int custodianColumnIndex = 1;
const int dateSentColumnIndex = 5;
const int emailToColumnIndex = 11;
const int fileNameColumnIndex = 13;
const int filePathColumnIndex = 22;
// Path to the load file used in data source settings.
const string loadFile01Path = "\\\\files\\T001\\StructuredData\\Import\\SampleDataSources\\load_file_01.dat";
// Create payload for request.
var createJobPayload = new
{
applicationName = "Import-service-sample-app",
correlationID = "Sample-job-0001",
};
// Configuration settings for document import. Builder is used to create settings.
ImportDocumentSettings importSettings = ImportDocumentSettingsBuilder.Create()
.WithAppendMode()
.WithNatives(x => x
.WithFilePathDefinedInColumn(filePathColumnIndex)
.WithFileNameDefinedInColumn(fileNameColumnIndex))
.WithoutImages()
.WithFieldsMapped(x => x
.WithField(controlNumberColumnIndex, "Control Number")
.WithField(custodianColumnIndex, "Custodian - Single Choice")
.WithField(emailToColumnIndex, "Email To")
.WithField(dateSentColumnIndex, "Date Sent"))
.WithoutFolders();
// Create payload for request.
var importSettingPayload = new { importSettings };
// Configuration settings for data source. Builder is used to create settings.
DataSourceSettings dataSourceSettings = DataSourceSettingsBuilder.Create()
.ForLoadFile(loadFile01Path)
.WithDelimiters(d => d
.WithColumnDelimiters('|')
.WithQuoteDelimiter('^')
.WithNewLineDelimiter('#')
.WithNestedValueDelimiter('&')
.WithMultiValueDelimiter('$'))
.WithFirstLineContainingHeaders()
.WithEndOfLineForWindows()
.WithStartFromBeginning()
.WithDefaultEncoding()
.WithDefaultCultureInfo();
// Create payload for request.
var dataSourceSettingsPayload = new { dataSourceSettings };
HttpClient httpClient = HttpClientHelper.CreateHttpClient();
// Create import job.
// endpoint: POST /import-jobs/{importId}
var createImportJobUri = RelativityImportEndpoints.GetImportJobCreateUri(workspaceId, importId);
var response = await httpClient.PostAsJsonAsync(createImportJobUri, createJobPayload);
await ImportJobSampleHelper.EnsureSuccessResponse(response);
// Add import document settings to existing import job (configure import job).
// endpoint: POST /import-jobs/{importId}/documents-configurations
var documentConfigurationUri = RelativityImportEndpoints.GetDocumentConfigurationUri(workspaceId, importId);
response = await httpClient.PostAsJsonAsync(documentConfigurationUri, importSettingPayload);
await ImportJobSampleHelper.EnsureSuccessResponse(response);
// Add data source settings to existing import job.
// endpoint: POST /import-jobs/{importId}/sources/{sourceId}
var importSourcesUri = RelativityImportEndpoints.GetImportSourceUri(workspaceId, importId, sourceId);
response = await httpClient.PostAsJsonAsync(importSourcesUri, dataSourceSettingsPayload);
await ImportJobSampleHelper.EnsureSuccessResponse(response);
// Start import job.
// endpoint: POST /import-jobs/{importId}/begin
var beginImportJobUri = RelativityImportEndpoints.GetImportJobBeginUri(workspaceId, importId);
response = await httpClient.PostAsync(beginImportJobUri, null);
await ImportJobSampleHelper.EnsureSuccessResponse(response);
// End import job.
// endpoint: POST /import-jobs/{importId}/end
var endImportJobUri = RelativityImportEndpoints.GetImportJobEndUri(workspaceId, importId);
response = await httpClient.PostAsync(endImportJobUri, null);
await ImportJobSampleHelper.EnsureSuccessResponse(response);
// It may take some time for import job to be completed. Request data source details to monitor the current state.
// NOTE: You can also request job details to verify if job is finished - see appropriate sample.
// endpoint: GET import-jobs/{importId}/sources/{sourceId}/details"
var importSourceDetailsUri = RelativityImportEndpoints.GetImportSourceDetailsUri(workspaceId, importId, sourceId);
JsonSerializerOptions options = new ()
{
Converters = { new JsonStringEnumConverter() },
};
var dataSourceState = await ImportJobSampleHelper.WaitImportDataSourceToBeCompleted(
funcAsync: () => httpClient.GetFromJsonAsync<ValueResponse<DataSourceDetails>>(importSourceDetailsUri, options),
timeout: 10000);
// Get current import progress for specific data source.
// endpoint: GET import-jobs/{importId}/sources/{sourceId}/progress"
var importSourceProgressUri = RelativityImportEndpoints.GetImportSourceProgressUri(workspaceId, importId, sourceId);
var valueResponse = await httpClient.GetFromJsonAsync<ValueResponse<ImportProgress>>(importSourceProgressUri);
if (valueResponse?.IsSuccess ?? false)
{
Console.WriteLine("\n");
Console.WriteLine($"Data source state: {dataSourceState}");
Console.WriteLine($"Import data source progress: Total records: {valueResponse.Value.TotalRecords}, Imported records: {valueResponse.Value.ImportedRecords}, Records with errors: {valueResponse.Value.ErroredRecords}");
}
}
}
}
/* Expected console result:
Data source state: Completed
Import data source progress: Total records: 4, Imported records: 4, Records with errors: 0
*/