-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileProcessorClient.cs
More file actions
261 lines (213 loc) · 11.5 KB
/
FileProcessorClient.cs
File metadata and controls
261 lines (213 loc) · 11.5 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using Shared.Results;
namespace FileProcessor.Client {
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using ClientProxyBase;
using DataTransferObjects;
using DataTransferObjects.Responses;
using Newtonsoft.Json;
using SimpleResults;
/// <summary>
///
/// </summary>
/// <seealso cref="ClientProxyBase" />
/// <seealso cref="FileProcessor.Client.IFileProcessorClient" />
public class FileProcessorClient : ClientProxyBase, IFileProcessorClient {
#region Fields
/// <summary>
/// The base address resolver
/// </summary>
private readonly Func<String, String> BaseAddressResolver;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="FileProcessorClient"/> class.
/// </summary>
/// <param name="baseAddressResolver">The base address resolver.</param>
/// <param name="httpClient">The HTTP client.</param>
public FileProcessorClient(Func<String, String> baseAddressResolver,
HttpClient httpClient) : base(httpClient) {
this.BaseAddressResolver = baseAddressResolver;
// Add the API version header
this.HttpClient.DefaultRequestHeaders.Add("api-version", "1.0");
}
#endregion
#region Methods
/// <summary>
/// Gets the file.
/// </summary>
/// <param name="accessToken">The access token.</param>
/// <param name="estateId">The estate identifier.</param>
/// <param name="fileId">The file identifier.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public async Task<Result<FileDetails>> GetFile(String accessToken,
Guid estateId,
Guid fileId,
CancellationToken cancellationToken) {
String requestUri = this.BuildRequestUrl($"/api/files/{fileId}?estateId={estateId}");
try {
// Add the access token header
this.HttpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", accessToken);
// Make the Http Call here
HttpResponseMessage httpResponse = await this.HttpClient.GetAsync(requestUri, cancellationToken);
// Process the response
Result<String> result = await this.HandleResponseX(httpResponse, cancellationToken);
if (result.IsFailed)
return ResultHelpers.CreateFailure(result);
ResponseData<FileDetails> responseData =
HandleResponseContent<FileDetails>(result.Data);
// call was successful so now deserialise the body to the response object
return Result.Success(responseData.Data);
}
catch (Exception ex) {
// An exception has occurred, add some additional information to the message
Exception exception = new Exception($"Error getting file with Id {fileId}.", ex);
throw exception;
}
}
/// <summary>
/// Gets the file import log.
/// </summary>
/// <param name="accessToken">The access token.</param>
/// <param name="fileImportLogId">The file import log identifier.</param>
/// <param name="estateId">The estate identifier.</param>
/// <param name="merchantId">The merchant identifier.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public async Task<Result<FileImportLog>> GetFileImportLog(String accessToken,
Guid fileImportLogId,
Guid estateId,
Guid? merchantId,
CancellationToken cancellationToken) {
String requestUri = this.BuildRequestUrl($"/api/fileImportLogs/{fileImportLogId}?estateId={estateId}");
if (merchantId.HasValue) {
requestUri += $"&merchantId={merchantId}";
}
try {
// Add the access token header
this.HttpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", accessToken);
// Make the Http Call here
HttpResponseMessage httpResponse = await this.HttpClient.GetAsync(requestUri, cancellationToken);
// Process the response
Result<String> result = await this.HandleResponseX(httpResponse, cancellationToken);
if (result.IsFailed)
return ResultHelpers.CreateFailure(result);
ResponseData<FileImportLog> responseData =
HandleResponseContent<FileImportLog>(result.Data);
// call was successful so now deserialise the body to the response object
return Result.Success(responseData.Data);
}
catch (Exception ex) {
// An exception has occurred, add some additional information to the message
Exception exception = new Exception("Error getting file import log.", ex);
throw exception;
}
}
/// <summary>
/// Gets the file import logs.
/// </summary>
/// <param name="accessToken">The access token.</param>
/// <param name="estateId">The estate identifier.</param>
/// <param name="startDateTime">The start date time.</param>
/// <param name="endDateTime">The end date time.</param>
/// <param name="merchantId">The merchant identifier.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public async Task<Result<FileImportLogList>> GetFileImportLogs(String accessToken,
Guid estateId,
DateTime startDateTime,
DateTime endDateTime,
Guid? merchantId,
CancellationToken cancellationToken) {
String requestUri =
this.BuildRequestUrl(
$"/api/fileImportLogs?estateId={estateId}&startDateTime={startDateTime.Date:yyyy-MM-dd}&endDateTime={endDateTime.Date:yyyy-MM-dd}");
if (merchantId.HasValue) {
requestUri += $"&merchantId={merchantId}";
}
try {
// Add the access token header
this.HttpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", accessToken);
// Make the Http Call here
HttpResponseMessage httpResponse = await this.HttpClient.GetAsync(requestUri, cancellationToken);
// Process the response
Result<String> result = await this.HandleResponseX(httpResponse, cancellationToken);
if (result.IsFailed)
return ResultHelpers.CreateFailure(result);
ResponseData<FileImportLogList> responseData =
HandleResponseContent<FileImportLogList>(result.Data);
// call was successful so now deserialise the body to the response object
return Result.Success(responseData.Data);
}
catch (Exception ex) {
// An exception has occurred, add some additional information to the message
Exception exception = new Exception("Error getting list of file import logs.", ex);
throw exception;
}
}
/// <summary>
/// Uploads the file.
/// </summary>
/// <param name="accessToken">The access token.</param>
/// <param name="fileName">Name of the file.</param>
/// <param name="fileData">The file data.</param>
/// <param name="uploadFileRequest">The upload file request.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public async Task<Result<Guid>> UploadFile(String accessToken,
String fileName,
Byte[] fileData,
UploadFileRequest uploadFileRequest,
CancellationToken cancellationToken) {
try {
String requestUri = this.BuildRequestUrl("/api/files");
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post, requestUri);
MultipartFormDataContent formData = new MultipartFormDataContent();
ByteArrayContent fileContent = new ByteArrayContent(fileData);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
formData.Add(fileContent, "file", fileName);
formData.Add(new StringContent(uploadFileRequest.EstateId.ToString()), "request.EstateId");
formData.Add(new StringContent(uploadFileRequest.MerchantId.ToString()), "request.MerchantId");
formData.Add(new StringContent(uploadFileRequest.FileProfileId.ToString()), "request.FileProfileId");
formData.Add(new StringContent(uploadFileRequest.UserId.ToString()), "request.UserId");
formData.Add(new StringContent(uploadFileRequest.UploadDateTime.ToString("yyyy-MM-dd HH:mm:ss")),
"request.UploadDateTime");
httpRequest.Content = formData;
httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(httpRequest, cancellationToken);
// Process the response
Result<String> result = await this.HandleResponseX(httpResponse, cancellationToken);
if (result.IsFailed)
return ResultHelpers.CreateFailure(result);
ResponseData<Guid> responseData =
HandleResponseContent<Guid>(result.Data);
// call was successful so now deserialise the body to the response object
return Result.Success(responseData.Data);
}
catch (Exception ex) {
// An exception has occurred, add some additional information to the message
Exception exception = new Exception($"Error uploading file {fileName}.", ex);
throw exception;
}
}
/// <summary>
/// Builds the request URL.
/// </summary>
/// <param name="route">The route.</param>
/// <returns></returns>
private String BuildRequestUrl(String route) {
String baseAddress = this.BaseAddressResolver("FileProcessorApi");
String requestUri = $"{baseAddress}{route}";
return requestUri;
}
#endregion
}
}